diff --git a/src/amo/middleware/prefixMiddleware.js b/src/amo/middleware/prefixMiddleware.js index 3fdfcfd0fd8..911343c2416 100644 --- a/src/amo/middleware/prefixMiddleware.js +++ b/src/amo/middleware/prefixMiddleware.js @@ -28,6 +28,7 @@ export function prefixMiddleware(req, res, next, { _config = config } = {}) { lang: URLPathParts[0], acceptLanguage, }); + let hasUnknownPartOne = false; const hasValidLang = isValidLang(URLPathParts[0]); const hasValidClientAppInPartOne = isValidClientApp(URLPathParts[0], { @@ -58,9 +59,11 @@ export function prefixMiddleware(req, res, next, { _config = config } = {}) { hasValidClientAppUrlExceptionInPartTwo ) { log.debug(`Replacing lang in URL: ${URLPathParts[0]} with ${lang}`); + hasUnknownPartOne = true; URLPathParts.splice(0, 1, lang); } else { log.debug(`Prepending lang and clientApp to URL: ${lang}/${userAgentApp}`); + hasUnknownPartOne = !!URLPathParts[0]; URLPathParts.splice(0, 0, lang, userAgentApp); isApplicationFromHeader = true; } @@ -104,7 +107,10 @@ export function prefixMiddleware(req, res, next, { _config = config } = {}) { res.vary('user-agent'); } res.set('Cache-Control', [`max-age=${ONE_YEAR_IN_SECONDS}`]); - return res.redirect(301, newURL); + // If there was something at the beginning of the URL we didn't recognize, + // it could be an old locale we have since disabled and might re-enable + // later, so make the redirect temporary (302), otherwise permanent (301). + return res.redirect(hasUnknownPartOne ? 302 : 301, newURL); } // Add the data to res.locals to be utilised later. diff --git a/tests/unit/amo/middleware/test_prefixMiddleware.js b/tests/unit/amo/middleware/test_prefixMiddleware.js index 33d6f890d5b..8efab3e5b0c 100644 --- a/tests/unit/amo/middleware/test_prefixMiddleware.js +++ b/tests/unit/amo/middleware/test_prefixMiddleware.js @@ -32,7 +32,7 @@ describe(__filename, () => { headers: {}, }; prefixMiddleware(fakeReq, fakeRes, fakeNext, { _config: fakeConfig }); - sinon.assert.calledWith(fakeRes.redirect, 301, '/en-US/firefox'); + sinon.assert.calledWith(fakeRes.redirect, 302, '/en-US/firefox'); sinon.assert.calledWith(fakeRes.set, 'Cache-Control', ['max-age=31536000']); sinon.assert.notCalled(fakeNext); }); @@ -43,7 +43,7 @@ describe(__filename, () => { headers: {}, }; prefixMiddleware(fakeReq, fakeRes, fakeNext, { _config: fakeConfig }); - sinon.assert.calledWith(fakeRes.redirect, 301, '/en-US/firefox'); + sinon.assert.calledWith(fakeRes.redirect, 302, '/en-US/firefox'); sinon.assert.calledWith(fakeRes.set, 'Cache-Control', ['max-age=31536000']); sinon.assert.notCalled(fakeNext); }); @@ -77,7 +77,7 @@ describe(__filename, () => { prefixMiddleware(fakeReq, fakeRes, fakeNext, { _config: fakeConfig }); sinon.assert.calledWith( fakeRes.redirect, - 301, + 302, '/en-US/validprefix/whatever', ); sinon.assert.calledWith(fakeRes.set, 'Cache-Control', ['max-age=31536000']); @@ -90,7 +90,7 @@ describe(__filename, () => { headers: {}, }; prefixMiddleware(fakeReq, fakeRes, fakeNext, { _config: fakeConfig }); - sinon.assert.calledWith(fakeRes.redirect, 301, '/en-US/developers/'); + sinon.assert.calledWith(fakeRes.redirect, 302, '/en-US/developers/'); sinon.assert.calledWith(fakeRes.set, 'Cache-Control', ['max-age=31536000']); sinon.assert.calledWith(fakeRes.vary, 'accept-language'); }); @@ -114,7 +114,7 @@ describe(__filename, () => { headers: {}, }; prefixMiddleware(fakeReq, fakeRes, fakeNext, { _config: fakeConfig }); - sinon.assert.calledWith(fakeRes.redirect, 301, '/pt-PT/firefox/whatever'); + sinon.assert.calledWith(fakeRes.redirect, 302, '/pt-PT/firefox/whatever'); sinon.assert.calledWith(fakeRes.set, 'Cache-Control', ['max-age=31536000']); }); @@ -126,7 +126,7 @@ describe(__filename, () => { }, }; prefixMiddleware(fakeReq, fakeRes, fakeNext, { _config: fakeConfig }); - sinon.assert.calledWith(fakeRes.redirect, 301, '/pt-BR/firefox/whatever'); + sinon.assert.calledWith(fakeRes.redirect, 302, '/pt-BR/firefox/whatever'); sinon.assert.calledWith(fakeRes.vary, 'accept-language'); sinon.assert.calledWith(fakeRes.vary, 'user-agent'); sinon.assert.calledWith(fakeRes.set, 'Cache-Control', ['max-age=31536000']); @@ -218,7 +218,7 @@ describe(__filename, () => { prefixMiddleware(fakeReq, fakeRes, fakeNext, { _config: fakeConfig }); sinon.assert.calledWith( fakeRes.redirect, - 301, + 302, '/en-US/firefox/foo/bar?test=1&bar=2', ); sinon.assert.calledWith(fakeRes.set, 'Cache-Control', ['max-age=31536000']);