Skip to content

fix(pages): keep the locale on the error page after a thrown render() - #113

Merged
sydevs-bot merged 6 commits into
mainfrom
claude/fix-110-error-page-locale
Sep 18, 2026
Merged

sydevs-bot merged 6 commits into
mainfrom
claude/fix-110-error-page-locale

Conversation

@sydevs-bot

@sydevs-bot sydevs-bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • A 404 under /fa/… now renders <html lang="fa" dir="rtl"> instead of English left-to-right.
  • The error page's wm-web-translations read carries its locale again, so it stops caching in KV under one key every locale shares.
  • One function, localeFromPath in lib/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. Fills locale on 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 (expect lang="<locale>"), and /zz/a/b/c (expect lang="en", the flavour that was never broken).

Test results

  • Lean gate: ✓ lint (0 errors), tsc --noEmit, vitest run — 651 tests, 70 files

  • Targeted specs: lib/urls.test.ts, pages/onCreatePageContext.test.ts, pages/onBeforeRoute.test.ts

  • Build: ✓ pnpm build. Confirmed the hook lands in dist/server only.

  • Live, on pnpm dev, the same server before and after the hook:

    URL before after
    /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/c lang="en" lang="en" (never broken)
    /en/about?x=1 301 → /about?x=1 301 → /about?x=1

    The request log agrees. Without the hook the error render reads 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.

Manual verification

  1. On the preview, curl -s <preview>/<locale>/__does_not_exist__ | grep -o '<html[^>]*>' for an offered non-English locale. Expect lang="<locale>".
  2. If the site offers fa, the same URL should carry dir="rtl". The local dev server showed this; no CI environment offers fa today, so the smoke spec asserts whatever localeDirection says for the locale it discovers.
  3. Worth a reviewer's eye: the error page now renders lang="de" (say) for a locale prefix the site does not offer, over English copy, because server/site-context.ts:145 404s before the locale set is known to the error fork. For an unoffered RTL prefix that also flips dir to rtl, so the English snapshot renders right-to-left where it used to render ltr. 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.ts get? Server and client (metaBuiltIn.js:24). It is .server.ts here: Vike re-runs +onBeforeRoute in the browser on hydration and on every client-side navigation — its own comment at renderPageClient.js:112 says "needed for i18n URL locale extraction" — so the client already has the locale and a second writer there would fix nothing. This also corrects the passToClient comment, which credited vike-react.

    Confirmed against installed node_modules in the review run: route() at renderPageClient.js:116, then this hook at :167→:649, then data(). 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.

  • Make +onCreatePageContext the sole unconditional writer and drop .server. Genuinely simpler on paper — one writer, no precedence rule. Feasible too: the hook order above puts it after route() on every path, client included. Not taken: +onBeforeRoute still needs localeFromPath for urlLogical and the 301, so dropping the assignment buys a two-line guard at the price of a second derivation on every request — and it moves where locale comes from on every request, which this environment cannot verify without a CMS key. The two-writer shape leaves the nominal path byte-identical to main, which is what made the before/after above possible. Worth revisiting with CMS access.
  • new URL(urlOriginal, 'http://localhost') is heavier than a split. Kept: Vike's own runtime parses urlOriginal exactly 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.
  • Memoise discoverFromCms. Applied, then reverted after review — see the fourth commit. loadCmsSamples never 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.
  • Consolidate the locale regex in 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 calls isLocale or produces a Locale, so "exactly one function derives a locale from a URL path" holds.

Filed on the way, not fixed here:

Why `+onCreatePageContext` is the seam

Read from [email protected], the version pnpm-lock.yaml pins.

Path What happens to locale
Nominal render route() runs +onBeforeRoute and merges its result. Present.
No-route-matched 404 Renders _error from that already-routed context. Present.
Thrown render(<status>) renderPageServer.js:185 forks pageContextBegin — the context from before route() — merges only the abort's own pageContext, and calls renderPageServerAfterRoute directly. route() never re-runs. Lost.
Thrown render(url) Re-enters the recursive entry, so route() runs again. Present.

