Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 7 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 hasUnknownPartOne = false;

const hasValidLang = isValidLang(URLPathParts[0]);
const hasValidClientAppInPartOne = isValidClientApp(URLPathParts[0], {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.
Expand Down
14 changes: 7 additions & 7 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 @@ -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']);
Expand All @@ -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 All @@ -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']);
Expand Down Expand Up @@ -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']);
Expand Down