Skip to content

Commit abce6f2

Browse files
ryansolidclaudecursoragent
committed
fix(solid-router): name the canonical arrival in the initial observe record
The client's initial record was declared in RouterProvider and read `router.latestLocation` as its frame closed, before the Transitioner committed the canonical form of the arrival (search defaults, trailing slash): `/about` arriving on a route whose search validates to `/about?tab=info` was named `/about`, and the next navigation's `from` was a location the record never named. Solid Router declares the arrival around building its context, the normalization of an empty arrival included, and reads `to` at settle, so the record names the normalized location (its `observe-navigation` spec, "an empty arrival normalized onto the base path is one initial record"). Declare the client's arrival around the Transitioner's establishing work (canonicalization included) and name the location it established; RouterProvider keeps the server's declaration around the load. Co-authored-by: Claude <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 245228c commit abce6f2

5 files changed

Lines changed: 82 additions & 54 deletions

File tree

‎.changeset/solid-router-observe-initial-route.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
'@tanstack/solid-router': patch
33
---
44

5-
Observe tier (Solid's dev and observe builds only): the router declares the route the document arrived on, with the same `OBSERVE.attribution.withOrigin` call it makes for every navigation, around the work in `RouterProvider` that establishes its initial match (`initial: true` on the ref), on both sides. On the client this is the first `"navigation"` record (`initial: true`, `at` the document's navigation start, no write to wait for), kept out of `feedback().navigations`; on the server the same declaration names the request's `"render"` record (`RenderEvent.route`: the matched route's `fullPath`, the path and search, the params), read when the render settles, so a render whose provider ran the load names the location it resolved. A consumer naming page loads and requests by route (`/users/$id` rather than one name per user) had this for every navigation but the first; now it has the first too.
5+
Observe tier (Solid's dev and observe builds only): the router declares the route the document arrived on, with the same `OBSERVE.attribution.withOrigin` call it makes for every navigation (`initial: true` on the ref), around the work that establishes its initial match — the Transitioner's on the client, `RouterProvider`'s load on the server. On the client this is the first `"navigation"` record (`initial: true`, `at` the document's navigation start, no write to wait for), kept out of `feedback().navigations`; on the server the same declaration names the request's `"render"` record (`RenderEvent.route`: the matched route's `fullPath`, the path and search, the params), read when the render settles, so a render whose provider ran the load names the location it resolved. A consumer naming page loads and requests by route (`/users/$id` rather than one name per user) had this for every navigation but the first; now it has the first too.
66

7-
The Transitioner committing the canonical form of the arrival location (search defaults, trailing slash) is part of the arrival, not a navigation: a router loaded before it mounted no longer records it as one, and the next navigation is dated from its own request rather than from that commit. Nothing changes in production, where `OBSERVE` is undefined.
7+
The Transitioner committing the canonical form of the arrival location (search defaults, trailing slash) is part of the arrival, as in `@solidjs/router`: the initial record names the canonical location, a router loaded before it mounted no longer records the commit as a navigation, and the next navigation is dated from its own request rather than from that commit. Nothing changes in production, where `OBSERVE` is undefined.

‎packages/solid-router/src/RouterProvider.tsx‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,17 @@ export function RouterContextProvider<
5656
? router.load().then(() => true)
5757
: true,
5858
)
59-
// The route the document arrived on, declared to Solid's observe tier
60-
// around the work that establishes the initial match (see
61-
// `describeInitial`); folded out of production with `OBSERVE`.
62-
const ready = Solid.OBSERVE
63-
? Solid.OBSERVE.attribution.withOrigin(describeInitial(router), establish)
64-
: establish()
59+
// The route the request is for, declared to Solid's observe tier around
60+
// the load that establishes it — the render's route (see
61+
// `describeInitial`); the client declares its arrival in Transitioner.
62+
// Folded out of production with `OBSERVE`.
63+
const ready =
64+
Solid.OBSERVE && (isServer ?? router.isServer)
65+
? Solid.OBSERVE.attribution.withOrigin(
66+
describeInitial(router, () => router.latestLocation),
67+
establish,
68+
)
69+
: establish()
6570

6671
// Client-side, the provider is where the router meets Solid's
6772
// server-function transport: it consumes the response metadata mutations

