Skip to content

Commit c3ad063

Browse files
authored
chore: migrate to @tanstack/store 0.9.1 (#6691)
1 parent 965d95f commit c3ad063

File tree

7 files changed

+102
-47
lines changed

7 files changed

+102
-47
lines changed

packages/react-router/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
},
9898
"dependencies": {
9999
"@tanstack/history": "workspace:*",
100-
"@tanstack/react-store": "^0.8.0",
100+
"@tanstack/react-store": "^0.9.1",
101101
"@tanstack/router-core": "workspace:*",
102102
"isbot": "^5.1.22",
103103
"tiny-invariant": "^1.3.3",

packages/router-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
},
161161
"dependencies": {
162162
"@tanstack/history": "workspace:*",
163-
"@tanstack/store": "^0.8.0",
163+
"@tanstack/store": "^0.9.1",
164164
"cookie-es": "^2.0.0",
165165
"seroval": "^1.4.2",
166166
"seroval-plugins": "^1.4.2",

packages/router-core/src/router.ts

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,13 @@ export function getLocationChangeInfo(routerState: {
871871
return { fromLocation, toLocation, pathChanged, hrefChanged, hashChanged }
872872
}
873873

874+
function filterRedirectedCachedMatches<T extends { status: string }>(
875+
matches: Array<T>,
876+
): Array<T> {
877+
const filtered = matches.filter((d) => d.status !== 'redirected')
878+
return filtered.length === matches.length ? matches : filtered
879+
}
880+
874881
export type CreateRouterFn = <
875882
TRouteTree extends AnyRoute,
876883
TTrailingSlashOption extends TrailingSlashOption = 'never',
@@ -1123,16 +1130,7 @@ export class RouterCore<
11231130
getInitialRouterState(this.latestLocation),
11241131
) as unknown as Store<any>
11251132
} else {
1126-
this.__store = new Store(getInitialRouterState(this.latestLocation), {
1127-
onUpdate: () => {
1128-
this.__store.state = {
1129-
...this.state,
1130-
cachedMatches: this.state.cachedMatches.filter(
1131-
(d) => !['redirected'].includes(d.status),
1132-
),
1133-
}
1134-
},
1135-
})
1133+
this.__store = new Store(getInitialRouterState(this.latestLocation))
11361134

11371135
setupScrollRestoration(this)
11381136
}
@@ -1175,10 +1173,10 @@ export class RouterCore<
11751173
}
11761174

11771175
if (needsLocationUpdate && this.__store) {
1178-
this.__store.state = {
1179-
...this.state,
1176+
this.__store.setState((s) => ({
1177+
...s,
11801178
location: this.latestLocation,
1181-
}
1179+
}))
11821180
}
11831181

11841182
if (
@@ -2445,7 +2443,9 @@ export class RouterCore<
24452443
...s.cachedMatches,
24462444
...exitingMatches.filter(
24472445
(d) =>
2448-
d.status !== 'error' && d.status !== 'notFound',
2446+
d.status !== 'error' &&
2447+
d.status !== 'notFound' &&
2448+
d.status !== 'redirected',
24492449
),
24502450
],
24512451
}
@@ -2600,12 +2600,21 @@ export class RouterCore<
26002600
: ''
26012601

26022602
if (matchesKey) {
2603-
this.__store.setState((s) => ({
2604-
...s,
2605-
[matchesKey]: s[matchesKey]?.map((d) =>
2606-
d.id === id ? updater(d) : d,
2607-
),
2608-
}))
2603+
if (matchesKey === 'cachedMatches') {
2604+
this.__store.setState((s) => ({
2605+
...s,
2606+
cachedMatches: filterRedirectedCachedMatches(
2607+
s.cachedMatches.map((d) => (d.id === id ? updater(d) : d)),
2608+
),
2609+
}))
2610+
} else {
2611+
this.__store.setState((s) => ({
2612+
...s,
2613+
[matchesKey]: s[matchesKey]?.map((d) =>
2614+
d.id === id ? updater(d) : d,
2615+
),
2616+
}))
2617+
}
26092618
}
26102619
})
26112620
}

