GeezerVison is showing github.com — fix(data): canonicalize locale pages · gateway/opencode@05ce6bc
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/console/app/src/lib/stats-proxy.ts +68-1Lines changed: 68 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import type { APIEvent } from "@solidjs/start/server" | 2 | 2 | import { Resource } from "@opencode-ai/console-resource" | | 3 | +import { LOCALE_HEADER, cookie, localeFromRequest, route, tag } from "~/lib/language" | 3 | 4 |
| 4 | 5 | const dataPath = "/data" | 5 | 6 |
| 6 | 7 | export async function statsProxy(evt: APIEvent) { | 7 | 8 | const req = evt.request.clone() | | 9 | + const locale = localeFromRequest(req) | | 10 | + const redirect = redirectToLocalizedData(req, new URL(req.url), locale) | | 11 | + if (redirect) return redirect | | 12 | + | 8 | 13 | const targetUrl = new URL(req.url) | 9 | 14 | targetUrl.protocol = "https:" | 10 | 15 | targetUrl.hostname = Resource.App.stage === "production" ? "stats.opencode.ai" : "stats.dev.opencode.ai" | @@ -18,9 +23,13 @@ export async function statsProxy(evt: APIEvent) { | 18 | 23 | targetUrl.pathname = targetUrl.pathname.slice(dataPath.length) | 19 | 24 | } | 20 | 25 |
| | 26 | + const requestHeaders = new Headers(req.headers) | | 27 | + requestHeaders.set(LOCALE_HEADER, locale) | | 28 | + requestHeaders.set("accept-language", tag(locale)) | | 29 | + | 21 | 30 | const response = await fetch(targetUrl, { | 22 | 31 | method: req.method, | 23 | | - headers: req.headers, | | 32 | + headers: requestHeaders, | 24 | 33 | body: req.body, | 25 | 34 | }) | 26 | 35 |
| @@ -30,6 +39,7 @@ export async function statsProxy(evt: APIEvent) { | 30 | 39 | headers.delete("content-encoding") | 31 | 40 | headers.delete("content-length") | 32 | 41 | headers.delete("etag") | | 42 | + appendVary(headers, "Accept-Language", "Cookie") | 33 | 43 |
| 34 | 44 | return new Response(rewriteStatsHtml(await response.text()), { | 35 | 45 | status: response.status, | @@ -52,3 +62,60 @@ export function statsRedirect(evt: APIEvent) { | 52 | 62 | function rewriteStatsHtml(html: string) { | 53 | 63 | return html.replaceAll('"/_build/', `"${dataPath}/_build/`).replaceAll("'/_build/", `'${dataPath}/_build/`) | 54 | 64 | } | | 65 | + | | 66 | +function redirectToLocalizedData(request: Request, url: URL, locale: ReturnType<typeof localeFromRequest>) { | | 67 | + if (locale === "en") return null | | 68 | + if (request.headers.get(LOCALE_HEADER)) return null | | 69 | + if (request.method !== "GET" && request.method !== "HEAD") return null | | 70 | + if (!acceptsHtml(request)) return null | | 71 | + if (!url.pathname.startsWith(`${dataPath}/`) && url.pathname !== dataPath) return null | | 72 | + if (isDataBypassPath(url.pathname)) return null | | 73 | + | | 74 | + const next = new URL(url) | | 75 | + next.pathname = route(locale, url.pathname) | | 76 | + | | 77 | + const headers = new Headers({ | | 78 | + Location: next.toString(), | | 79 | + }) | | 80 | + headers.append("set-cookie", cookie(locale)) | | 81 | + appendVary(headers, "Accept-Language", "Cookie") | | 82 | + | | 83 | + return new Response(null, { | | 84 | + status: 308, | | 85 | + headers, | | 86 | + }) | | 87 | +} | | 88 | + | | 89 | +function acceptsHtml(request: Request) { | | 90 | + const accept = request.headers.get("accept") | | 91 | + return !accept || accept.includes("text/html") || accept.includes("*/*") | | 92 | +} | | 93 | + | | 94 | +function isDataBypassPath(pathname: string) { | | 95 | + return ( | | 96 | + pathname.startsWith(`${dataPath}/_build/`) || | | 97 | + pathname.startsWith(`${dataPath}/api/`) || | | 98 | + pathname.startsWith(`${dataPath}/_server`) || | | 99 | + pathname === `${dataPath}/banner.jpg` || | | 100 | + pathname === `${dataPath}/banner.png` | | 101 | + ) | | 102 | +} | | 103 | + | | 104 | +function appendVary(headers: Headers, ...values: string[]) { | | 105 | + const existing = headers | | 106 | + .get("vary") | | 107 | + ?.split(",") | | 108 | + .map((value) => value.trim()) | | 109 | + .filter(Boolean) | | 110 | + | | 111 | + headers.set( | | 112 | + "vary", | | 113 | + values | | 114 | + .reduce( | | 115 | + (result, value) => | | 116 | + result.some((item) => item.toLowerCase() === value.toLowerCase()) ? result : [...result, value], | | 117 | + existing ?? [], | | 118 | + ) | | 119 | + .join(", "), | | 120 | + ) | | 121 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/stats/app/src/routes/[lab]/[model].tsx +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,6 +31,7 @@ import { | 31 | 31 | type ModelCatalogEntry, | 32 | 32 | } from "../model-catalog" | 33 | 33 | import { runStatsEffect } from "../../stats-runtime" | | 34 | +import { setStatsPageCacheHeaders } from "../stats-cache" | 34 | 35 | import { | 35 | 36 | applyThemePreference, | 36 | 37 | Footer, | @@ -86,7 +87,7 @@ export default function StatsModel() { | 86 | 87 | const i18n = useI18n() | 87 | 88 | const language = useLanguage() | 88 | 89 | const event = getRequestEvent() | 89 | | - event?.response.headers.set("Cache-Control", "public, max-age=60, s-maxage=300, stale-while-revalidate=86400") | | 90 | + setStatsPageCacheHeaders(event?.response.headers) | 90 | 91 | const params = useParams() | 91 | 92 | const labParam = createMemo(() => params.lab ?? "") | 92 | 93 | const modelParam = createMemo(() => params.model ?? "") | |
Collapse file Copy file name to clipboardExpand all lines: packages/stats/app/src/routes/[lab]/index.tsx +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,6 +21,7 @@ import { | 21 | 21 | type ModelCatalogLab, | 22 | 22 | } from "../model-catalog" | 23 | 23 | import { runStatsEffect } from "../../stats-runtime" | | 24 | +import { setStatsPageCacheHeaders } from "../stats-cache" | 24 | 25 | import { | 25 | 26 | applyThemePreference, | 26 | 27 | Footer, | @@ -43,7 +44,7 @@ export default function StatsLab() { | 43 | 44 | const i18n = useI18n() | 44 | 45 | const language = useLanguage() | 45 | 46 | const event = getRequestEvent() | 46 | | - event?.response.headers.set("Cache-Control", "public, max-age=60, s-maxage=300, stale-while-revalidate=86400") | | 47 | + setStatsPageCacheHeaders(event?.response.headers) | 47 | 48 | const params = useParams() | 48 | 49 | const labParam = createMemo(() => params.lab ?? "") | 49 | 50 | const catalog = createAsync(() => getModelCatalog()) | |
Collapse file Copy file name to clipboardExpand all lines: packages/stats/app/src/routes/index.tsx +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,6 +32,7 @@ import { useI18n } from "../context/i18n" | 32 | 32 | import { useLanguage } from "../context/language" | 33 | 33 | import { localizedUrl } from "../lib/language" | 34 | 34 | import { findModelCatalogEntry, getModelCatalog, type ModelCatalog } from "./model-catalog" | | 35 | +import { setStatsPageCacheHeaders } from "./stats-cache" | 35 | 36 | import { | 36 | 37 | applyThemePreference, | 37 | 38 | Footer, | @@ -121,7 +122,7 @@ export default function StatsHome() { | 121 | 122 | const i18n = useI18n() | 122 | 123 | const language = useLanguage() | 123 | 124 | const event = getRequestEvent() | 124 | | - event?.response.headers.set("Cache-Control", "public, max-age=60, s-maxage=300, stale-while-revalidate=86400") | | 125 | + setStatsPageCacheHeaders(event?.response.headers) | 125 | 126 | const statsHomeUrl = localizedUrl(language.locale(), "/data/") | 126 | 127 | const statsUnfurlUrl = new URL(statsUnfurlPath, localizedUrl("en", "/data/")).toString() | 127 | 128 | const data = createAsync(() => getData()) | |
Collapse file Copy file name to clipboard +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +const statsPageCacheControl = "public, max-age=60, s-maxage=300, stale-while-revalidate=86400" | | 2 | + | | 3 | +export function setStatsPageCacheHeaders(headers: Headers | undefined) { | | 4 | + if (!headers) return | | 5 | + | | 6 | + headers.set("Cache-Control", statsPageCacheControl) | | 7 | + appendVary(headers, "Accept-Language", "Cookie") | | 8 | +} | | 9 | + | | 10 | +function appendVary(headers: Headers, ...values: string[]) { | | 11 | + const existing = headers | | 12 | + .get("vary") | | 13 | + ?.split(",") | | 14 | + .map((value) => value.trim()) | | 15 | + .filter(Boolean) | | 16 | + | | 17 | + headers.set( | | 18 | + "vary", | | 19 | + values | | 20 | + .reduce( | | 21 | + (result, value) => | | 22 | + result.some((item) => item.toLowerCase() === value.toLowerCase()) ? result : [...result, value], | | 23 | + existing ?? [], | | 24 | + ) | | 25 | + .join(", "), | | 26 | + ) | | 27 | +} |
|