‎packages/solid-router/src/Transitioner.tsx‎

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import * as Solid from 'solid-js'
22
import { getLocationChangeInfo, trimPathRight } from '@tanstack/router-core'
33
import { isServer } from '@tanstack/router-core/isServer'
44
import { useRouter } from './useRouter'
5-
import { describeNavigation } from './observe'
5+
import { describeInitial, describeNavigation } from './observe'
66
import type { NavigationRequest } from './observe'
7-
import type { AnyRouteMatch } from '@tanstack/router-core'
7+
import type { AnyRouteMatch, ParsedLocation } from '@tanstack/router-core'
88

99
/** The history change was the arrival's own canonicalization, not a request. */
1010
const ARRIVAL = Symbol()
@@ -45,6 +45,8 @@ export function Transitioner() {
4545
// canonicalized (below): the initial declaration already covers it.
4646
let request: NavigationRequest | typeof ARRIVAL | undefined
4747
let canonicalizing = false
48+
// The location the initial declaration names: the arrival, canonical.
49+
let arrival: ParsedLocation | undefined
4850

4951
// Ack when the commit's transition settles (the atomic swap), not when the
5052
// flush parks it; superseded or rolled-back commits resolve false.
@@ -118,40 +120,55 @@ export function Transitioner() {
118120
queueMicrotask(() => router.load().catch(console.error))
119121
})
120122

121-
// The URL may have changed synchronously between render and settlement.
122-
router.updateLatestLocation()
123-
const nextLocation = router.buildLocation({
124-
to: router.latestLocation.pathname,
125-
search: true,
126-
params: true,
127-
hash: true,
128-
state: true,
129-
_includeValidateSearch: true,
130-
})
123+
// The route the document arrived on is declared around establishing it,
124+
// canonicalization included, as `@solidjs/router` does: the record
125+
// names the canonical location, and the commit is the arrival's.
126+
const establish = () => {
127+
// The URL may have changed synchronously between render and settlement.
128+
router.updateLatestLocation()
129+
const nextLocation = router.buildLocation({
130+
to: router.latestLocation.pathname,
131+
search: true,
132+
params: true,
133+
hash: true,
134+
state: true,
135+
_includeValidateSearch: true,
136+
})
131137

132-
if (
133-
trimPathRight(router.latestLocation.publicHref) !==
134-
trimPathRight(nextLocation.publicHref)
135-
) {
136-
// The history notifies synchronously inside the commit (no blocker to
137-
// await), so the subscriber above sees the flag.
138-
canonicalizing = true
139-
try {
140-
router.commitLocation({
141-
...nextLocation,
142-
replace: true,
143-
ignoreBlocker: true,
144-
})
145-
} finally {
146-
canonicalizing = false
138+
if (
139+
trimPathRight(router.latestLocation.publicHref) !==
140+
trimPathRight(nextLocation.publicHref)
141+
) {
142+
arrival = nextLocation
143+
// The history notifies synchronously inside the commit (no blocker
144+
// to await), so the subscriber above sees the flag.
145+
canonicalizing = true
146+
try {
147+
router.commitLocation({
148+
...nextLocation,
149+
replace: true,
150+
ignoreBlocker: true,
151+
})
152+
} finally {
153+
canonicalizing = false
154+
}
155+
return
147156
}
148-
return unsub
149-
}
150157

151-
if (!getResolvedLocation(router) && !router._tx) {
152-
queueMicrotask(() => router.load().catch(console.error))
158+
arrival = router.latestLocation
159+
if (!getResolvedLocation(router) && !router._tx) {
160+
queueMicrotask(() => router.load().catch(console.error))
161+
}
153162
}
154163

164+
const observe = Solid.OBSERVE
165+
if (observe !== undefined) {
166+
observe.attribution.withOrigin(
167+
describeInitial(router, () => arrival ?? router.latestLocation),
168+
establish,
169+
)
170+
} else establish()
171+
155172
return unsub
156173
})
157174

‎packages/solid-router/src/observe.ts‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { NavigationRef } from 'solid-js'
2-
import type { AnyRouter } from '@tanstack/router-core'
2+
import type { AnyRouter, ParsedLocation } from '@tanstack/router-core'
33

