diff --git a/middleware/catch-bad-accept-language.js b/middleware/catch-bad-accept-language.js deleted file mode 100644 index 4c0fc0b36a..0000000000 --- a/middleware/catch-bad-accept-language.js +++ /dev/null @@ -1,15 +0,0 @@ -import accept from '@hapi/accept' - -// Next.JS uses the @hapi/accept package to parse and detect languages. If the accept-language header is malformed -// it throws an error from within Next.JS, which results in a 500 response. This ends up being noisy because we -// track 500s. To counteract this, we'll try to catch the error first and make sure it doesn't happen -export default function catchBadAcceptLanguage(req, res, next) { - try { - accept.language(req.headers['accept-language']) - } catch (e) { - // if there's a problem with parsing 'accept-language', just clear it out. - req.headers['accept-language'] = '' - } - - return next() -} diff --git a/middleware/index.js b/middleware/index.js index 691fedd7c3..ae270b0bef 100644 --- a/middleware/index.js +++ b/middleware/index.js @@ -17,7 +17,6 @@ import csrf from './csrf.js' import handleCsrfErrors from './handle-csrf-errors.js' import { setDefaultFastlySurrogateKey } from './set-fastly-surrogate-key.js' import setFastlyCacheHeaders from './set-fastly-cache-headers.js' -import catchBadAcceptLanguage from './catch-bad-accept-language.js' import reqUtils from './req-utils.js' import recordRedirect from './record-redirect.js' import connectSlashes from 'connect-slashes' @@ -207,7 +206,6 @@ export default function (app) { // *** Headers *** app.set('etag', false) // We will manage our own ETags if desired - app.use(catchBadAcceptLanguage) // *** Config and context for redirects *** app.use(reqUtils) // Must come before record-redirect and events diff --git a/package-lock.json b/package-lock.json index ccbb53ae7f..ffb2aff60c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,6 @@ "license": "(MIT AND CC-BY-4.0)", "dependencies": { "@github/failbot": "0.8.0", - "@hapi/accept": "^5.0.2", "@primer/components": "^33.1.0", "@primer/css": "^19.1.1", "@primer/octicons": "^16.3.0", diff --git a/package.json b/package.json index dbd46ec6b2..59dba3dcf7 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,6 @@ ], "dependencies": { "@github/failbot": "0.8.0", - "@hapi/accept": "^5.0.2", "@primer/components": "^33.1.0", "@primer/css": "^19.1.1", "@primer/octicons": "^16.3.0", diff --git a/tests/rendering/server.js b/tests/rendering/server.js index 2c35f0840d..970c21c7fa 100644 --- a/tests/rendering/server.js +++ b/tests/rendering/server.js @@ -664,6 +664,24 @@ describe('server', () => { ) }) + // This test exists because in a previous life, our NextJS used to + // 500 if the 'Accept-Language' header was malformed. + // We *used* have a custom middleware to cope with this and force a + // fallback redirect. + // See internal issue 19909 + test('redirects /en if Accept-Language header is malformed', async () => { + const res = await get('/', { + headers: { + 'accept-language': 'ldfir;', + }, + }) + + expect(res.statusCode).toBe(302) + expect(res.headers.location).toBe('/en') + expect(res.headers['cache-control']).toBe('private, no-store') + expect(res.headers['set-cookie']).toBeUndefined() + }) + test('redirects / to /en when unsupported language preference is specified', async () => { const res = await get('/', { headers: {