Skip to content

fix(urls): Link does not normalize its href, so a trailing slash spells a page twice - #121

Merged
sydevs-bot merged 9 commits into
mainfrom
claude/fix-119-sitepath
Sep 21, 2026
Merged

sydevs-bot merged 9 commits into
mainfrom
claude/fix-119-sitepath

Conversation

@sydevs-bot

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

Copy link
Copy Markdown
Contributor

Summary

  • <Link href="/about/"> rendered /fr/about/ while ContentHead emitted /fr/about as that page's canonical. English was affected too, with no prefix involved.
  • New localeHref(locale, href) in lib/urls.ts composes the isSitePath guard, normalizeContentPath and localePath, so the rule stays in one place. Link and buildEmbedSnippet both call it.
  • It normalizes the path only. A query or fragment goes back untouched, so ?url=https://example.com/ keeps the slash that belongs to its value.

Changes

  • lib/urls.ts — adds localeHref; localePath's JSDoc now points arbitrary-href callers at it instead of at isSitePath.
  • components/atoms/Link/Link.tsx, components/molecules/EmbedButton/EmbedButton.tsx — one call each, replacing the duplicated isSitePath(…) ? localePath(…) : href ternary.
  • layouts/LayoutChrome.tsx — the language dropdown hands urlPathname on as written. Its own normalizeContentPath call did what Link now does to the same string.
  • AGENTS.md — the URL-spelling rule points at localeHref, and stays a pointer rather than a second copy of the JSDoc.

Preview

Test results

  • Lean gate: ✓ .claude/skills/pr-prep/check.sh — lint (0 errors), tsc --noEmit, vitest run 721/721
  • Targeted specs: 60 passed across lib/urls.test.ts, Link.test.tsx, EmbedButton.test.tsx, LayoutChrome.test.tsx
  • Build: N/A — no build, server or Wrangler paths touched

Before implementing, every row of the ticket's evidence table was re-measured against current main (cf7e088, past both #118 and #120) by running the real lib/urls.ts. All reproduced. The new specs were then confirmed to fail against the unfixed function: 6 failures across all three files.

Manual verification

  1. Open a /fr page and click a rich-text link whose URL an editor typed with a trailing slash — the href should be /fr/<slug>, matching the canonical in <head>.
  2. Confirm the language dropdown still points at /fr on the home page.

Notes for reviewer

Three judgement calls worth your eye:

  1. The function is named localeHref, not the ticket's sitePath. The critic argued sitePath overclaims — it returns mailto:, //cdn… and '' untouched, and since the query/fragment split it returns a whole href, not a path. localeHref is the third member of the module's existing vocabulary beside localePath and localeUrl. Adopted in 59bbe0c. Revert that commit if you want the ticket's name back.

  2. I answered the ticket's open question the other way. It recommended accepting that a trailing slash before ? or # survives. Skipping those hrefs turned out to leave three wrong spellings, including /index?x=1 leaking the routing-internal /index into an href against this repo's own stated invariant. Splitting at the first ? or # costs three lines, so I fixed it. Revert 82ab0ed if you disagree.

  3. localePath stays the raw primitive, per the ticket's Approach step 4. The altitude review wanted normalization folded into it; the critic came down the other way, and I left it. Reasoning in Detail below — it is a one-line follow-up either way.

Filed separately, found on the way: #122 — the atlas breadcrumb and event links drop the visitor's locale.

Detail

Adversarial review, three findings, all adopted.

  • lib/urls.ts — the name overclaims. Renamed to localeHref in 59bbe0c, across both call sites, the specs and AGENTS.md.
  • layouts/LayoutChrome.tsx — the normalizeContentPath call is dead. Confirmed by reading the chain: pathWithoutLocale fed only language.href → LanguageDropdown.tsx:75 → Link → localeHref, which normalizes the same string, /index branch included. Dropped in 48c3833, with a new LayoutChrome spec pinning the wiring, so the equivalence that justifies the deletion is now enforced rather than argued.
  • AGENTS.md — the rule doubled to restate the JSDoc. Cut in 40decff, one line shorter than before this PR. One deviation from the suggested wording: //another-origin stays in the not-ours-to-spell list, because a protocol-relative URL is the member of that list that looks like a path.

On localePath (note 3). All four existing callers (lib/head.tsx:190, lib/hreflang.ts:96,100, pages/+onBeforeRoute.ts:19, pages/[slug]/+data.ts:86) pass an already-normalized path or the literal '/', and normalizeContentPath is idempotent, so folding it in would alter no existing output. The critic's argument against: localeHref still needs its own guard and its own query/fragment split, so the fold shrinks nothing and costs a primitive's documented contract that #118 and #120 just set.

Earlier findings, both mine, both caught after the first green gate:

  • normalizeContentPath strips a final slash off whatever string it gets, and it was being handed a whole href. /share?url=https://example.com/ rendered /fr/share?url=https://example.com — a corrupted query value on an editor-typed rich-text link. Fixed in 686b688, then properly in 82ab0ed.
  • A LayoutChrome comment claimed urlPathname may be nullish. It is typed string; a throwaway assignment compiled clean. That comment is gone with the call it explained.

