Skip to content

Commit

Permalink
fix: Handle case where the locale param is an array and set cookie …
Browse files Browse the repository at this point in the history
…expiration to one year (#435)
  • Loading branch information
amannn authored Aug 1, 2023
1 parent 431764d commit 82e842c
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 29 deletions.
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@vercel/analytics": "^1.0.1",
"clsx": "^1.2.1",
"http-status-codes": "^2.2.0",
"next": "^13.4.7",
"next": "13.4.7",
"nextra": "^2.8.0",
"nextra-theme-docs": "^2.8.0",
"react": "^18.2.0",
Expand Down
4 changes: 2 additions & 2 deletions examples/example-advanced/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"dependencies": {
"accept-language-parser": "^1.5.0",
"lodash": "^4.17.21",
"next": "^13.4.7",
"next-intl": "^2.14.3",
"next": "13.4.7",
"next-intl": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
4 changes: 2 additions & 2 deletions examples/example-next-13-next-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"start": "next start"
},
"dependencies": {
"next": "^13.4.7",
"next": "13.4.7",
"next-auth": "^4.22.1",
"next-intl": "^2.14.3",
"next-intl": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
4 changes: 2 additions & 2 deletions examples/example-next-13/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
},
"dependencies": {
"clsx": "^1.2.1",
"next": "^13.4.7",
"next-intl": "^2.14.3",
"next": "13.4.7",
"next-intl": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"tailwindcss": "^3.2.4"
Expand Down
9 changes: 6 additions & 3 deletions examples/example-next-13/tests/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ it('can be used to localize the page', async ({page}) => {

it('sets a cookie', async ({page}) => {
const response = await page.goto('/');
expect(await response?.headerValue('set-cookie')).toBe(
'NEXT_LOCALE=en; Path=/; SameSite=strict'
);
const value = await response?.headerValue('set-cookie');
expect(value).toContain('NEXT_LOCALE=en;');
expect(value).toContain('Path=/;');
expect(value).toContain('SameSite=strict');
expect(value).toContain('Max-Age=31536000;');
expect(value).toContain('Expires=');
});
4 changes: 2 additions & 2 deletions examples/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
},
"dependencies": {
"date-fns": "^2.16.1",
"next": "^13.4.7",
"next-intl": "^2.14.3",
"next": "13.4.7",
"next-intl": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/next-intl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"eslint": "^8.39.0",
"eslint-config-molindo": "^6.0.0",
"eslint-plugin-deprecation": "^1.4.1",
"next": "^13.4.7",
"next": "13.4.7",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"size-limit": "^8.2.6",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-intl/src/client/useClientLocale.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function useClientLocale(): string {
// `useParams` can be called, but the return type is `null`.
const params = useParams() as ReturnType<typeof useParams> | null;

if (params?.[LOCALE_SEGMENT_NAME]) {
if (typeof params?.[LOCALE_SEGMENT_NAME] === 'string') {
locale = params[LOCALE_SEGMENT_NAME];
} else {
// eslint-disable-next-line react-hooks/rules-of-hooks -- Reading from context conditionally is fine
Expand Down
3 changes: 2 additions & 1 deletion packages/next-intl/src/middleware/middleware.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ export default function createMiddleware(config: MiddlewareConfig) {

if (hasOutdatedCookie) {
response.cookies.set(COOKIE_LOCALE_NAME, locale, {
sameSite: 'strict'
sameSite: 'strict',
maxAge: 31536000 // 1 year
});
}

Expand Down
32 changes: 18 additions & 14 deletions pnpm-lock.yaml

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

3 comments on commit 82e842c

@vercel
Copy link

@vercel vercel bot commented on 82e842c Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

example-next-13-next-auth – ./examples/example-next-13-next-auth

example-next-13-next-auth.vercel.app
example-next-13-next-auth-git-main-next-intl.vercel.app
example-next-13-next-auth-next-intl.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 82e842c Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

next-intl-example-next-13 – ./examples/example-next-13

next-intl-example-next-13-next-intl.vercel.app
next-intl-example-next-13-git-main-next-intl.vercel.app
next-intl-example-next-13.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 82e842c Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

next-intl-docs – ./docs

next-intl-docs.vercel.app
next-intl-docs-next-intl.vercel.app
next-intl-docs-git-main-next-intl.vercel.app

Please sign in to comment.