Skip to content

Commit 5857dd3

Browse files
Seungjun0906manno23
authored andcommitted
fix(console): follow-up for anomalyco#13108 docs/en routing and locale cookie sync (anomalyco#13608)
1 parent 55355ea commit 5857dd3

4 files changed

Lines changed: 50 additions & 7 deletions

File tree

packages/console/app/src/lib/language.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,26 @@ const DOCS_SEGMENT = new Set([
108108
"zh-tw",
109109
])
110110

111+
const DOCS_LOCALE = {
112+
ar: "ar",
113+
da: "da",
114+
de: "de",
115+
en: "en",
116+
es: "es",
117+
fr: "fr",
118+
it: "it",
119+
ja: "ja",
120+
ko: "ko",
121+
nb: "no",
122+
"pt-br": "br",
123+
root: "en",
124+
ru: "ru",
125+
th: "th",
126+
tr: "tr",
127+
"zh-cn": "zh",
128+
"zh-tw": "zht",
129+
} as const satisfies Record<string, Locale>
130+
111131
function suffix(pathname: string) {
112132
const index = pathname.search(/[?#]/)
113133
if (index === -1) {
@@ -130,7 +150,12 @@ export function docs(locale: Locale, pathname: string) {
130150
return `${next.path}${next.suffix}`
131151
}
132152

133-
if (value === "root") return `${next.path}${next.suffix}`
153+
if (value === "root") {
154+
if (next.path === "/docs/en") return `/docs${next.suffix}`
155+
if (next.path === "/docs/en/") return `/docs/${next.suffix}`
156+
if (next.path.startsWith("/docs/en/")) return `/docs/${next.path.slice("/docs/en/".length)}${next.suffix}`
157+
return `${next.path}${next.suffix}`
158+
}
134159

135160
if (next.path === "/docs") return `/docs/${value}${next.suffix}`
136161
if (next.path === "/docs/") return `/docs/${value}/${next.suffix}`
@@ -154,6 +179,15 @@ export function fromPathname(pathname: string) {
154179
return parseLocale(fix(pathname).split("/")[1])
155180
}
156181

182+
export function fromDocsPathname(pathname: string) {
183+
const next = fix(pathname)
184+
const value = next.split("/")[2]?.toLowerCase()
185+
if (!value) return null
186+
if (!next.startsWith("/docs/")) return null
187+
if (!(value in DOCS_LOCALE)) return null
188+
return DOCS_LOCALE[value as keyof typeof DOCS_LOCALE]
189+
}
190+
157191
export function strip(pathname: string) {
158192
const locale = fromPathname(pathname)
159193
if (!locale) return fix(pathname)
@@ -272,6 +306,9 @@ export function localeFromRequest(request: Request) {
272306
const fromPath = fromPathname(new URL(request.url).pathname)
273307
if (fromPath) return fromPath
274308

309+
const fromDocsPath = fromDocsPathname(new URL(request.url).pathname)
310+
if (fromDocsPath) return fromDocsPath
311+
275312
return (
276313
localeFromCookieHeader(request.headers.get("cookie")) ??
277314
detectFromAcceptLanguage(request.headers.get("accept-language"))

packages/console/app/src/routes/docs/[...path].ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { APIEvent } from "@solidjs/start/server"
22
import { Resource } from "@opencode-ai/console-resource"
3-
import { docs, localeFromRequest, tag } from "~/lib/language"
3+
import { cookie, docs, localeFromRequest, tag } from "~/lib/language"
44

55
async function handler(evt: APIEvent) {
66
const req = evt.request.clone()
@@ -17,7 +17,9 @@ async function handler(evt: APIEvent) {
1717
headers,
1818
body: req.body,
1919
})
20-
return response
20+
const next = new Response(response.body, response)
21+
next.headers.append("set-cookie", cookie(locale))
22+
return next
2123
}
2224

2325
export const GET = handler

packages/console/app/src/routes/docs/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { APIEvent } from "@solidjs/start/server"
22
import { Resource } from "@opencode-ai/console-resource"
3-
import { docs, localeFromRequest, tag } from "~/lib/language"
3+
import { cookie, docs, localeFromRequest, tag } from "~/lib/language"
44

55
async function handler(evt: APIEvent) {
66
const req = evt.request.clone()
@@ -17,7 +17,9 @@ async function handler(evt: APIEvent) {
1717
headers,
1818
body: req.body,
1919
})
20-
return response
20+
const next = new Response(response.body, response)
21+
next.headers.append("set-cookie", cookie(locale))
22+
return next
2123
}
2224

2325
export const GET = handler

packages/console/app/src/routes/s/[id].ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { APIEvent } from "@solidjs/start/server"
22
import { Resource } from "@opencode-ai/console-resource"
3-
import { docs, localeFromRequest, tag } from "~/lib/language"
3+
import { cookie, docs, localeFromRequest, tag } from "~/lib/language"
44

55
async function handler(evt: APIEvent) {
66
const req = evt.request.clone()
@@ -17,7 +17,9 @@ async function handler(evt: APIEvent) {
1717
headers,
1818
body: req.body,
1919
})
20-
return response
20+
const next = new Response(response.body, response)
21+
next.headers.append("set-cookie", cookie(locale))
22+
return next
2123
}
2224

2325
export const GET = handler

0 commit comments

Comments
 (0)