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

fix: Handle case where the locale param is an array and set cookie expiration to one year #435

Merged
merged 4 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading