Skip to content

Commit

Permalink
change default of serverSetCookie to "always"
Browse files Browse the repository at this point in the history
  • Loading branch information
dcporter44 committed Feb 8, 2024
1 parent b3e839c commit b11b9c2
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 43 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 5.3.0

- Change default of `serverSetCookie` to `"always"`.
- Add `"never"` option to `serverSetCookie`. This is the same as `undefined` in previous versions.

## 5.2.1

- Update dependencies
Expand Down
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ You now have internationalized routing!

## Config Options

| Option | Default value | Type | Required? |
| ----------------- | --------------- | ---------------------- | --------- |
| `locales` | | string[] | ✔ |
| `defaultLocale` | | string | ✔ |
| `prefixDefault` | `false` | boolean | |
| `localeDetector` | (See below) | function \| false | |
| `localeCookie` | `'NEXT_LOCALE'` | string | |
| `serverSetCookie` | | "always" \| "if-empty" | |
| `basePath` | `''` | string | |
| Option | Default value | Type | Required? |
| ----------------- | --------------- | --------------------------------- | --------- |
| `locales` | | string[] | ✔ |
| `defaultLocale` | | string | ✔ |
| `prefixDefault` | `false` | boolean | |
| `localeDetector` | (See below) | function \| false | |
| `localeCookie` | `'NEXT_LOCALE'` | string | |
| `serverSetCookie` | `'always'` | "always" \| "if-empty" \| "never" | |
| `basePath` | `''` | string | |

## Locale Path Prefixing

Expand Down Expand Up @@ -113,11 +113,11 @@ If you would prefer to use a different cookie key other than `NEXT_LOCALE`, you

The `serverSetCookie` option automatically changes a visitor's preferred locale cookie by simply visiting a pathname that contains a locale.

`'always'`: When the pathname of a request includes a locale, that locale will be set as the cookie by the middleware. This means that locale detection and any existing locale cookie will be ignored if a locale exists in the request's pathname. Locale detection and the reading of any existing cookie will still be run on pathnames that do not include a locale.
`'always'`(default): When the pathname of a request includes a locale, that locale will be set as the cookie by the middleware. This means that locale detection and any existing locale cookie will be ignored if a locale exists in the request's pathname. Locale detection and the reading of any existing cookie will still be run on pathnames that do not include a locale.

`'if-empty'`: Same as `'always'`, except the middleware will not overwrite the cookie if one already exists.

`undefined` (default): When `serverSetCookie` is not set, the middleware will not automatically set the cookie.
`'never'`: The middleware will not automatically set the cookie.

## Using `basePath` (optional)

