GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(data): canonicalize locale pages · gateway/opencode@05ce6bc

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages
    • console/app/src/lib
      • stats-proxy.ts
    • stats/app/src/routes
      • [lab]
        • [model].tsx
        • index.tsx
      • index.tsx
      • stats-cache.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/console/app/src/lib/stats-proxy.ts‎

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 numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import type { APIEvent } from "@solidjs/start/server"
22
import { Resource } from "@opencode-ai/console-resource"
3+
import { LOCALE_HEADER, cookie, localeFromRequest, route, tag } from "~/lib/language"
34

45
const dataPath = "/data"
56

67
export async function statsProxy(evt: APIEvent) {
78
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+
813
const targetUrl = new URL(req.url)
914
targetUrl.protocol = "https:"
1015
targetUrl.hostname = Resource.App.stage === "production" ? "stats.opencode.ai" : "stats.dev.opencode.ai"
@@ -18,9 +23,13 @@ export async function statsProxy(evt: APIEvent) {
1823
targetUrl.pathname = targetUrl.pathname.slice(dataPath.length)
1924
}
2025

26+
const requestHeaders = new Headers(req.headers)
27+
requestHeaders.set(LOCALE_HEADER, locale)
28+
requestHeaders.set("accept-language", tag(locale))
29+
2130
const response = await fetch(targetUrl, {
2231
method: req.method,
23-
headers: req.headers,
32+
headers: requestHeaders,
2433
body: req.body,
2534
})
2635

@@ -30,6 +39,7 @@ export async function statsProxy(evt: APIEvent) {
3039
headers.delete("content-encoding")
3140
headers.delete("content-length")
3241
headers.delete("etag")
42+
appendVary(headers, "Accept-Language", "Cookie")
3343

3444
return new Response(rewriteStatsHtml(await response.text()), {
3545
status: response.status,
@@ -52,3 +62,60 @@ export function statsRedirect(evt: APIEvent) {
5262
function rewriteStatsHtml(html: string) {
5363
return html.replaceAll('"/_build/', `"${dataPath}/_build/`).replaceAll("'/_build/", `'${dataPath}/_build/`)
5464
}
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

‎packages/stats/app/src/routes/[lab]/[model].tsx‎

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 numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
type ModelCatalogEntry,
3232
} from "../model-catalog"
3333
import { runStatsEffect } from "../../stats-runtime"
34+
import { setStatsPageCacheHeaders } from "../stats-cache"
3435
import {
3536
applyThemePreference,
3637
Footer,
@@ -86,7 +87,7 @@ export default function StatsModel() {
8687
const i18n = useI18n()
8788
const language = useLanguage()
8889
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)
9091
const params = useParams()
9192
const labParam = createMemo(() => params.lab ?? "")
9293
const modelParam = createMemo(() => params.model ?? "")
Collapse file

‎packages/stats/app/src/routes/[lab]/index.tsx‎

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 numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
type ModelCatalogLab,
2222
} from "../model-catalog"
2323
import { runStatsEffect } from "../../stats-runtime"
24+
import { setStatsPageCacheHeaders } from "../stats-cache"
2425
import {
2526
applyThemePreference,
2627
Footer,
@@ -43,7 +44,7 @@ export default function StatsLab() {
4344
const i18n = useI18n()
4445
const language = useLanguage()
4546
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)
4748
const params = useParams()
4849
const labParam = createMemo(() => params.lab ?? "")
4950
const catalog = createAsync(() => getModelCatalog())
Collapse file

‎packages/stats/app/src/routes/index.tsx‎

Copy file name to clipboardExpand all lines: packages/stats/app/src/routes/index.tsx
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { useI18n } from "../context/i18n"
3232
import { useLanguage } from "../context/language"
3333
import { localizedUrl } from "../lib/language"
3434
import { findModelCatalogEntry, getModelCatalog, type ModelCatalog } from "./model-catalog"
35+
import { setStatsPageCacheHeaders } from "./stats-cache"
3536
import {
3637
applyThemePreference,
3738
Footer,
@@ -121,7 +122,7 @@ export default function StatsHome() {
121122
const i18n = useI18n()
122123
const language = useLanguage()
123124
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)
125126
const statsHomeUrl = localizedUrl(language.locale(), "/data/")
126127
const statsUnfurlUrl = new URL(statsUnfurlPath, localizedUrl("en", "/data/")).toString()
127128
const data = createAsync(() => getData())
Collapse file

‎packages/stats/app/src/routes/stats-cache.ts‎

Copy file name to clipboard
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.