Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
Draft
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
2 changes: 2 additions & 0 deletions packages/mosaic/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Read every line that matches your task. Paths below are relative to this package
- Write or change a controller, or how a feature handles pending state, errors, dialogs or removal focus → [Controllers](docs/controllers.md)
- Write or debug a state machine → [Controllers → Machines](docs/controllers.md#machines), then [Machine runtime](src/machine/README.md)
- Build a flow with several steps on one surface → [Controllers → Multi-step flows](docs/controllers.md#multi-step-flows), then [Views → Moving between screens](docs/views.md#moving-between-screens-on-one-surface)
- Route a component's pages or steps, put them in the URL or behind the host router, or open a modal at a page → [Routing](docs/routing.md)
- Write or change a view, a dialog or step it renders, or a feature's copy → [Views](docs/views.md)
- Add a styled component, or change a token, a `.cl-<slot>` class or `data-<axis>` attribute, `MosaicProvider`, or the CSS build → [Theming](docs/theming.md)
- Write or change a `*.styles.ts` or `*.stylex.ts` file → [StyleX](docs/stylex.md)
Expand Down Expand Up @@ -60,6 +61,7 @@ Before you change a file that matches a pattern below, read its guide, even for
- `src/**/*.view.tsx` → [Views](docs/views.md)
- `src/**/*.doc.ts`, `src/docs-schema.ts` → [Agent docs](docs/agent-docs.md)
- `src/**/__tests__/**`, `src/**/*.test.ts(x)`, `src/**/*.test-d.ts` → [Testing](docs/testing.md)
- `src/routing/**`, `src/modals/modal-host.tsx` → [Routing decisions](docs/edrs/README.md)

## Layers in one paragraph

Expand Down
45 changes: 45 additions & 0 deletions packages/mosaic/docs/edrs/0001-embedded-segment-router.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
id: 1
title: 'Embed a segment router that derives its route from the location'
summary: "A Mosaic component owns one segment of the page's location and derives its route from it on every change. Navigations inside the segment are steps through the router; everything else is an exit handed to clerk-js as a full URL."
status: accepted
implementation: shipped
implementation_note: 'src/routing; exits still go through clerk-js as before.'
date: 2026-11-06
authors:
- 'Fredrik Höglund <fredrik@clerk.dev>'
tags: [architecture]
supersedes: null
superseded_by: null
---

## TL;DR

A component owns a segment under a base, `security` under `/user`, and never the page. It reads its segment, derives a route from it, and navigates by writing a new segment. It keeps no routing state and awaits nothing after a write. A navigation whose destination is outside the segment is not a routing concern: build a full URL and hand it to `clerk-js`, which keeps its origin and protocol checks, URL decoration and the transitive-state handling.

## Context

Mosaic renders inside the host app and on the server, before `clerk-js` has loaded, so it cannot wait for `clerk-js` to route. Legacy's router loads with `clerk-js`, uses one `navigate` for both steps and exits, and awaits navigations, which most frameworks cannot honour and which Clerk hacks around. Legacy's `<Route>` tree also mixes matching, rendering, guards and relative paths.

Rejected: routing through `clerk.navigate` for steps, as PR #9843 did. It awaited the write and committed with `flushSync`, ignoring outside changes while in flight, which reintroduced the awaitable-navigate dependency and a stuck state when a host promise never resolves.

## Decision

Mosaic has its own routing layer that owns one segment per component and derives the route from the location on every change. `go(name, params)` is the only navigation it offers. Exits are not part of the router and cannot be expressed through it. Guards belong to the component or its machine, not to routes.

## Consequences

- Nothing is synchronised after a navigation, so nothing needs to be awaited.
- The server and the client render the same thing from the same input.
- Open-redirect protection stays in one place, `clerk-js`.
- "Which step next" decisions after an OAuth or email-link return still live in `clerk-js` and should move to `shared` as pure functions; that is separate work.

## References

- `src/routing/use-router.ts`, `src/routing/router.ts`
- [Routing in Mosaic](../internals/routing.md), mental model and constraints
- [MEDR-0002](./0002-names-in-parts-out.md), [MEDR-0004](./0004-owner-and-nested-routers.md)

## Changelog

- **2026-11-06**: Accepted.
44 changes: 44 additions & 0 deletions packages/mosaic/docs/edrs/0002-names-in-parts-out.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
id: 2
title: 'Route by name, encode in the location, keep the codec representation-free'
summary: 'Code refers to routes by name and go() takes a name, never a URL. The location splits and decodes once per part and encodes once on write. The codec matches decoded literals against decoded parts and never sees a URL string.'
status: accepted
implementation: shipped
implementation_note: 'src/routing/routes.ts, src/routing/location.ts; virtual mode has no encoded form, so the encoding half is exercised only by the codec tests until a URL mode lands.'
date: 2026-11-06
authors:
- 'Fredrik Höglund <fredrik@clerk.dev>'
tags: [architecture, security]
supersedes: null
superseded_by: null
---

## TL;DR

A route table maps names to patterns; views, controllers and machines use names; `go(name, params)` takes a name and the param types the pattern implies. Only `routes.ts` sees patterns. A pattern literal is written decoded, `api keys` not `api%20keys`; a percent-encoded literal throws when the pattern compiles. Nothing above a location calls `encodeURIComponent` or `decodeURIComponent` on anything from a URL, and nothing navigates to a value taken from a URL.

## Context

Double decoding has caused real vulnerabilities at Clerk. Legacy builds and parses URLs ad hoc across components. Names are also what lets the step/exit split from [MEDR-0001](./0001-embedded-segment-router.md) be enforced by a type rather than a convention.

Rejected: a codec that works on path strings and encodes or decodes itself. It would be one half of a double decode by construction. Rejected: a literal check that refuses anything `encodeURIComponent` would change; it rejected spaces and non-ASCII that match fine as decoded literals. The hazard is only a literal written in encoded form.

## Decision

The location owns encoding: split the raw segment, decode once per part, encode once per part on write, and read a malformed escape as no parts. The codec is representation-free: it matches decoded literals against decoded parts and captures params verbatim. `go` takes names; param types flow from the pattern to the name.

## Consequences

- Encoding cannot change which route matches, and a param is either fully decoded once or absent.
- A pattern's shape can change for legacy compatibility without touching callers.
- A framework adapter that only offers a decoded path collapses the encoded-slash distinction; that ambiguity is the host's, and Clerk never generates a value with a slash.
- Every location must carry a round-trip test for slashes, percent signs and non-ASCII.

## References

- `src/routing/routes.ts`, `src/routing/__tests__/routes.test.ts`
- The Security section of [Routing in Mosaic](../internals/routing.md)

## Changelog

- **2026-11-06**: Accepted.
54 changes: 54 additions & 0 deletions packages/mosaic/docs/edrs/0003-page-state-acts-like-a-url.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
id: 3
title: 'Hold routing state above the components and make it behave like a URL'
summary: "The page's routing state is a map from base to parts in MosaicProvider. A segment lives while its base has a mounted owner. A modal is open exactly while its segment exists. Components hold no routing state in any mode."
status: accepted
implementation: shipped
implementation_note: 'src/routing/virtual-store.tsx, src/modals/modal-host.tsx. The URL modes keep the same contract without the map; see MEDR-0008.'
date: 2026-11-06
authors:
- 'Fredrik Höglund <fredrik@clerk.dev>'
tags: [architecture, virtual, modals]
supersedes: null
superseded_by: null
---

## TL;DR

In virtual mode `MosaicProvider` holds a map keyed by normalised base, one segment per owner. A location is one segment: it reads its own, writes its own, clears its own. A mounted `useRouterScope` counts itself in as an owner of its base; when a base's last owner leaves, the store drops the segment after the commit. A modal host keeps no open flag: `open(options)` writes `initialParts(options)` to the modal's base, `close()` clears it, and the content renders while the segment exists. Nothing a component does on mount or unmount writes to the location.

## Context

Legacy virtual state lived inside each component, so it could not be written before mount and did not behave like a URL. Lifting it gave modals a way to open at a page and made two profiles with different bases independent, but it also made the state outlive the component, which no URL does: a host that navigated away and back found the profile where it left it.

Rejected, in order: clearing on modal close only (left inline profiles stale); the component clearing on its own unmount (wipes a pre-mount write under StrictMode and puts mode knowledge in the component); a writer-keyed release that clears only what the leaving scope wrote (right for user navigations, wrong for a host's seed, which a URL would also drop); an ownership registry with takeover (correct, but a queue, two contexts and a provider). Owner counting with a post-commit sweep is what survived the contract suite, including an owner replaced in the same commit. On its own it treated a parked component as having left, which under `Activity` would reset a profile on Back and on every hide; parking in component state is what keeps that case. A `WeakMap` in the store keyed by the location was passed over because the component's own state already has the right lifetime. Parking from the same effect that owns the base, with the segment as a dependency, was tried and failed the outside-clear case: the cleanup also ran when the segment was cleared and the next run restored it.

## Decision

Routing state is a map above the components. A segment lives while its base has an owner, counted in store state and swept after the commit; only a base that has had an owner is swept, so a write ahead of a later mount survives. A component owns its segment only while its effects are live; when they stop it parks the segment in its own state, and when they run again it writes the segment back unless the base was written meanwhile. The park happens only on hide, unmount or a change of location, never on a navigation; the scope renders from the parked segment while hidden, so the page's subtree keeps its state and resume has no flash; a park is tied to the location it came from, so a scope moved to another base starts on that base's root; and the parked value is consumed in render once a segment is present, so a segment cleared from outside shows the root. A truly unmounted component has no state to park in and starts fresh. A modal's open state is the presence of its segment, and its content loads whenever the segment is present, so a segment written from outside opens the modal fully. The sweep deletes only the segment committed with the zero count, never one written after it. In URL modes `own` is a no-op and nothing is parked, because the URL holds the segment; owners are keyed by base alone.

## Consequences

- Leave and return starts fresh; a parked owner resumes where it was; an owner replaced mid-exit keeps its page; StrictMode changes nothing.
- A parked modal does not resume, since its content is inside a closed dialog; legacy closes modals on navigation too.
- The parked case is a todo in the contract suite until a React 19 test project exists, since React 18 cannot produce the cycle. The outside-clear case is in the suite, because it is the one a park that also ran on navigation got wrong.
- Back closes a modal and a URL with the segment opens one in the URL modes. A link in the query string writes a segment the same way in every mode, and a redirect returns through one, [MEDR-0010](./0010-links.md).
- A modal clears its own segment when the host's pathname changes, in every mode: the modal host records the page it opened on, on every `open()`, and compares in a passive effect. In the URL modes a host navigation that drops the param closes the modal before the check runs, and one that carries the param to another page, a host that preserves its query string, meets the same close, with a replace that removes the param, so the mode does not change what a page change does; the rule was virtual-only first and widened for that case. Legacy closed modals on every Clerk navigation, hash steps included; here a change of the query string or the hash alone leaves a modal open, since in those modes its own steps live there. A deep link opens on the page it names and stays. The virtual store itself is not scoped to a page, so an inline owner that stays mounted across pathnames keeps its segment. One race is accepted: a page that opens the same modal from its own mount effect, in the commit that changed the pathname, loses to the close, since the child's effect runs before the host's; an `open()` after that commit is kept, because it records the new page. Making the close a layout effect would win by phase ordering alone, which is the kind of timing rule this design avoids.
- Ownership arrives one commit after mount, so an owner's first redirect runs on its second commit.
- `useSegmentParts` returns `undefined` for a base with no segment; a scope reads that as the root, a modal as closed.
- A URL location was expected to project the map so two writes in one tick could not clobber each other. [MEDR-0008](./0008-url-carriers.md) decided otherwise: a URL location reads and writes the URL directly, and the clobber is accepted.

## References

- `src/routing/virtual-store.tsx`, `src/routing/location.ts`, `src/modals/modal-host.tsx`
- `src/routing/__tests__/location-contract.test.tsx`
- [MEDR-0007](./0007-location-contract-suite.md)

## Changelog

- **2026-11-06**: Accepted.
- **2026-11-06**: Amended after review: render from the parked entry while hidden, park tied to its location, compare-and-delete in the sweep, content loaded on any entry.
- **2026-11-11**: Amended when the URL modes shipped: the projection consequence points at MEDR-0008.
- **2026-11-17**: Amended: links and the return after a redirect are MEDR-0010.
- **2026-11-24**: Amended: a virtual modal closes when the host's pathname changes.
- **2026-11-25**: Amended: `open()` records the page every time; the mount-effect race is accepted and named; entry is now called segment; the close on a pathname change applies in every mode.
51 changes: 51 additions & 0 deletions packages/mosaic/docs/edrs/0004-owner-and-nested-routers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
id: 4
title: "Let the segment's owner name its root and decide the rest; nested routers never write"
summary: 'useRouterScope is for the one component that owns a segment: it names the root that lives at the base, an unknown segment shows the root and is replaced with the base, and an unsettled table reports no route. useRouter is for anything inside: it never writes.'
status: accepted
implementation: shipped
implementation_note: 'src/routing/use-router.ts; MosaicRoutingScope is rendered only in tests until a nested consumer exists.'
date: 2026-11-06
authors:
- 'Fredrik Höglund <fredrik@clerk.dev>'
tags: [api]
supersedes: null
superseded_by: null
---

## TL;DR

The component that owns a segment calls `useRouterScope(routes, { routing, path, root })` once and renders the `scope` it returns around its children. `root` is the route that lives at the base: it is matched by the empty segment and `go` writes the empty segment for it, whatever pattern the table gives it, and that pattern is accepted too and canonicalised to the base. Any other segment nothing matches shows the root at once and is replaced with the base, so the dead segment leaves history. `route` is never undefined, unless the owner passes `ready: false` because its table depends on data that has not arrived; then it is undefined and nothing is written, so an entry the table cannot match yet survives. Children call `useRouter(routes)` with their own table, read mode and base from the scope, and get `route: undefined` for a segment a sibling owns. They never redirect.

## Context

If every router could redirect, two tables under one base would fight. Not-found needs a single decision, and the replace is what keeps legacy's back-button traps out. Page-shaped surfaces all put their first entry at the base, and the profiles also let that entry be named from outside through the modal `page` option, so the root needs both an empty segment and its own name.

Earlier shape: a `notFound` route, which the owner redirected unknown segments to. Two owners on one base with different fallbacks then looped, and three guards were tried: remembering the last redirected-from segment (blocked legitimately recurring segments), detecting an overwritten redirect (only worked at mount), and an ownership registry (correct, but a queue, two contexts and a provider). A redirect fuse replaced them. Naming the root made all of it unnecessary: every owner's fallback is its own base, so two owners of one base write the same thing and cannot loop, and the fuse was removed as untestable. A separate `aliases` map for the root's own segment was folded into `root` for the same reason.

`notFound` came back as an option of the owner, for the combined profile, which sends an unknown page in the organization group to that group's first page. It names another route for a segment nothing matches, so it reopens the loop for two owners of one base whose `notFound` disagree, and a route it names that needs params would be written without them, match nothing and redirect forever. The second is checked: a name whose built parts do not match back to it shows the root, with a warning. The first is left as the configuration error below rather than guarded with a fuse again.

Not-ready: the combined profile routed while the organization model loaded, and a deep link to an organization page was replaced with the base before the pages existed. Withholding the table at the call site was `main`'s answer; `ready` is the same answer as a routing-layer concept.

## Decision

Two hooks with an opaque scope between them. The owner names its `root`, redirects unknown segments to the base with replace, and reports no route while not ready; nested routers never write.

## Consequences

- `route` can be switched on without a guard. Passing `ready` at all makes it optional in the type, so an owner with an unsettled table has to decide what the loading window shows.
- Owners of one base that fall back to their root cannot loop each other, but the second keeps snapping the first back to its root on every navigation; a configuration error shows as that, with no warning. Two such owners whose `notFound` send one segment to different routes redirect each other forever, also with no warning. Owners of one base in different modes, a virtual and a query owner of `/user`, share the query param of [MEDR-0010](./0010-links.md): the query owner reads it as its entry and the virtual owner takes it as a link and removes it.
- An empty table, as when a profile has no pages yet, routes to a placeholder root and writes nothing.
- Nesting lands on the scope later: a sub-surface's base is the parent's base plus its segment.

## References

- `src/routing/use-router.ts`, `src/routing/routing-scope.tsx`
- `src/routing/__tests__/use-router.test.tsx`

## Changelog

- **2026-11-06**: Accepted.
- **2026-11-06**: Amended after review: `notFound` and the redirect fuse replaced by `root`; `ready` added.
- **2026-11-12**: Amended: `notFound` as an owner option recorded; a route it names that needs params shows the root with a warning; two owners of one base whose `notFound` disagree can loop.
- **2026-11-17**: Amended: owners of one base in different modes share a link's param.
Loading
Loading