Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add redirectStatusCode option #1471

Open
wants to merge 1 commit into
base: v7
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/content/en/options-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ Set to a path to which you want to redirect users accessing the root URL (`/`).
}
```

## `redirectStatusCode`

- type: `number`
- default: `302`

Customize the status code to redirect unprefixed route to the default locale.

## `differentDomains`

- type: `boolean`
Expand Down
1 change: 1 addition & 0 deletions src/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const DEFAULT_OPTIONS = {
lazy: false,
langDir: null,
rootRedirect: null,
redirectStatusCode: 302,
detectBrowserLanguage: {
alwaysRedirect: false,
cookieCrossOrigin: false,
Expand Down
2 changes: 1 addition & 1 deletion src/templates/plugin.main.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export default async (context) => {
const storedRedirect = app.i18n.__redirect
if (storedRedirect) {
app.i18n.__redirect = null
return [302, storedRedirect]
return [options.redirectStatusCode, storedRedirect]
}

const resolveBaseUrlOptions = {
Expand Down
30 changes: 30 additions & 0 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,36 @@ describe('with rootRedirect (object)', () => {
})
})

describe('with redirectStatusCode', () => {
/** @type {Nuxt} */
let nuxt

beforeAll(async () => {
const override = {
i18n: {
redirectStatusCode: 301,
strategy: 'prefix'
}
}
nuxt = (await setup(loadConfig(__dirname, 'basic', override, { merge: true }))).nuxt
})

afterAll(async () => {
await nuxt.close()
})

test('redirect unprefixed route /about-us to /en/about-us with correct status', async () => {
const requestOptions = {
followRedirect: false,
resolveWithFullResponse: true,
simple: false // Don't reject on non-2xx response
}
const response = await get('/about-us', requestOptions)
expect(response.statusCode).toBe(301)
expect(response.headers.location).toBe('/en/about-us')
})
})

describe('prefix_and_default strategy', () => {
/** @type {Nuxt} */
let nuxt
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export interface Options extends BaseOptions {
}
parsePages?: boolean
rootRedirect?: string | null | RootRedirectOptions
redirectStatusCode?: number
routesNameSeparator?: string
skipSettingLocaleOnNavigate?: boolean,
sortRoutes?: boolean,
Expand Down