Skip to content

Commit d0b6127

Browse files
Copilothuangyiirene
andcommitted
refactor: improve cookie handling and documentation
- Add setLocaleCookie helper function with consistent cookie options - Set sameSite=lax and path=/ for security and consistency - Update comment for SUPPORTED_LANGUAGES constant - Ensure all cookie operations use the same configuration Co-authored-by: huangyiirene <7665279+huangyiirene@users.noreply.github.com>
1 parent 427d33e commit d0b6127

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

apps/docs/middleware.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@ import { i18n } from '@/lib/i18n';
55
const LOCALE_COOKIE = 'FD_LOCALE';
66

77
/**
8-
* Helper to check if a language is supported
8+
* Supported languages extracted from i18n configuration
99
*/
1010
const SUPPORTED_LANGUAGES = i18n.languages as readonly string[];
1111

12+
/**
13+
* Set locale cookie with consistent options
14+
*/
15+
function setLocaleCookie(response: NextResponse, locale: string): void {
16+
response.cookies.set(LOCALE_COOKIE, locale, {
17+
sameSite: 'lax',
18+
path: '/',
19+
});
20+
}
21+
1222
/**
1323
* Language code mapping
1424
* Maps browser language codes to our supported language codes
@@ -93,7 +103,7 @@ export default function middleware(request: NextRequest) {
93103
const url = new URL(request.url);
94104
url.pathname = pathname.replace(`/${i18n.defaultLanguage}`, '') || '/';
95105
const response = NextResponse.redirect(url);
96-
response.cookies.set(LOCALE_COOKIE, locale);
106+
setLocaleCookie(response, locale);
97107
return response;
98108
}
99109

@@ -114,7 +124,7 @@ export default function middleware(request: NextRequest) {
114124
const url = new URL(request.url);
115125
url.pathname = `/${preferredLanguage}${pathname}`;
116126
const response = NextResponse.redirect(url);
117-
response.cookies.set(LOCALE_COOKIE, preferredLanguage);
127+
setLocaleCookie(response, preferredLanguage);
118128
return response;
119129
}
120130

0 commit comments

Comments
 (0)