Skip to content

Commit

Permalink
fix: redirect to 301 instead of 302 in case when only trailing slashe…
Browse files Browse the repository at this point in the history
…s are present
  • Loading branch information
wRLSS committed Oct 23, 2024
1 parent 320723c commit b542240
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ilc/server/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ const onRequestFactory =
containsOnlySlashes(fixedUrl) &&
containsOnlySlashes(req.raw.url);

if (containsOnlySlashes(req.raw.url) && !shouldSkipRedirectForSlashes) {
return reply.redirect(301, '/');
}

if (fixedUrl !== req.raw.url && !shouldSkipRedirectForSlashes) {
return reply.redirect(fixedUrl);
}
Expand Down
21 changes: 21 additions & 0 deletions ilc/server/i18n.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,27 @@ describe('i18n', () => {
sinon.assert.calledWithExactly(reply.redirect, '/ua/test');
});

it('redirects trailing slash with 301 code', async () => {
const detectedI18nConfig = {
locale: 'en-US',
currency: 'USD',
};

i18nParamsDetectionPlugin.detectI18nConfig.onFirstCall().returns(detectedI18nConfig);

const req = getReqMock('///');

await onRequest(req, reply);

const [providedReqRaw, , providedI18nConfig] =
i18nParamsDetectionPlugin.detectI18nConfig.getCalls()[0].args;

chai.expect(providedReqRaw).to.be.eql(req.raw);
chai.expect(providedI18nConfig).to.be.eql(i18nConfig.default);

sinon.assert.calledWithExactly(reply.redirect, 301, '/');
});

it('ua-ua, redirects to URL with correct lang code', async () => {
const detectedI18nConfig = {
locale: 'ua-ua',
Expand Down

0 comments on commit b542240

Please sign in to comment.