diff options
| author | Jun <[email protected]> | 2026-02-13 20:12:28 +0900 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-02-13 05:12:28 -0600 |
| commit | 9f20e0d14b1d7db2167b2a81523a2521fe1c3b73 (patch) | |
| tree | 2590f67537fe47a3decab7813eb0b21e41ad8150 /packages/web | |
| parent | ebb907d646022d2e7bb8effc164e1f09943d64a9 (diff) | |
| download | opencode-9f20e0d14b1d7db2167b2a81523a2521fe1c3b73.tar.gz opencode-9f20e0d14b1d7db2167b2a81523a2521fe1c3b73.zip | |
fix(web): sync docs locale cookie on alias redirects (#13109)
Diffstat (limited to 'packages/web')
| -rw-r--r-- | packages/web/src/middleware.ts | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/packages/web/src/middleware.ts b/packages/web/src/middleware.ts index 97d085dfb..cf9f97b0b 100644 --- a/packages/web/src/middleware.ts +++ b/packages/web/src/middleware.ts @@ -12,7 +12,28 @@ function docsAlias(pathname: string) { const next = locale === "root" ? `/docs${tail}` : `/docs/${locale}${tail}` if (next === pathname) return null - return next + return { + path: next, + locale, + } +} + +function cookie(locale: string) { + const value = locale === "root" ? "en" : locale + return `oc_locale=${encodeURIComponent(value)}; Path=/; Max-Age=31536000; SameSite=Lax` +} + +function redirect(url: URL, path: string, locale?: string) { + const next = new URL(url.toString()) + next.pathname = path + const headers = new Headers({ + Location: next.toString(), + }) + if (locale) headers.set("Set-Cookie", cookie(locale)) + return new Response(null, { + status: 302, + headers, + }) } function localeFromCookie(header: string | null) { @@ -59,9 +80,7 @@ function localeFromAcceptLanguage(header: string | null) { export const onRequest = defineMiddleware((ctx, next) => { const alias = docsAlias(ctx.url.pathname) if (alias) { - const url = new URL(ctx.request.url) - url.pathname = alias - return ctx.redirect(url.toString(), 302) + return redirect(ctx.url, alias.path, alias.locale) } if (ctx.url.pathname !== "/docs" && ctx.url.pathname !== "/docs/") return next() @@ -71,7 +90,5 @@ export const onRequest = defineMiddleware((ctx, next) => { localeFromAcceptLanguage(ctx.request.headers.get("accept-language")) if (!locale || locale === "root") return next() - const url = new URL(ctx.request.url) - url.pathname = `/docs/${locale}/` - return ctx.redirect(url.toString(), 302) + return redirect(ctx.url, `/docs/${locale}/`) }) |