44
/**
55
* What the router says about its navigations to Solid's observe tier
@@ -70,23 +70,25 @@ export function describeNavigation(
7070
* publish to wrap. On the client the engine opens it at the time origin and
7171
* settles it as that work returns (the first `"navigation"` record,
7272
* `initial: true`); on the server the same call names the request's
73-
* `"render"` record (`RenderEvent.route`). The fields are getters read at
74-
* settle, so a server render whose provider ran the load names the location
75-
* the load resolved.
73+
* `"render"` record (`RenderEvent.route`). The fields are getters of
74+
* `location`, read at settle: the canonical location the client committed,
75+
* or the one a server load resolved.
7676
*/
77-
export function describeInitial(router: AnyRouter): NavigationRef {
78-
const pathname = () => router.latestLocation.pathname
77+
export function describeInitial(
78+
router: AnyRouter,
79+
location: () => ParsedLocation,
80+
): NavigationRef {
7981
return {
8082
kind: 'navigation',
8183
initial: true,
8284
get to() {
83-
return router.latestLocation.href
85+
return location().href
8486
},
8587
get name() {
86-
return describeRoute(router, pathname()).name
88+
return describeRoute(router, location().pathname).name
8789
},
8890
get params() {
89-
return describeRoute(router, pathname()).params
91+
return describeRoute(router, location().pathname).params
9092
},
9193
}
9294
}

‎packages/solid-router/tests/observe-navigation.test.tsx‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// declared to the attribution engine as the navigation, so the holds and
33
// re-runs it causes are named after the route and the record spans from
44
// the history change that started the load; the route the document arrived
5-
// on is declared when the provider establishes its initial match. `OBSERVE`
6-
// is defined on the dev build the tests resolve; in production it is
7-
// undefined and the declarations fold out.
5+
// on is declared when the Transitioner establishes the initial match.
6+
// `OBSERVE` is defined on the dev build the tests resolve; in production it
7+
// is undefined and the declarations fold out.
88
import {
99
cleanup,
1010
fireEvent,
@@ -95,7 +95,7 @@ function makeRouter(loaderMs: number, initialEntry = '/') {
9595
test('mounting declares the route the document arrived on — the first record, initial', async () => {
9696
const router = makeRouter(30, '/users/42?tab=posts')
9797
render(() => <RouterProvider router={router} />)
98-
// Delivered as the provider established its initial match: nothing to
98+
// Delivered as the Transitioner established the initial match: nothing to
9999
// wait for, so the record settled before the loader ran.
100100
expect(attribution.history('navigation')).toHaveLength(1)
101101
const nav = attribution.history('navigation')[0]!
@@ -118,10 +118,12 @@ test('mounting declares the route the document arrived on — the first record,
118118
})
119119

120120
test.each([false, true])(
121-
'an arrival the router canonicalizes is the one initial record, not a navigation (loaded before mount: %s)',
121+
'an arrival the router canonicalizes is the one initial record, naming the canonical location (loaded before mount: %s)',
122122
async (preloaded) => {
123123
// `/about` validates to `/about?tab=info`; the Transitioner commits the
124-
// canonical location once mounted. That write is the arrival's.
124+
// canonical location as it establishes the initial match. That write is
125+
// the arrival's, as `@solidjs/router`'s normalization of an empty
126+
// arrival is.
125127
const router = makeRouter(0, '/about')
126128
if (preloaded) await router.load()
127129
render(() => <RouterProvider router={router} />)
@@ -136,6 +138,7 @@ test.each([false, true])(
136138
expect(navs).toHaveLength(1)
137139
expect(navs[0]!.initial).toBe(true)
138140
expect(navs[0]!.name).toBe('/about')
141+
expect(navs[0]!.to).toBe('/about?tab=info')
139142

140143
// The next navigation is dated from its own request, not the arrival's.
141144
const requested = performance.now()
@@ -144,6 +147,7 @@ test.each([false, true])(
144147
await sleep(0)
145148
expect(navigations()).toHaveLength(1)
146149
expect(navigations()[0]!.name).toBe('/')
150+
expect(navigations()[0]!.from).toBe('/about?tab=info')
147151
expect(navigations()[0]!.at).toBeGreaterThanOrEqual(requested)
148152
},
149153
)

0 commit comments

Comments
 (0)