packages/router-core/tests/load.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,16 @@ describe('beforeLoad skip or exec', () => {
167167
beforeLoad,
168168
})
169169
await router.preloadRoute({ to: '/foo' })
170+
expect(
171+
router.state.cachedMatches.some((d) => d.status === 'redirected'),
172+
).toBe(false)
170173
await sleep(10)
171174
await router.navigate({ to: '/foo' })
172175

173176
expect(router.state.location.pathname).toBe('/foo')
177+
expect(
178+
router.state.cachedMatches.some((d) => d.status === 'redirected'),
179+
).toBe(false)
174180
expect(beforeLoad).toHaveBeenCalledTimes(2)
175181
})
176182

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

189198
expect(router.state.location.pathname).toBe('/foo')
199+
expect(
200+
router.state.cachedMatches.some((d) => d.status === 'redirected'),
201+
).toBe(false)
190202
expect(beforeLoad).toHaveBeenCalledTimes(2)
191203
})
192204

@@ -362,10 +374,16 @@ describe('loader skip or exec', () => {
362374
loader,
363375
})
364376
await router.preloadRoute({ to: '/foo' })
377+
expect(
378+
router.state.cachedMatches.some((d) => d.status === 'redirected'),
379+
).toBe(false)
365380
await sleep(10)
366381
await router.navigate({ to: '/foo' })
367382

368383
expect(router.state.location.pathname).toBe('/foo')
384+
expect(
385+
router.state.cachedMatches.some((d) => d.status === 'redirected'),
386+
).toBe(false)
369387
expect(loader).toHaveBeenCalledTimes(2)
370388
})
371389

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

384405
expect(router.state.location.pathname).toBe('/bar')
406+
expect(
407+
router.state.cachedMatches.some((d) => d.status === 'redirected'),
408+
).toBe(false)
385409
expect(loader).toHaveBeenCalledTimes(1)
386410
})
387411

412+
test('updateMatch removes redirected matches from cachedMatches', async () => {
413+
const loader = vi.fn()
414+
const router = setup({ loader })
415+
416+
await router.preloadRoute({ to: '/foo' })
417+
expect(router.state.cachedMatches).toEqual(
418+
expect.arrayContaining([expect.objectContaining({ id: '/foo/foo' })]),
419+
)
420+
421+
router.updateMatch('/foo/foo', (prev) => ({
422+
...prev,
423+
status: 'redirected',
424+
}))
425+
426+
expect(router.state.cachedMatches.some((d) => d.id === '/foo/foo')).toBe(
427+
false,
428+
)
429+
expect(
430+
router.state.cachedMatches.some((d) => d.status === 'redirected'),
431+
).toBe(false)
432+
})
433+
388434
test('exec if rejected preload (error)', async () => {
389435
const loader = vi.fn<Loader>(async ({ preload }) => {
390436
if (preload) throw new Error('error')

packages/solid-router/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
"@solidjs/meta": "^0.29.4",
107107
"@tanstack/history": "workspace:*",
108108
"@tanstack/router-core": "workspace:*",
109-
"@tanstack/solid-store": "^0.8.0",
109+
"@tanstack/solid-store": "^0.9.1",
110110
"isbot": "^5.1.22",
111111
"tiny-invariant": "^1.3.3",
112112
"tiny-warning": "^1.0.3"

packages/vue-router/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"dependencies": {
7373
"@tanstack/history": "workspace:*",
7474
"@tanstack/router-core": "workspace:*",
75-
"@tanstack/vue-store": "^0.8.0",
75+
"@tanstack/vue-store": "^0.9.1",
7676
"@vue/runtime-dom": "^3.5.25",
7777
"isbot": "^5.1.22",
7878
"jsesc": "^3.0.2",

pnpm-lock.yaml

Lines changed: 23 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)