Skip to content

Commit 245228c

Browse files
ryansolidclaudecursoragent
committed
fix(solid-router): give observe navigations the location's path, search and hash
`to` and `from` on a navigation's NavigationRef, the initial record's `to`, and the server render's RenderEvent.route.to were the location's pathname, so `/users/42?tab=posts#bio` and `/users/42` were one destination. Solid Router gives the location as the router holds it: pathname + search + hash on the client (browser history's `get()`, `memoryHistory`'s entry, and the navigate target `url.pathname + url.search + url.hash`), pathname + search on the server (`staticIntegration`: a request carries no hash). TanStack's ParsedLocation.href is exactly that shape (pathname + stringified search + `#hash`), in the router's own path space, the one `name` (the route's fullPath) is in, before a basepath or rewrite is applied (`publicHref`). Use it for all three. Co-authored-by: Claude <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 8bbb754 commit 245228c

5 files changed

Lines changed: 39 additions & 7 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
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 pathname, 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, 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.
66

77
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.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/solid-router': patch
3+
---
4+
5+
Observe tier (Solid's dev and observe builds only): a navigation's `to` and `from`, the initial record's `to`, and a server render's `RenderEvent.route.to` are the location's path, search and hash (`/users/42?tab=posts#bio`) rather than its pathname, as `@solidjs/router` gives them — its `href`, in the router's own path space, the one `name` is in (before a basepath or rewrite). A request carries no hash, so a render's `to` is the path and search. The name stays the route pattern the pathname matches. Nothing changes in production, where `OBSERVE` is undefined.

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import type { AnyRouter } from '@tanstack/router-core'
55
* What the router says about its navigations to Solid's observe tier
66
* (`OBSERVE.attribution.withOrigin`; `OBSERVE` is defined on the dev and
77
* observe builds, undefined in production). A navigation is named by the
8-
* route pattern its pathname matches, so occurrences fold together.
8+
* route pattern its pathname matches, so occurrences fold together; `to` and
9+
* `from` are the location's `href` — path, search and hash, as
10+
* `@solidjs/router` gives them, in the router's own path space (`name`'s,
11+
* before a basepath or rewrite is applied).
912
*/
1013

1114
/** When a navigation was requested, and the interaction it was requested in. */
@@ -51,8 +54,8 @@ export function describeNavigation(
5154
const ref: NavigationRef = {
5255
kind: 'navigation',
5356
...describeRoute(router, to.pathname),
54-
to: to.pathname,
55-
from: from.pathname,
57+
to: to.href,
58+
from: from.href,
5659
}
5760
if (request) {
5861
ref.at = request.at
@@ -77,7 +80,7 @@ export function describeInitial(router: AnyRouter): NavigationRef {
7780
kind: 'navigation',
7881
initial: true,
7982
get to() {
80-
return pathname()
83+
return router.latestLocation.href
8184
},
8285
get name() {
8386
return describeRoute(router, pathname()).name

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ test('mounting declares the route the document arrived on — the first record,
101101
const nav = attribution.history('navigation')[0]!
102102
expect(nav.initial).toBe(true)
103103
expect(nav.name).toBe('/users/$id')
104-
expect(nav.to).toBe('/users/42')
104+
expect(nav.to).toBe('/users/42?tab=posts')
105105
expect(nav.params).toEqual({ id: '42' })
106106
expect(nav.from).toBeUndefined()
107107
expect(nav.interaction).toBeUndefined()
@@ -191,6 +191,28 @@ test('the match publish is declared as the navigation, dated from the history ch
191191
expect(nav.settledMs!).toBeGreaterThanOrEqual(30)
192192
})
193193

194+
test('`to` and `from` are the path, search and hash, as `@solidjs/router` gives them', async () => {
195+
const router = makeRouter(0)
196+
render(() => <RouterProvider router={router} />)
197+
await waitFor(() => expect(screen.getByTestId('home')).toBeTruthy())
198+
199+
await router.navigate({
200+
to: '/users/$id',
201+
params: { id: '42' },
202+
search: { tab: 'posts' } as any,
203+
hash: 'bio',
204+
})
205+
await waitFor(() => expect(screen.getByTestId('user')).toBeTruthy())
206+
await router.navigate({ to: '/about' })
207+
await waitFor(() => expect(screen.getByTestId('about')).toBeTruthy())
208+
await sleep(0)
209+
210+
expect(navigations().map((nav) => [nav.name, nav.to, nav.from])).toEqual([
211+
['/users/$id', '/users/42?tab=posts#bio', '/'],
212+
['/about', '/about?tab=info', '/users/42?tab=posts#bio'],
213+
])
214+
})
215+
194216
test('a navigation superseded before it published leaves one record for the destination that showed', async () => {
195217
const router = makeRouter(30)
196218
render(() => <RouterProvider router={router} />)

‎packages/solid-router/tests/server/observe-render-route.test.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ describe("observe tier, server: RenderEvent.route from the provider's initial ma
7171
))
7272
expect(html).toContain('user 7')
7373
expect(seen).toHaveLength(1)
74+
// The path and search, as `@solidjs/router` names a render (a request
75+
// carries no hash).
7476
expect(seen[0]!.route).toEqual({
7577
name: '/users/$id',
76-
to: '/users/7',
78+
to: '/users/7?tab=posts',
7779
params: { id: '7' },
7880
})
7981
},

0 commit comments

Comments
 (0)