Skip to content

Commit 0d78d79

Browse files
ryansolidclaudecursoragent
committed
fix(solid-router): record a redirect while the first page loads as a navigation
A client-rendered arrival that redirects (a `beforeLoad` or loader throwing `redirect()`) published its destination with no resolved location to navigate from, so describeNavigation declared nothing and the destination had no record: the initial record named the route the user never saw, and nothing named the one they did. Solid Router records it as a navigation of its own: the arrival's frame has closed, so the redirect's location write is not a hop of a pending navigation (`navigationDepth` is 0) and is declared from the arrival (`from: "/files?x=1"`, no interaction, no `redirects`). Until the first publish resolves a location, navigate from the arrival the initial record names. A publish with no request behind it (the initial load itself) still declares nothing. Co-authored-by: Claude <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent abce6f2 commit 0d78d79

4 files changed

Lines changed: 59 additions & 17 deletions

File tree

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 redirect while the first page loads — a client-rendered arrival whose `beforeLoad` or loader throws `redirect()` — is recorded as a navigation from the arrival to where the redirect sent it, after the initial record that names the arrival, as `@solidjs/router` records one. It had no record: nothing was shown yet to navigate from. Nothing changes in production, where `OBSERVE` is undefined.

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +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.
48+
// The location the initial declaration names, canonical: what a navigation
49+
// is from until the first publish resolves one (a redirect while it loads).
4950
let arrival: ParsedLocation | undefined
5051

5152
// Ack when the commit's transition settles (the atomic swap), not when the
@@ -69,9 +70,13 @@ export function Transitioner() {
6970
const answered = request
7071
request = undefined
7172
const ref =
72-
answered === ARRIVAL
73+
answered === undefined || answered === ARRIVAL
7374
? undefined
74-
: describeNavigation(router, answered)
75+
: describeNavigation(
76+
router,
77+
answered,
78+
router.stores.resolvedLocation.get() ?? arrival,
79+
)
7580
if (ref !== undefined) {
7681
const observe = Solid.OBSERVE
7782
publish = () => observe.attribution.withOrigin(ref, fn)

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,28 @@ function describeRoute(
3737
}
3838

3939
/**
40-
* The navigation a match publish lands: the latest location, from the one
41-
* shown. Nothing when nothing is shown yet (the initial load — declared by
42-
* `describeInitial`) or the publish reloads what is shown. `request` dates it
43-
* from the history change and joins it to the interaction that asked, both
44-
* gone by the time the loaders resolve and the publish runs; the key's
45-
* presence declares the interaction, `undefined` included.
40+
* The navigation a match publish lands: the latest location, from `from`
41+
* (the location shown, or the arrival while the first page is still
42+
* loading). Nothing when the publish reloads the location it came from.
43+
* `request` dates it from the history change and joins it to the interaction
44+
* that asked, both gone by the time the loaders resolve and the publish
45+
* runs; the key's presence declares the interaction, `undefined` included.
4646
*/
4747
export function describeNavigation(
4848
router: AnyRouter,
49-
request: NavigationRequest | undefined,
49+
request: NavigationRequest,
50+
from: ParsedLocation | undefined,
5051
): NavigationRef | undefined {
5152
const to = router.latestLocation
52-
const from = router.stores.resolvedLocation.get()
5353
if (!from || from.href === to.href) return
54-
const ref: NavigationRef = {
54+
return {
5555
kind: 'navigation',
5656
...describeRoute(router, to.pathname),
5757
to: to.href,
5858
from: from.href,
59+
at: request.at,
60+
interaction: request.interaction,
5961
}
60-
if (request) {
61-
ref.at = request.at
62-
ref.interaction = request.interaction
63-
}
64-
return ref
6562
}
6663

6764
/**

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
createRootRoute,
2525
createRoute,
2626
createRouter,
27+
redirect,
2728
} from '../src'
2829

2930
beforeEach(() => attribution.enable({ log: false }))
@@ -80,11 +81,26 @@ function makeRouter(loaderMs: number, initialEntry = '/') {
8081
},
8182
component: () => <div data-testid="post">Post</div>,
8283
})
84+
const dashboardRoute = createRoute({
85+
getParentRoute: () => rootRoute,
86+
path: '/dashboard',
87+
beforeLoad: () => {
88+
throw redirect({ to: '/login' })
89+
},
90+
component: () => <div data-testid="dashboard">Dashboard</div>,
91+
})
92+
const loginRoute = createRoute({
93+
getParentRoute: () => rootRoute,
94+
path: '/login',
95+
component: () => <div data-testid="login">Login</div>,
96+
})
8397
const routeTree = rootRoute.addChildren([
8498
indexRoute,
8599
aboutRoute,
86100
userRoute,
87101
postRoute,
102+
dashboardRoute,
103+
loginRoute,
88104
])
89105
return createRouter({
90106
routeTree,
@@ -152,6 +168,25 @@ test.each([false, true])(
152168
},
153169
)
154170

171+
test('a redirect while the first page loads is a navigation from the arrival', async () => {
172+
// As `@solidjs/router` records one: the arrival is declared and settled,
173+
// so the redirect is a navigation of its own, from it, not a hop.
174+
const router = makeRouter(0, '/dashboard')
175+
render(() => <RouterProvider router={router} />)
176+
await waitFor(() => expect(screen.getByTestId('login')).toBeTruthy())
177+
await sleep(0)
178+
179+
const navs = attribution.history('navigation')
180+
expect(navs.map((nav) => [nav.initial, nav.name, nav.to, nav.from])).toEqual([
181+
[true, '/dashboard', '/dashboard', undefined],
182+
[undefined, '/login', '/login', '/dashboard'],
183+
])
184+
// The guard redirected on its own: no interaction asked for it.
185+
expect(navs[1]!.interaction).toBeUndefined()
186+
expect(navs[1]!.redirects).toBeUndefined()
187+
expect(navs[1]!.outcome).toBe('committed')
188+
})
189+
155190
test('the initial declaration is not a row in feedback().navigations', async () => {
156191
const router = makeRouter(0)
157192
render(() => <RouterProvider router={router} />)

0 commit comments

Comments
 (0)