Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/amo/middleware/prefixMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function prefixMiddleware(req, res, next, { _config = config } = {}) {
lang: URLPathParts[0],
acceptLanguage,
});
let hasReplacedLang = false;

const hasValidLang = isValidLang(URLPathParts[0]);
const hasValidClientAppInPartOne = isValidClientApp(URLPathParts[0], {
Expand Down Expand Up @@ -58,6 +59,7 @@ export function prefixMiddleware(req, res, next, { _config = config } = {}) {
hasValidClientAppUrlExceptionInPartTwo
) {
log.debug(`Replacing lang in URL: ${URLPathParts[0]} with ${lang}`);
hasReplacedLang = true;
URLPathParts.splice(0, 1, lang);
} else {
log.debug(`Prepending lang and clientApp to URL: ${lang}/${userAgentApp}`);
Expand Down Expand Up @@ -104,7 +106,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 a locale in the URL but we considered it invalid,
// return a 302 temporary redirect, in case this was a locale we have
// disabled and might re-enable later, otherwise 301 permanent.
return res.redirect(hasReplacedLang ? 302 : 301, newURL);
}

// Add the data to res.locals to be utilised later.
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/amo/middleware/test_prefixMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
});
Expand Down Expand Up @@ -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');
});
Expand All @@ -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']);
});

Expand Down