Expand Down
74 changes: 47 additions & 27 deletions __tests__/i18nRouter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ basePaths.forEach(basePath => {
const config = {
locales: 'invalid',
defaultLocale: 'en-US',
basePath
basePath,
serverSetCookie: 'never'
};

// @ts-ignore
Expand All @@ -20,7 +21,7 @@ basePaths.forEach(basePath => {
});

it('should throw an error if defaultLocale is not defined', () => {
const config = { locales: ['en-US'], basePath };
const config = { locales: ['en-US'], basePath, serverSetCookie: 'never' };

// @ts-ignore
expect(() => i18nRouter(mockRequest('/', ['en']), config)).toThrow(
Expand All @@ -29,28 +30,35 @@ basePaths.forEach(basePath => {
});

it('should throw an error if defaultLocale is not in locales array', () => {
const config = {
locales: ['en-US'],
defaultLocale: 'invalid',
basePath
};
expect(() => i18nRouter(mockRequest('/', ['en']), config)).toThrow(
/defaultLocale/
);
expect(() =>
i18nRouter(mockRequest('/', ['en']), {
locales: ['en-US'],
defaultLocale: 'invalid',
basePath,
serverSetCookie: 'never'
})
).toThrow(/defaultLocale/);
});

it('should throw an error if localeDetector is not a function', () => {
const config = {
locales: ['en-US'],
defaultLocale: 'en-US',
localeDetector: 'invalid',
basePath
basePath,
serverSetCookie: 'never'
};

// @ts-ignore
expect(() => i18nRouter(mockRequest('/', ['en']), config)).toThrow(
/localeDetector/
);
expect(() =>
i18nRouter(mockRequest('/', ['en']), {
locales: ['en-US'],
defaultLocale: 'en-US',
// @ts-ignore
localeDetector: 'invalid',
basePath,
serverSetCookie: 'never'
})
).toThrow(/localeDetector/);
});

it('should throw an error if request argument is missing', () => {
Expand All @@ -67,7 +75,8 @@ basePaths.forEach(basePath => {
i18nRouter(request, {
locales: ['en', 'jp'],
defaultLocale: 'en',
basePath
basePath,
serverSetCookie: 'never'
});

expect(mockRedirect).toHaveBeenCalledTimes(0);
Expand All @@ -83,7 +92,8 @@ basePaths.forEach(basePath => {
locales: ['en', 'jp'],
defaultLocale: 'en',
prefixDefault: true,
basePath
basePath,
serverSetCookie: 'never'
});

expect(mockRedirect).toHaveBeenCalledWith(
Expand All @@ -100,7 +110,8 @@ basePaths.forEach(basePath => {
i18nRouter(request, {
locales: ['en', 'jp'],
defaultLocale: 'en',
basePath
basePath,
serverSetCookie: 'never'
});

expect(mockRedirect).toHaveBeenCalledWith(
Expand All @@ -118,7 +129,8 @@ basePaths.forEach(basePath => {
locales: ['en', 'jp'],
defaultLocale: 'en',
prefixDefault: true,
basePath
basePath,
serverSetCookie: 'never'
});

expect(mockRedirect).toHaveBeenCalledWith(
Expand All @@ -135,7 +147,8 @@ basePaths.forEach(basePath => {
i18nRouter(request, {
locales: ['en', 'jp'],
defaultLocale: 'en',
basePath
basePath,
serverSetCookie: 'never'
});

expect(mockRedirect).toHaveBeenCalledWith(
Expand All @@ -152,7 +165,8 @@ basePaths.forEach(basePath => {
i18nRouter(request, {
locales: ['en', 'jp'],
defaultLocale: 'en',
basePath
basePath,
serverSetCookie: 'never'
});

expect(mockRedirect).toHaveBeenCalledTimes(0);
Expand All @@ -167,7 +181,8 @@ basePaths.forEach(basePath => {
i18nRouter(request, {
locales: ['en', 'jp'],
defaultLocale: 'en',
basePath
basePath,
serverSetCookie: 'never'
});

expect(mockRedirect).toHaveBeenCalledTimes(0);
Expand All @@ -182,7 +197,8 @@ basePaths.forEach(basePath => {
i18nRouter(request, {
locales: ['en', 'jp'],
defaultLocale: 'en',
basePath
basePath,
serverSetCookie: 'never'
});

expect(mockRedirect).toHaveBeenCalledWith(
Expand All @@ -199,7 +215,8 @@ basePaths.forEach(basePath => {
i18nRouter(request, {
locales: ['en', 'jp'],
defaultLocale: 'en',
basePath
basePath,
serverSetCookie: 'never'
});

expect(mockRedirect).toHaveBeenCalledTimes(0);
Expand All @@ -214,7 +231,8 @@ basePaths.forEach(basePath => {
i18nRouter(request, {
locales: ['en', 'jp'],
defaultLocale: 'en',
basePath
basePath,
serverSetCookie: 'never'
});

expect(mockRedirect).toHaveBeenCalledTimes(0);
Expand All @@ -229,7 +247,8 @@ basePaths.forEach(basePath => {
i18nRouter(request, {
locales: ['en', 'jp'],
defaultLocale: 'en',
basePath
basePath,
serverSetCookie: 'never'
});

expect(mockRedirect).toHaveBeenCalledWith(
Expand All @@ -246,7 +265,8 @@ basePaths.forEach(basePath => {
i18nRouter(request, {
locales: ['en', 'de', 'jp'],
defaultLocale: 'en',
basePath
basePath,
serverSetCookie: 'never'
});

expect(mockRedirect).toHaveBeenCalledWith(
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-i18n-router",
"version": "5.2.1",
"version": "5.3.0",
"description": "Next.js App Router internationalized routing and locale detection.",
"repository": "https://github.com/i18nexus/next-i18n-router",
"keywords": [
Expand Down
4 changes: 2 additions & 2 deletions src/i18nRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function i18nRouter(request: NextRequest, config: Config): NextResponse {
localeDetector = defaultLocaleDetector,
prefixDefault = false,
basePath = '',
serverSetCookie
serverSetCookie = 'always'
} = config;

validateConfig(config);
Expand Down Expand Up @@ -134,7 +134,7 @@ function i18nRouter(request: NextRequest, config: Config): NextResponse {
});
};

if (serverSetCookie) {
if (serverSetCookie !== 'never') {
if (
cookieLocale &&
cookieLocale !== pathLocale &&
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export interface Config {
localeDetector?: ((request: NextRequest, config: Config) => string) | false;
prefixDefault?: boolean;
basePath?: string;
serverSetCookie?: 'if-empty' | 'always';
serverSetCookie?: 'if-empty' | 'always' | 'never';
}

0 comments on commit b11b9c2

Please sign in to comment.