Skip to content
Merged
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: 1 addition & 1 deletion packages/react-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
},
"dependencies": {
"@tanstack/history": "workspace:*",
"@tanstack/react-store": "^0.8.0",
"@tanstack/react-store": "^0.9.1",
"@tanstack/router-core": "workspace:*",
"isbot": "^5.1.22",
"tiny-invariant": "^1.3.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/router-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
},
"dependencies": {
"@tanstack/history": "workspace:*",
"@tanstack/store": "^0.8.0",
"@tanstack/store": "^0.9.1",
"cookie-es": "^2.0.0",
"seroval": "^1.4.2",
"seroval-plugins": "^1.4.2",
Expand Down
49 changes: 29 additions & 20 deletions packages/router-core/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,13 @@ export function getLocationChangeInfo(routerState: {
return { fromLocation, toLocation, pathChanged, hrefChanged, hashChanged }
}

function filterRedirectedCachedMatches<T extends { status: string }>(
matches: Array<T>,
): Array<T> {
const filtered = matches.filter((d) => d.status !== 'redirected')
return filtered.length === matches.length ? matches : filtered
}

export type CreateRouterFn = <
TRouteTree extends AnyRoute,
TTrailingSlashOption extends TrailingSlashOption = 'never',
Expand Down Expand Up @@ -1123,16 +1130,7 @@ export class RouterCore<
getInitialRouterState(this.latestLocation),
) as unknown as Store<any>
} else {
this.__store = new Store(getInitialRouterState(this.latestLocation), {
onUpdate: () => {
this.__store.state = {
...this.state,
cachedMatches: this.state.cachedMatches.filter(
(d) => !['redirected'].includes(d.status),
),
}
},
})
this.__store = new Store(getInitialRouterState(this.latestLocation))

setupScrollRestoration(this)
}
Expand Down Expand Up @@ -1175,10 +1173,10 @@ export class RouterCore<
}

if (needsLocationUpdate && this.__store) {
this.__store.state = {
...this.state,
this.__store.setState((s) => ({
...s,
location: this.latestLocation,
}
}))
}

if (
Expand Down Expand Up @@ -2445,7 +2443,9 @@ export class RouterCore<
...s.cachedMatches,
...exitingMatches.filter(
(d) =>
d.status !== 'error' && d.status !== 'notFound',
d.status !== 'error' &&
d.status !== 'notFound' &&
d.status !== 'redirected',
),
],
}
Expand Down Expand Up @@ -2600,12 +2600,21 @@ export class RouterCore<
: ''

if (matchesKey) {
this.__store.setState((s) => ({
...s,
[matchesKey]: s[matchesKey]?.map((d) =>
d.id === id ? updater(d) : d,
),
}))
if (matchesKey === 'cachedMatches') {
this.__store.setState((s) => ({
...s,
cachedMatches: filterRedirectedCachedMatches(
s.cachedMatches.map((d) => (d.id === id ? updater(d) : d)),
),
}))
} else {
this.__store.setState((s) => ({
...s,
[matchesKey]: s[matchesKey]?.map((d) =>
d.id === id ? updater(d) : d,
),
}))
}
}
})
}
Expand Down
46 changes: 46 additions & 0 deletions packages/router-core/tests/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,16 @@ describe('beforeLoad skip or exec', () => {
beforeLoad,
})
await router.preloadRoute({ to: '/foo' })
expect(
router.state.cachedMatches.some((d) => d.status === 'redirected'),
).toBe(false)
await sleep(10)
await router.navigate({ to: '/foo' })

expect(router.state.location.pathname).toBe('/foo')
expect(
router.state.cachedMatches.some((d) => d.status === 'redirected'),
).toBe(false)
expect(beforeLoad).toHaveBeenCalledTimes(2)
})

Expand All @@ -184,9 +190,15 @@ describe('beforeLoad skip or exec', () => {
})
router.preloadRoute({ to: '/foo' })
await Promise.resolve()
expect(
router.state.cachedMatches.some((d) => d.status === 'redirected'),
).toBe(false)
await router.navigate({ to: '/foo' })

expect(router.state.location.pathname).toBe('/foo')
expect(
router.state.cachedMatches.some((d) => d.status === 'redirected'),
).toBe(false)
expect(beforeLoad).toHaveBeenCalledTimes(2)
})

Expand Down Expand Up @@ -362,10 +374,16 @@ describe('loader skip or exec', () => {
loader,
})
await router.preloadRoute({ to: '/foo' })
expect(
router.state.cachedMatches.some((d) => d.status === 'redirected'),
).toBe(false)
await sleep(10)
await router.navigate({ to: '/foo' })

expect(router.state.location.pathname).toBe('/foo')
expect(
router.state.cachedMatches.some((d) => d.status === 'redirected'),
).toBe(false)
expect(loader).toHaveBeenCalledTimes(2)
})

Expand All @@ -379,12 +397,40 @@ describe('loader skip or exec', () => {
})
router.preloadRoute({ to: '/foo' })
await Promise.resolve()
expect(
router.state.cachedMatches.some((d) => d.status === 'redirected'),
).toBe(false)
await router.navigate({ to: '/foo' })

expect(router.state.location.pathname).toBe('/bar')
expect(
router.state.cachedMatches.some((d) => d.status === 'redirected'),
).toBe(false)
expect(loader).toHaveBeenCalledTimes(1)
})

test('updateMatch removes redirected matches from cachedMatches', async () => {
const loader = vi.fn()
const router = setup({ loader })

await router.preloadRoute({ to: '/foo' })
expect(router.state.cachedMatches).toEqual(
expect.arrayContaining([expect.objectContaining({ id: '/foo/foo' })]),
)

router.updateMatch('/foo/foo', (prev) => ({
...prev,
status: 'redirected',
}))

expect(router.state.cachedMatches.some((d) => d.id === '/foo/foo')).toBe(
false,
)
expect(
router.state.cachedMatches.some((d) => d.status === 'redirected'),
).toBe(false)
})

test('exec if rejected preload (error)', async () => {
const loader = vi.fn<Loader>(async ({ preload }) => {
if (preload) throw new Error('error')
Expand Down
2 changes: 1 addition & 1 deletion packages/solid-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"@solidjs/meta": "^0.29.4",
"@tanstack/history": "workspace:*",
"@tanstack/router-core": "workspace:*",
"@tanstack/solid-store": "^0.8.0",
"@tanstack/solid-store": "^0.9.1",
"isbot": "^5.1.22",
"tiny-invariant": "^1.3.3",
"tiny-warning": "^1.0.3"
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"dependencies": {
"@tanstack/history": "workspace:*",
"@tanstack/router-core": "workspace:*",
"@tanstack/vue-store": "^0.8.0",
"@tanstack/vue-store": "^0.9.1",
"@vue/runtime-dom": "^3.5.25",
"isbot": "^5.1.22",
"jsesc": "^3.0.2",
Expand Down
46 changes: 23 additions & 23 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.