@@ -77,13 +77,36 @@ function localeFromAcceptLanguage(header: string | null) {
7777 return locale ?? "root"
7878}
7979
80- export const onRequest = defineMiddleware ( ( ctx , next ) => {
80+ export const onRequest = defineMiddleware ( async ( ctx , next ) => {
8181 const alias = docsAlias ( ctx . url . pathname )
8282 if ( alias ) {
8383 return redirect ( ctx . url , alias . path , alias . locale )
8484 }
8585
86- if ( ctx . url . pathname !== "/docs" && ctx . url . pathname !== "/docs/" ) return next ( )
86+ if ( ctx . url . pathname !== "/docs" && ctx . url . pathname !== "/docs/" ) {
87+ const response = await next ( )
88+
89+ // Detect the locale from the URL path and persist it in the oc_locale cookie so
90+ // that the user's language-switcher choice is remembered across navigations.
91+ //
92+ // Previously the cookie was only written during alias-redirect (e.g. /docs/en/ →
93+ // /docs/). Canonical locale paths like /docs/tr/page and English paths like
94+ // /docs/page never wrote the cookie, so returning to /docs/ would re-apply the
95+ // browser's Accept-Language header and undo the user's manual selection.
96+ const hit = / ^ \/ d o c s \/ ( [ ^ / ] + ) (?: \/ | $ ) / . exec ( ctx . url . pathname )
97+ const urlLocale = ( hit ? exactLocale ( hit [ 1 ] ?? "" ) : null ) ?? "root"
98+ const cookieLocale = localeFromCookie ( ctx . request . headers . get ( "cookie" ) )
99+ if ( cookieLocale !== urlLocale ) {
100+ const headers = new Headers ( response . headers )
101+ headers . append ( "Set-Cookie" , cookie ( urlLocale ) )
102+ return new Response ( response . body , {
103+ status : response . status ,
104+ statusText : response . statusText ,
105+ headers,
106+ } )
107+ }
108+ return response
109+ }
87110
88111 const locale =
89112 localeFromCookie ( ctx . request . headers . get ( "cookie" ) ) ??
0 commit comments