Dismissed, with reasons:

  • Un-export isSitePath, now that only localeHref calls it. Kept. AGENTS.md names it as the site-path predicate and it has its own specs; removing a documented export is a separate decision, not this ticket's.
  • Fold the pre-existing describe('isSitePath') block into the new one. Kept. It predates this PR and tests the predicate rather than the branch.
  • Hoist normalizeContentPath's /\/+$/ to module scope. Skipped. Pre-existing, and measured as immaterial — one small short-lived allocation next to three object literals Link already builds per render.
  • A siteUrl(origin, locale, href) to absorb EmbedButton's ${origin}${src}. Skipped as over-engineering for one caller.

Not a regression, deliberate behaviour change: href="/index" now renders /fr rather than /fr/index. /index was already a link to a 301, and the language dropdown — the one place that now passes it — gets the same /fr it got before.

Efficiency: no Worker bundle delta — both call sites swapped named imports within lib/urls, and normalizeContentPath was already reachable from both bundles. LayoutChrome drops one import.

Test hygiene: lib/urls.test.ts carried two overlapping describe('localePath') blocks, one left by #118 and one by #120. Folded into one; both of the deleted block's assertions survive.

Closes #119

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

🤖 Generated with Claude Code

https://claude.ai/code/session_01UmTczzh2NV5SddDiDCkeMM

`localePath` documents a precondition — pass it a path from
`normalizeContentPath` — that its two component callers could not meet.
`Link` takes any href a component writes, and `RichText` hands it the URL
an editor types into the rich-text link dialog. `buildEmbedSnippet` takes
whatever `embedPath` it is given. Neither normalized first, so
`<Link href="/about/">` rendered `/fr/about/` while `ContentHead` emitted
`/fr/about` as that same page's canonical: one document spelling one URL
two ways.

English is affected too. `localePath` returns the path untouched for the
default locale, so `/about/` contradicted a canonical of `/about` with no
locale prefix involved at all.

`sitePath` composes the guard, the normalization and the prefix, so the
rule stays in `lib/urls.ts` rather than being restated at each call site.
It normalizes inside the `isSitePath` branch, never before it:
`normalizeContentPath('')` returns `/`, which would turn an empty href
into a link to the home page.

A trailing slash before a `?` or a `#` survives, because
`normalizeContentPath` strips only a final slash. That limit is recorded
in the JSDoc and in a spec rather than fixed with a URL parser.

`lib/urls.test.ts` carried two overlapping `localePath` describe blocks,
one from #118 and one from #120. They are folded into one.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01S2Dv9N87tE1CJaufz5U41v
The `lib/urls.ts` convention told a caller holding arbitrary hrefs to
filter them through `isSitePath` itself. That is now `sitePath`'s job,
and filtering alone was never enough: it let a trailing slash through.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01S2Dv9N87tE1CJaufz5U41v
Four review lenses over the branch diff. Production code came back clean
on reuse, efficiency and altitude; these are the test and comment fixes.

- `sitePath`'s JSDoc stated the `''` case as the reason for the guard
  order. The invariant is more general — the predicate classifies the
  href as written — so it now says that, in a third of the lines.
- One non-site-path fixture list, not two spellings of it.
- Drop assertions that only re-prove `localePath`: `localePath('en', '/')`
  was already asserted a test above, and `sitePath`'s locale rows are
  `localePath`'s rows.
- Move the no-locale trailing-slash assertion into the trailing-slash
  test, out of the one about the `pageContext` fallback.
- `LayoutChrome`'s comment said adding the prefix back is `Link`'s job.
  `Link` now normalizes as well, and the call here survives because
  `urlPathname` may be nullish, which `sitePath` does not accept.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01S2Dv9N87tE1CJaufz5U41v
`normalizeContentPath` strips a final slash off the whole string, and it
was being handed a whole href. An editor-typed rich-text link then lost a
slash that belonged to a value rather than to a path:
`/share?url=https://example.com/` rendered `/fr/share?url=https://example.com`,
and `/about#heading/` lost its anchor's trailing slash.

`sitePath` now normalizes only an href with no `?` and no `#`. The
trailing-slash fix is unchanged for every path that carries neither, and
the limit the JSDoc already recorded — `/about/#section` keeps its
slash — is now the mechanism rather than a side effect.

Found by the review pass over this branch, not in the ticket.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01S2Dv9N87tE1CJaufz5U41v
Skipping an href that carried a `?` or a `#` kept the query's value safe
but left three wrong spellings: `/about/#section` rendered `/fr/about/#section`
against a canonical of `/fr/about`, `/?utm=1` rendered a 200 duplicate of
`/fr`, and `/index?x=1` leaked the routing-internal `/index` into an href,
against this repo's own rule that `/index` is never a URL.

Splitting at the first `?` or `#` normalizes the path and puts the rest
back exactly as written, which is correct in all three cases and still
never edits a slash that belongs to a value.

