Skip to content

Commit

Permalink
fix: 302 -> 301 permanent redirect for routes without locale
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Jan 8, 2023
1 parent f9d5ffa commit 5f13a86
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ class I18N {
// if the locale was not available then redirect user
if (locale !== ctx.state.locale) {
debug('locale was not available redirecting user');
ctx.status = 301;
return ctx.redirect(
`/${ctx.state.locale}${
ctx.pathWithoutLocale === '/' ? '' : ctx.pathWithoutLocale
Expand Down Expand Up @@ -322,7 +323,7 @@ class I18N {
// if the URL did not have a valid language found
// then redirect the user to their detected locale
if (!hasLang) {
ctx.status = 302;
ctx.status = 301;
let redirect = `/${ctx.request.locale}${ctx.path}`;
if (redirect === `/${ctx.request.locale}/`)
redirect = `/${ctx.request.locale}`;
Expand Down
18 changes: 9 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ test('redirects if locale is not avaiable', async (t) => {

const res = await request(app.listen()).get('/');

t.is(res.status, 302);
t.is(res.status, 301);
t.is(res.header.location, '/en');
});

Expand Down Expand Up @@ -250,7 +250,7 @@ test('redirects if locale is not avaiable with query', async (t) => {

const res = await request(app.listen()).get('/');

t.is(res.status, 302);
t.is(res.status, 301);
t.is(res.header.location, '/en?a=b');
});

Expand Down Expand Up @@ -328,7 +328,7 @@ test('does not redirect with ignored redirect globs', async (t) => {
res = await request(app.listen())
.get('/login/beep/baz')
.set('Cookie', ['locale=es']);
t.is(res.status, 302);
t.is(res.status, 301);
t.is(res.headers.location, '/es/login/beep/baz');
});

Expand All @@ -352,7 +352,7 @@ test('does not duplicate querystring if no locale provided', async (t) => {
'/?test=test?test=test?test=test'
);

t.is(res.status, 302);
t.is(res.status, 301);
t.is(res.headers.location, '/en?test=test%3Ftest%3Dtest%3Ftest%3Dtest');
});

Expand All @@ -371,7 +371,7 @@ test('redirectIgnoresNonGetMethods', async (t) => {
});

let res = await request(app.listen()).get('/');
t.is(res.status, 302);
t.is(res.status, 301);
t.is(res.headers.location, '/en');

res = await request(app.listen()).post('/');
Expand Down Expand Up @@ -419,7 +419,7 @@ test('redirects to correct path based on locale set via cookie', async (t) => {

const res = await request(app.listen()).get('/').set('Cookie', ['locale=es']);

t.is(res.status, 302);
t.is(res.status, 301);
t.is(res.headers.location, '/es');
});

Expand Down Expand Up @@ -614,11 +614,11 @@ for (const tld of tlds) {
let res = await request(app.listen())
.get(route)
.set('Cookie', ['locale=es']);
t.is(res.status, 302);
t.is(res.status, 301);
t.is(res.header.location, `/es/login.${toASCII(tld)}`);

res = await request(app.listen()).get(route).set('Cookie', ['locale=es']);
t.is(res.status, 302);
t.is(res.status, 301);
t.is(res.header.location, `/es/login.${toASCII(tld)}`);
});
}
Expand Down Expand Up @@ -690,6 +690,6 @@ test('supports detectLocale function', async (t) => {

const res = await request(app.listen()).get('/');

t.is(res.status, 302);
t.is(res.status, 301);
t.is(res.header.location, '/zh');
});

0 comments on commit 5f13a86

Please sign in to comment.