Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "project 1A",
"image": "oven/bun:1",
"customizations": {
"vscode": {
"extensions": [
"Oven.bun-vscode"
]
}
},
"postCreateCommand": "bun install",
}

36 changes: 0 additions & 36 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/web/src/components/share/part.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export function Part(props: PartProps) {
<div data-component="assistant-text-markdown">
<ContentMarkdown expand={props.last} text={props.part.text} />
</div>
{props.last && props.message.role === "assistant" && props.message.time.completed && (
{props.last && props.message.time.completed && (
<Footer
title={DateTime.fromMillis(props.message.time.completed)
.setLocale(normalizeLocale(messages.locale))
Expand Down
48 changes: 48 additions & 0 deletions packages/web/src/middleware.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { describe, expect, mock, test } from "bun:test"

mock.module("astro:middleware", () => ({
defineMiddleware: (middleware: unknown) => middleware,
}))

const { onRequest } = await import("./middleware")
const origin = "https://example.com"
const noop = () => "next"

function context(path: string, headers: Record<string, string> = {}) {
const url = new URL(origin + path)
return { url, request: new Request(url, { headers }) }
}

async function expectRedirect(path: string, expectedPath: string, headers: Record<string, string> = {}) {
const res = await onRequest(context(path, headers), noop)

expect(res).toBeInstanceOf(Response)
expect(res.status).toBe(302)
expect(res.headers.get("location")).toBe(origin + expectedPath)

return res
}

describe("web middleware", () => {
test("leaves non-docs routes alone", async () => {
expect(await onRequest(context("/about"), noop)).toBe("next")
})

test("redirects a locale-prefixed docs url to canonical", async () => {
const res = await expectRedirect("/docs/en/getting-started", "/docs/getting-started")
expect(res.headers.get("set-cookie")).toContain("oc_locale=en")
})

test("docs root: locale cookie wins over accept-language", async () => {
await expectRedirect("/docs", "/docs/de/", {
cookie: "oc_locale=de",
"accept-language": "fr-FR",
})
})

test("falls back to accept-language, highest q first", async () => {
await expectRedirect("/docs/", "/docs/fr/", {
"accept-language": "es;q=0.4, fr-FR;q=0.9, en;q=0.8",
})
})
})
25 changes: 9 additions & 16 deletions packages/web/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,15 @@ function localeFromCookie(header: string | null) {
function localeFromAcceptLanguage(header: string | null) {
if (!header) return "root"

const items = header
.split(",")
.map((raw) => raw.trim())
.filter(Boolean)
.map((raw) => {
const parts = raw.split(";").map((x) => x.trim())
const lang = parts[0] ?? ""
const q = parts
.slice(1)
.find((x) => x.startsWith("q="))
?.slice(2)
return {
lang,
q: q ? Number.parseFloat(q) : 1,
}
})
const items = header.split(",").map((raw) => {
const [language = "", ...parameters] = raw.split(";")
const quality = parameters.find((parameter) => parameter.trim().startsWith("q="))
const value = quality?.trim().slice(2)
return {
lang: language.trim(),
q: value ? Number.parseFloat(value) : 1,
}
})
.sort((a, b) => b.q - a.q)

const locale = items
Expand Down
Loading