renderPageServerAfterRoute.js:32 calls loadPageConfigsLazyServerSide, which runs onCreatePageContext at its line 28 — on the error fork too, and before execHookDataAndOnBeforeRender. The hook receives a get-only Proxy over 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

`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
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

Deploying wemeditate-design with  Cloudflare Pages  Cloudflare Pages

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

View logs

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

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

@sydevs-bot sydevs-bot added the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Sep 17, 2026
@sydevs-bot

sydevs-bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor Author

🤖 no session running

recent dispatches
  • 00:54Z resolve-conflicts · attempt 1 · session · done
  • 00:54Z resolve-conflicts · attempt 1 · session
  • 07:22Z address-review · attempt 1 · session · done
  • 07:22Z address-review · attempt 1 · session
  • 07:14Z adversarial-review · attempt 1 · session · done

Written by the sydevs dispatcher. Sessions never edit this comment.

@sydevs-bot sydevs-bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧐 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:24
  • consider — 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, plus server/site-context.ts:63-90 and :145.
  • The +onBeforeRoute refactor preserves behaviour: same regex, same /index mapping, same 301 condition.
  • Grepped every locale-shaped regex and isLocale caller: the others exclude a prefix or read a query param.
  • Unchecked: Vike's hook order — no node_modules here, so renderPageServerAfterRoute.js:32 is the PR's reading.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

Comment thread pages/+onCreatePageContext.server.ts Resolved
Comment thread pages/+onCreatePageContext.server.ts Resolved
@sydevs-bot sydevs-bot added bot:working Lock: a cloud session is running on this item. Removed when it ends. and removed bot:working Lock: a cloud session is running on this item. Removed when it ends. labels Sep 17, 2026
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

Both critic threads answered. No commit — the evidence went the other way on each.

  • Two writers, or one? — rebutted. Installed node_modules and read the hook order the critic could not. route() runs before onCreatePageContext on the server, on hydration, and on client navigation. One writer is feasible, but +onBeforeRoute still derives localeFromPath for urlLogical and the 301, so it trades a two-line guard for a second derivation per request.
  • Unoffered RTL locale renders English right-to-left — rebutted. Real, and now recorded in the PR body. Bounding it costs the error fork a CMS read.

Both left open for the reviewer to settle. The PR body carries both answers.

Lean gate re-run on 0685d16: lint, tsc --noEmit, 651 tests in 70 files — green.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot sydevs-bot removed the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Sep 17, 2026
@sydevs-bot
sydevs-bot marked this pull request as ready for review September 17, 2026 07:30
@sydevs-bot
sydevs-bot requested a review from Ardnived September 17, 2026 07:30
@sydevs-bot sydevs-bot moved this from Revising to Approved in SYDevelopers Technical Roadmap Sep 17, 2026
@sydevs-bot sydevs-bot added the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Sep 18, 2026
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
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

Merged main and resolved the one conflict. Pushed a0ec3af.

Only the types/vike.d.ts file header conflicted. #107 dropped "Cloudflare Workers bindings" along with the KV binding that phrase named. This branch had rewritten the same line to "the hooks under pages/", because a second hook now writes locale. The header keeps both edits.

No code file overlapped, so the rest auto-merged. Lean gate on the merge: lint 0 errors, tsc --noEmit clean, 655 tests in 70 files. pnpm build green too.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot sydevs-bot removed the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Sep 18, 2026
@sydevs-bot
sydevs-bot added this pull request to the merge queue Sep 18, 2026
Merged via the queue into main with commit ac6d0a8 Sep 18, 2026
11 checks passed
@sydevs-bot
sydevs-bot deleted the claude/fix-110-error-page-locale branch September 18, 2026 01:38
@sydevs-bot sydevs-bot moved this from Approved to Done in SYDevelopers Technical Roadmap Sep 18, 2026
sydevs-bot added a commit that referenced this pull request Sep 18, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

fix(pages): keep the locale on the error page after a thrown render()

3 participants