Repository navigation
fix(pages): keep the locale on the error page after a thrown render() - #113
Conversation
`extractLocale` in `+onBeforeRoute` was the only place that read a locale off a path, and it was unreachable from anywhere else — a hook's private function. The error page needs the same answer from a different hook, and the repo convention is that how a URL is spelled lives in `lib/urls.ts`, not in a consumer. `localeFromPath` carries the whole rule, including the one that makes `/status/page` a path rather than locale `st`. It adds `prefixed`, which is what tells `/about` from `/en/about`: both are English on `/about`, and only the second one 301s. `+onBeforeRoute` keeps the redirect and `urlLogical`, so its behaviour is unchanged. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LwiQ9UQALv96Atvy9okdsS
A thrown `render(<status>)` forks the PRE-routing pageContext and calls the render directly, so `+onBeforeRoute` never runs for that render and `pageContext.locale` is missing. Confirmed in `[email protected]`: `renderPageServer.js:185` forks `pageContextBegin`, then calls `renderPageServerAfterRoute` without re-routing. A no-route-matched 404 renders from the already-routed context and was never affected. Four consumers read the gap: `<html lang>` fell back to vike-react's default `en`, `<html dir>` to `ltr`, and the `wm-web-translations` read dropped its `locale` — which then cached in KV under the bare key every locale shares. `+onCreatePageContext` is the seam: Vike runs it from `loadPageConfigsLazyServerSide.js:28`, which the error fork reaches too, and before `data()` and `+onBeforeRender`. It writes `locale` only when absent, so `+onBeforeRoute` stays the single writer wherever routing ran. `.server.ts`, not `.ts`: the hook's env is server AND client, but Vike re-runs `+onBeforeRoute` in the browser on hydration and on every client-side navigation ("needed for i18n URL locale extraction", `renderPageClient.js:112`), so a client copy would be a second writer with nothing to fix. `lib/head.tsx` drops its `?? 'en'`, which existed only for this bug. Verified on `pnpm dev`, before and after, against the same running server: /fr/not-a-real-slug before <html dir="ltr" lang="en"> after <html dir="ltr" lang="fr"> /fa/not-a-real-slug before <html dir="ltr" lang="en"> after <html dir="rtl" lang="fa"> /zz/a/b/c after <html dir="ltr" lang="en"> (never broken) The request log shows the same: without the hook, the error render's read is `wm-web-translations?depth=0&draft=false` with no `locale`, and `[loadTranslations] "undefined" returned no strings` follows. With it, every read carries one and that line never appears. The `/fa` error render then served the `/fr` render's strings from the shared locale-less cache entry, which is the KV consequence in one observation. Closes #110 Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LwiQ9UQALv96Atvy9okdsS
`extractLocale` was a wrapper around one call once the regex moved, so it folds back into `onBeforeRoute`. Three comment blocks that restated each other lose their duplicate halves. The smoke helpers take the rest. `discoverFromCms` costs three sequential CMS round trips and was called five times a run, so it memoises its promise. `nonEnglishLocale` and `htmlTag` move beside it, out of the one spec file that happened to need them first. The direction assertion asks `localeDirection` — the function `+htmlAttributes` calls — instead of restating the RTL locale list, so it catches the error page dropping that call rather than agreeing with a second copy of the data. `lib/urls.test.ts` owns the derivation's cases; the hook spec keeps only what the hook adds. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LwiQ9UQALv96Atvy9okdsS
`loadCmsSamples` never rejects. A transient CMS timeout returns `availableLocales: ['en']`, which is also the site's real value today, so a cached result cannot be told from a flake. Caching one would skip every later locale spec in the run and report green having tested nothing. Reverts the memo added in the previous commit. `nonEnglishLocale` and `htmlTag` stay — they remove real duplication and cost nothing. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LwiQ9UQALv96Atvy9okdsS
The `lib/urls.ts` bullet listed only the two inverse functions, so a contributor deriving a locale from a path had nothing pointing them at `localeFromPath`. And nothing said why `+onCreatePageContext` exists, which is the rule a consumer needs before it reaches for a `?? 'en'` guard. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LwiQ9UQALv96Atvy9okdsS
Deploying wemeditate-design with
|
| Latest commit: |
a0ec3af
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://9c618720.wm-design.pages.dev |
| Branch Preview URL: | https://claude-fix-110-error-page-lo.wm-design.pages.dev |
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
wemeditate-web | a0ec3af | Commit Preview URL Branch Preview URL |
Sep 18 2026, 12:58 AM |
|
🤖 no session running recent dispatchesWritten by the sydevs dispatcher. Sessions never edit this comment. |
sydevs-bot
left a comment
There was a problem hiding this comment.
🧐 Adversarial review
Every file maps to a step in #110, and nothing is larger than the ticket asked for. "Exactly one function derives a locale from a URL path" holds. No cross-repo contract moves.
The shape is the open question: two writers of pageContext.locale plus a precedence rule, kept only because this environment could not verify the simpler version.
Findings
should-fix— Two writers, or one?+onCreatePageContext.server.ts:24consider— An unoffered RTL locale renders English right-to-left.+onCreatePageContext.server.ts:30
What was checked
- Read whole: the four
pages/hooks,lib/urls.ts,types/vike.d.ts, both new specs, plusserver/site-context.ts:63-90and:145. - The
+onBeforeRouterefactor preserves behaviour: same regex, same/indexmapping, same 301 condition. - Grepped every locale-shaped regex and
isLocalecaller: the others exclude a prefix or read a query param. - Unchecked: Vike's hook order — no
node_moduleshere, sorenderPageServerAfterRoute.js:32is the PR's reading.
🤖 Written by the sydevs autonomous loop — see what this is
Generated by Claude Code
|
Both critic threads answered. No commit — the evidence went the other way on each.
Both left open for the reviewer to settle. The PR body carries both answers. Lean gate re-run on 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
The only conflict was the file header of `types/vike.d.ts`. Both sides rewrote the same line: #107 dropped "Cloudflare Workers bindings" with the KV binding it described, and this branch replaced "hooks such as onBeforeRoute" because a second hook now writes `locale`. The resolution keeps both edits. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01TW8NJxcfXF2WkwXNQfV1cE
|
Merged Only the No code file overlapped, so the rest auto-merged. Lean gate on the merge: lint 0 errors, 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
Cloudflare Pages is red on 797d9c9, and the build log lives only in the Cloudflare dashboard, which a routine cannot reach. The branch forked at 2af1870, three commits back, so the Pages build was still installing the pre-#111 dependency tree: 88 extra packages and a whole unused Next.js subtree. This merge builds against the tree main installs today. It also brings in the two server changes this PR's memos sit on top of: #107 drops `preview` from `getWebConfig`, which `loadSiteContext` calls, and #113 adds `+onCreatePageContext.server.ts`, the error-page path this PR's fork spec describes. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01B9MPJ8NWXhK85k27xRq6Ji
Summary
/fa/…now renders<html lang="fa" dir="rtl">instead of English left-to-right.wm-web-translationsread carries its locale again, so it stops caching in KV under one key every locale shares.localeFromPathinlib/urls.ts, derives a locale from a path. Two hooks read it.Changes
lib/urls.ts—localeFromPath(pathname), moved out of+onBeforeRoute's private helper.pages/+onCreatePageContext.server.ts— new. Fillslocaleon the error fork, which never routes.pages/+onBeforeRoute.ts— calls the helper. Behaviour unchanged.lib/head.tsx— drops?? 'en', which existed only for this bug.AGENTS.md,types/vike.d.ts,pages/+config.ts— name both writers; correct two stale comments.Preview
Preview pending — the Cloudflare Workers alias is published by the build, not derived from the branch name.
The routes to open once it is up:
/<locale>/__does_not_exist__for any offered non-English locale (expectlang="<locale>"), and/zz/a/b/c(expectlang="en", the flavour that was never broken).Test results
Lean gate: ✓ lint (0 errors),
tsc --noEmit,vitest run— 651 tests, 70 filesTargeted specs:
lib/urls.test.ts,pages/onCreatePageContext.test.ts,pages/onBeforeRoute.test.tsBuild: ✓
pnpm build. Confirmed the hook lands indist/serveronly.Live, on
pnpm dev, the same server before and after the hook:/fr/not-a-real-slug<html dir="ltr" lang="en"><html dir="ltr" lang="fr">/fa/not-a-real-slug<html dir="ltr" lang="en"><html dir="rtl" lang="fa">/zz/a/b/clang="en"lang="en"(never broken)/en/about?x=1/about?x=1/about?x=1The request log agrees. Without the hook the error render reads
wm-web-translations?depth=0&draft=falsewith nolocale, and[loadTranslations] "undefined" returned no stringsfollows. With it, every read carries one and that line never appears.Manual verification
curl -s <preview>/<locale>/__does_not_exist__ | grep -o '<html[^>]*>'for an offered non-English locale. Expectlang="<locale>".fa, the same URL should carrydir="rtl". The local dev server showed this; no CI environment offersfatoday, so the smoke spec asserts whateverlocaleDirectionsays for the locale it discovers.lang="de"(say) for a locale prefix the site does not offer, over English copy, becauseserver/site-context.ts:145404s before the locale set is known to the error fork. For an unoffered RTL prefix that also flipsdirtortl, so the English snapshot renders right-to-left where it used to renderltr. Only the direction changes — the copy is English either way. See Notes.Notes for reviewer
Two open questions from the ticket, both answered.
Which env does a plain
+onCreatePageContext.tsget? Server and client (metaBuiltIn.js:24). It is.server.tshere: Vike re-runs+onBeforeRoutein the browser on hydration and on every client-side navigation — its own comment atrenderPageClient.js:112says "needed for i18n URL locale extraction" — so the client already has the locale and a second writer there would fix nothing. This also corrects thepassToClientcomment, which credited vike-react.Confirmed against installed
node_modulesin the review run:route()atrenderPageClient.js:116, then this hook at:167→:649, thendata(). The order holds on hydration and on client navigation alike, and matches the server (renderPageServerAfterRoute.js:32,:41).Should an unoffered locale's error page render in that locale? It does now, by the same one rule as every other path. The alternative needs the error fork to know
availableLocales, which costs it a CMS read on the one path that must not depend on one. Flagged above for verification rather than decided silently.Dismissed findings.
+onCreatePageContextthe sole unconditional writer and drop.server. Genuinely simpler on paper — one writer, no precedence rule. Feasible too: the hook order above puts it afterroute()on every path, client included. Not taken:+onBeforeRoutestill needslocaleFromPathforurlLogicaland the 301, so dropping the assignment buys a two-line guard at the price of a second derivation on every request — and it moves wherelocalecomes from on every request, which this environment cannot verify without a CMS key. The two-writer shape leaves the nominal path byte-identical tomain, which is what made the before/after above possible. Worth revisiting with CMS access.new URL(urlOriginal, 'http://localhost')is heavier than asplit. Kept: Vike's own runtime parsesurlOriginalexactly this way (renderPageServer.js:270) because it may be a path or an absolute URL depending on the adapter. The cost falls on error renders only.discoverFromCms. Applied, then reverted after review — see the fourth commit.loadCmsSamplesnever rejects, and a degraded result is indistinguishable from the site's real value, so one timeout would have skipped every later locale spec and reported green.pages/[slug]/+route.ts,lib/cms-routes.ts,lib/atlas-route.ts. Those three recognise a locale-shaped prefix in order to exclude or tolerate it. None callsisLocaleor produces aLocale, so "exactly one function derives a locale from a URL path" holds.Filed on the way, not fixed here:
/en301s to/index, a 200 duplicate of/. Pre-existing, and documented as expected in the smoke spec, so it wants its own decision.Link.tsxandEmbedButton.tsxeach restate the locale-prefix rule, andLinkhas already drifted:href="/"yields/fr/wherelocaleUrlyields/fr.Why `+onCreatePageContext` is the seam
Read from
[email protected], the versionpnpm-lock.yamlpins.localeroute()runs+onBeforeRouteand merges its result. Present._errorfrom that already-routed context. Present.render(<status>)renderPageServer.js:185forkspageContextBegin— the context from beforeroute()— merges only the abort's own pageContext, and callsrenderPageServerAfterRoutedirectly.route()never re-runs. Lost.render(url)route()runs again. Present.renderPageServerAfterRoute.js:32callsloadPageConfigsLazyServerSide, which runsonCreatePageContextat its line 28 — on the error fork too, and beforeexecHookDataAndOnBeforeRender. The hook receives aget-onlyProxyover the real pageContext (getPublicProxy.js), so a direct write lands on the object Vike goes on to render.The same fork handles an unhandled rejection, not only an abort, so this covers a 500 as well as a 404.
Closes #110
🤖 Written by the sydevs autonomous loop — see what this is
🤖 Generated with Claude Code
https://claude.ai/code/session_01LwiQ9UQALv96Atvy9okdsS