This answers the ticket's open question — it recommended accepting the
limit, on the grounds that fixing it meant a parser. It costs three lines,
and leaving it violated a stated invariant.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01S2Dv9N87tE1CJaufz5U41v
Both templates build `embedPath` without a trailing slash, so the comment
claiming a caller produces one was wrong about its own fixture.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01S2Dv9N87tE1CJaufz5U41v
@cloudflare-workers-and-pages

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

Copy link
Copy Markdown

Deploying wemeditate-design with  Cloudflare Pages  Cloudflare Pages

Latest commit: 40decff
Status: ✅  Deploy successful!
Preview URL: https://86e5434b.wm-design.pages.dev
Branch Preview URL: https://claude-fix-119-sitepath.wm-design.pages.dev

View logs

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Sep 21, 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 40decff Commit Preview URL

Branch Preview URL
Sep 21 2026, 07:18 PM

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

sydevs-bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor Author

🤖 no session running

recent dispatches
  • 19:11Z address-review · attempt 1 · session · done
  • 19:11Z address-review · attempt 1 · session
  • 19:06Z adversarial-review · attempt 1 · session · done
  • 19:06Z adversarial-review · attempt 1 · session

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

The diff is the size of the ticket: one composed function, two one-line call sites, no refactor smuggled in beside them. The query/fragment split overrides the ticket's open question and is right — normalizeContentPath strips the last slash of whatever it is handed, so the un-split version corrupts ?url=…/. On note 1: leave localePath alone. Folding normalization into it would not shrink sitePath, which still needs its own guard and split.

Three findings, all consider, none blocking:

  • lib/urls.ts:177 — the name sitePath overclaims.
  • layouts/LayoutChrome.tsx:115 — that normalizeContentPath is now dead.
  • AGENTS.md:194 — the rule doubled to restate the JSDoc.

What was checked. #119 against the diff. lib/urls.ts, Link.tsx, EmbedButton.tsx, LayoutChrome.tsx read whole. Traced languages[].href → LanguageDropdown.tsx:75 → Link to prove the dead call. git grep on the head leaves localePath( in lib/urls.ts and two '/' literals under pages/. No contract file is in the diff.

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


Generated by Claude Code

Comment thread lib/urls.ts Resolved
Comment thread layouts/LayoutChrome.tsx Resolved
Comment thread AGENTS.md 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 21, 2026
`sitePath` overclaimed. The function returns `mailto:`, `//cdn…` and an
empty href untouched, and since the query/fragment split it returns a
whole href rather than a path. It also read as the constructor half of
`isSitePath` two functions above it.

`localeHref` is the third member of the module's existing vocabulary:
`localePath` returns a path, `localeUrl` an absolute URL, `localeHref`
the href to render for any href.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01UmTczzh2NV5SddDiDCkeMM
`pathWithoutLocale` fed only `language.href`, which `LanguageDropdown`
hands to `Link`, which calls `localeHref` — `normalizeContentPath` on
the same string, `/index` branch included. The layout now passes
`urlPathname` on as written, and a new spec pins that wiring.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01UmTczzh2NV5SddDiDCkeMM
The paragraph had doubled to restate `localeHref`'s JSDoc. AGENTS.md is
read every session; the JSDoc is read when someone opens the function.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01UmTczzh2NV5SddDiDCkeMM
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

All three findings adopted, nothing rebutted, nothing asked. Pushed as 40decff.

Lean gate green: lint, tsc --noEmit, 721/721.

🤖 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 21, 2026
@sydevs-bot
sydevs-bot marked this pull request as ready for review September 21, 2026 19:20
@sydevs-bot
sydevs-bot requested a review from Ardnived September 21, 2026 19:20
@sydevs-bot
sydevs-bot added this pull request to the merge queue Sep 21, 2026
Merged via the queue into main with commit 81802cc Sep 21, 2026
34 of 38 checks passed
@sydevs-bot
sydevs-bot deleted the claude/fix-119-sitepath branch September 21, 2026 19:44
@sydevs-bot sydevs-bot moved this from Revising to Done in SYDevelopers Technical Roadmap Sep 21, 2026
sydevs-bot pushed a commit that referenced this pull request Sep 21, 2026
#121 landed `localeHref` on the same three files this branch extends with
`sitePathFromUrl`. Both sides are kept: `sitePathFromUrl` relativizes a
same-origin canonical, `Link` then spells it through `localeHref`, which
re-normalizes an already-normalized path to no effect.

- `lib/urls.test.ts` — both suites, both imports.
- `AGENTS.md` — main's rewritten `localePath` paragraph, with this branch's
  `sitePathFromUrl` paragraph after it.
- `lib/urls.ts` — auto-merged. `sitePathFromUrl`'s JSDoc named `localePath`
  as its only caller, which is no longer the path `Link` takes.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bsg6d9XWRSKXFF2zAsnoWp
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(urls): Link does not normalize its href, so a trailing slash spells a page twice

3 participants