// i18n end to end (nowaki.config `i18n`, en default, ja prefixed): locale routing, ctx.locale / // getLocale() (page, island SSR), , hreflang, the default-locale redirect, per-locale // content, the 404 page, sitemap alternates, and static output per locale, on a small app // generated into a temp directory (linked to examples/hello's node_modules). // A second app ("combo", on --port + 1) composes i18n with basePath "/app", trailingSlash // "never" and a parallel-route slot: prefixed URLs, canonical / hreflang / sitemap, the locale // redirect, static file names and _redirects, the slot (with an intercepting route) under a // locale, and config redirects / rewrites / headers matched without the locale prefix. // A third app ("detect", on --port + 2) turns on `i18n.detect`: Accept-Language and the // nowaki_locale cookie on `/`, Vary / Cache-Control, and an ISR root never answering another // language. // Usage: node scripts/i18n-e2e.mjs ... [--port 4990] // Modes: start (`nowaki start`), dev (`nowaki dev`), node (node adapter), cloudflare (the worker's // fetch, called in Node), static (prerender output). NOWAKI_BIN overrides the binary (default // target/debug/nowaki). import { spawn, spawnSync } from "node:child_process"; import { existsSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs"; import http from "node:http"; import os from "node:os"; import path from "node:path"; import { fileURLToPath, pathToFileURL } from "node:url"; const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); const args = process.argv.slice(2); const portArg = args.indexOf("--port"); const PORT = portArg >= 0 ? Number(args[portArg + 1]) : 4990; const modes = args.filter((a, i) => !a.startsWith("--") && (portArg < 0 || i !== portArg + 1)); const BIN = process.env.NOWAKI_BIN ?? path.join(ROOT, "target/debug/nowaki" + (process.platform === "win32" ? ".exe" : "")); const KNOWN = ["start", "dev", "node", "cloudflare", "static"]; if (!modes.length || modes.some((m) => !KNOWN.includes(m))) { console.error(`usage: node scripts/i18n-e2e.mjs <${KNOWN.join("|")}>... [--port 4990]`); process.exit(2); } let failed = 0; const check = (label, cond, detail = "") => { console.log(`${cond ? "ok " : "FAIL"} ${label}${cond || !detail ? "" : ` (${detail})`}`); if (!cond) failed++; }; const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); const FILES = { "package.json": `{ "name": "i18n-e2e", "private": true, "type": "module" }\n`, "nowaki.config.mjs": `export default { site: "https://example.com", i18n: { locales: ["en", "ja"], defaultLocale: "en" }, }; `, "routes/index.tsx": `import { getLocale } from "@nowaki-dev/runtime"; export default function Home() { return
{\`HOME \${getLocale()}\`}
; }\n`, "routes/about.tsx": `import { getLocale } from "@nowaki-dev/runtime"; import { href, Link } from "@nowaki-dev/runtime/navigation"; import Where from "../islands/Where.tsx"; export const loader = (ctx) => ({ locale: ctx.locale, path: ctx.url.pathname }); export default function About({ data }) { return (

{\`ABOUT locale:\${data.locale} get:\${getLocale()} url:\${data.path}\`}

ja keep raw
); }\n`, "islands/Where.tsx": `import { getLocale, Link } from "@nowaki-dev/runtime/navigation"; export default function Where() { return {\`WHERE \${getLocale()}\`}i; }\n`, "routes/blog/[slug].tsx": `import { getCollection, getEntry } from "@nowaki-dev/runtime/content"; export const staticPaths = async () => (await getCollection("blog")).map((e) => ({ slug: e.slug })); export const loader = async (ctx) => ({ entry: await getEntry("blog", ctx.params.slug), count: (await getCollection("blog")).length }); export default function Post({ data }) { return
{data.entry ? \`POST \${data.entry.data.title} n:\${data.count}\` : "POST MISSING"}
; }\n`, "routes/isr.tsx": `import { getLocale } from "@nowaki-dev/runtime"; export const revalidate = 60; export default function Isr() { return
{\`ISR \${getLocale()}\`}
; }\n`, "routes/_404.tsx": `import { getLocale } from "@nowaki-dev/runtime"; export default function NotFound() { return
{\`NOTFOUND \${getLocale()}\`}
; }\n`, "content/config.ts": `import { defineCollection } from "@nowaki-dev/runtime/content"; export const blog = defineCollection({});\n`, "content/blog/en/hello.md": "---\ntitle: Hello\n---\nhi\n", "content/blog/ja/hello.md": "---\ntitle: Konnichiwa\n---\nhi\n", "content/blog/ja/only-ja.md": "---\ntitle: OnlyJa\n---\nhi\n", }; // i18n + basePath + trailingSlash "never" + a parallel-route slot, together. const COMBO = { ...FILES, "nowaki.config.mjs": `export default { site: "https://example.com", basePath: "/app", trailingSlash: "never", i18n: { locales: ["en", "ja"], defaultLocale: "en", detect: true }, redirects: [ { source: "/old-about", destination: "/about" }, { source: "/go/[to]", destination: "/[to]" }, { source: "/ja/legacy", destination: "/ja/about", locale: false }, ], rewrites: [{ source: "/p/[id]", destination: "/photos/[id]" }], headers: [ { source: "/about", headers: { "x-about": "1" } }, { source: "/ja/[...rest]", headers: { "x-ja-raw": "1" }, locale: false }, ], }; `, "routes/about.tsx": FILES["routes/about.tsx"].replace("export const loader", "export const meta = { canonical: true };\nexport const loader"), "routes/photos/_layout.tsx": `export default function Photos({ children, slots }) { return
{children}{slots.modal}
; }\n`, "routes/photos/index.tsx": `export default function List() { return

PHOTOS

; }\n`, "routes/photos/[id].tsx": `import { getLocale } from "@nowaki-dev/runtime"; export const staticPaths = () => [{ id: "1" }]; export default function Photo({ params }) { return

{\`PHOTO \${params.id} \${getLocale()}\`}

; }\n`, "routes/photos/@modal/[id].tsx": `import { getLocale } from "@nowaki-dev/runtime"; export const loader = (ctx) => ({ id: ctx.params.id }); export default function Modal({ data }) { return ; }\n`, "routes/photos/@modal/default.tsx": `export default function NoModal() { return

NOMODAL

; }\n`, "routes/photos/@modal/(.)[id].tsx": `import { getLocale } from "@nowaki-dev/runtime"; export default function Intercepted({ params }) { return ; }\n`, }; // i18n.detect, with an ISR root. const DETECT = { ...FILES, "nowaki.config.mjs": `export default { site: "https://example.com", i18n: { locales: ["en", "ja"], defaultLocale: "en", detect: true }, }; `, "routes/index.tsx": `import { getLocale } from "@nowaki-dev/runtime"; export const revalidate = 60; export default function Home() { return
{\`HOME \${getLocale()}\`}
; }\n`, }; async function detectChecks(label, req, { isr = false } = {}) { const t = (s) => `${label} detect: ${s}`; const varies = (r) => /accept-language/i.test(r.headers.vary ?? "") && /cookie/i.test(r.headers.vary ?? ""); const ja = { "accept-language": "ja-JP,ja;q=0.9,en;q=0.5" }; if (isr) { // Fill the root's ISR entry from an English visitor first. await req("/", { headers: { "accept-language": "en" } }); const hit = await req("/", { headers: { "accept-language": "en-US" } }); check(t("an English visitor gets the cached root"), hit.status === 200 && hit.body.includes("HOME en") && hit.headers["x-nowaki-cache"] === "HIT" && varies(hit), `${hit.status} ${hit.headers["x-nowaki-cache"]} ${hit.headers.vary}`); } let r = await req("/?utm=1", { headers: ja }); check(t("Accept-Language ja on / → 307 /ja with the query"), r.status === 307 && r.headers.location === "/ja?utm=1", `${r.status} ${r.headers.location} ${r.body.slice(0, 120)}`); check(t("the redirect varies and is private"), varies(r) && /private/.test(r.headers["cache-control"] ?? ""), `${r.headers.vary} ${r.headers["cache-control"]}`); check(t("the redirect has the security headers"), r.headers["x-content-type-options"] === "nosniff", JSON.stringify(r.headers)); r = await req("/", { method: "HEAD", headers: ja }); check(t("HEAD / is detected too"), r.status === 307 && r.headers.location === "/ja", `${r.status} ${r.headers.location}`); r = await req("/", { headers: { "accept-language": "en-GB,en;q=0.9,ja;q=0.8" } }); check(t("English stays on / with Vary"), r.status === 200 && r.body.includes("HOME en") && varies(r), `${r.status} ${r.headers.vary}`); r = await req("/"); check(t("no Accept-Language stays on /"), r.status === 200 && r.body.includes("HOME en") && varies(r), `${r.status} ${r.headers.vary}`); r = await req("/", { headers: { ...ja, cookie: "nowaki_locale=en" } }); check(t("the nowaki_locale cookie wins over Accept-Language"), r.status === 200 && r.body.includes("HOME en"), `${r.status} ${r.headers.location}`); r = await req("/", { headers: { "accept-language": "en", cookie: "theme=dark; nowaki_locale=ja" } }); check(t("a cookie naming a locale redirects to it"), r.status === 307 && r.headers.location === "/ja", `${r.status} ${r.headers.location}`); r = await req("/", { headers: { "accept-language": "en", cookie: "nowaki_locale=%2F%2Fevil.com" } }); check(t("an invalid cookie is ignored"), r.status === 200 && r.body.includes("HOME en"), `${r.status} ${r.headers.location}`); r = await req("/", { headers: { "accept-language": "ja;q=0, en" } }); check(t("q=0 is not acceptable"), r.status === 200, `${r.status} ${r.headers.location}`); r = await req("/about", { headers: ja }); check(t("only / is detected"), r.status === 200 && r.body.includes("ABOUT locale:en") && !/accept-language/i.test(r.headers.vary ?? ""), `${r.status} ${r.headers.vary}`); r = await req("/ja", { headers: { "accept-language": "en" } }); check(t("/ja is never redirected"), r.status === 200 && r.body.includes("HOME ja"), `${r.status}`); r = await req("/", { method: "POST", headers: { ...ja, "content-type": "text/plain" } }); check(t("POST / is not detected"), r.headers.location !== "/ja", `${r.status} ${r.headers.location}`); if (isr) { const after = await req("/", { headers: { "accept-language": "ja" } }); check(t("the cached English root never answers a Japanese visitor"), after.status === 307 && !after.body.includes("HOME en"), `${after.status} ${after.headers["x-nowaki-cache"]}`); } } async function comboChecks(label, req) { const t = (s) => `${label} combo: ${s}`; let r = await req("/app/ja/about"); check(t("/app/ja/about renders in ja with the browser URL"), r.status === 200 && /]*/)?.[0]); check( t("hreflang alternates carry the base"), r.body.includes('hreflang="ja" href="https://example.com/app/ja/about"') && r.body.includes('hreflang="en" href="https://example.com/app/about"') && r.body.includes('hreflang="x-default" href="https://example.com/app/about"'), r.body.slice(0, 900), ); check(t(" gets the base after the locale"), r.body.includes('href="/app/ja/about" hreflang="ja"'), r.body.slice(0, 900)); check(t(" and href() keep the locale under the base"), r.body.includes('id="keep" href="/app/ja/blog/hello"') && r.body.includes('id="island-keep" href="/app/ja/blog/hello"') && r.body.includes('id="raw" href="/app/blog/hello"') && r.body.includes('action="/app/ja/about"'), r.body.slice(0, 1200)); r = await req("/app/ja/about/"); check(t("/app/ja/about/ → 308 /app/ja/about (never)"), r.status === 308 && r.headers.location === "/app/ja/about", `${r.status} ${r.headers.location}`); r = await req("/app/en/about?x=1"); check(t("/app/en/about → 308 /app/about with the query"), r.status === 308 && r.headers.location === "/app/about?x=1", `${r.status} ${r.headers.location}`); r = await req("/app/en"); check(t("/app/en → 308 /app (the bare base under never)"), r.status === 308 && r.headers.location === "/app", `${r.status} ${r.headers.location}`); r = await req("/app/en//evil.com"); check(t("/app/en//evil.com is no open redirect"), !(r.status >= 300 && r.status < 400) || !/^\/[/\\]/.test(r.headers.location ?? ""), `${r.status} ${r.headers.location}`); r = await req("/ja/about"); check(t("outside the base is a 404"), r.status === 404, `${r.status}`); r = await req("/app/ja"); check(t("/app/ja is the locale's home"), r.status === 200 && r.body.includes("HOME ja"), `${r.status}`); r = await req("/app", { headers: { "accept-language": "ja" } }); check(t("detection on the bare base → 307 /app/ja"), r.status === 307 && r.headers.location === "/app/ja", `${r.status} ${r.headers.location}`); r = await req("/app/", { headers: { "accept-language": "ja" } }); check(t("/app/ → 308 /app first (never)"), r.status === 308 && r.headers.location === "/app", `${r.status} ${r.headers.location}`); r = await req("/app", { headers: { "accept-language": "en" } }); check(t("English stays on the bare base"), r.status === 200 && r.body.includes("HOME en") && /accept-language/i.test(r.headers.vary ?? ""), `${r.status} ${r.headers.vary}`); r = await req("/app/ja/photos/1"); check(t("slot renders under base + locale"), r.status === 200 && r.body.includes("PHOTO 1 ja") && /