Skip to content

Commit dca0722

Browse files
authored
fix(loading): allow preloading after a cancelled revalidation (#108)
1 parent 1c4c9ab commit dca0722

3 files changed

Lines changed: 40 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
- Cancel a preload superseded by a forced load so its late failure cannot evict fresh data or follow an obsolete redirect.
2424

25+
- Allow preloading a cached route again after navigating away during revalidation.
26+
2527
### CI
2628

2729
- Start workflow lint on changes to workflows, action manifests, or lint inputs. Manual CI continues to run it.

‎src/router.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,17 @@ export function createRouter<
164164
if (current?.status !== "success") {
165165
return;
166166
}
167+
// A superseded revalidation can leave usable data behind, but its signal is spent.
168+
const cached = current.abortController.signal.aborted
169+
? { ...current, abortController: new AbortController(), isFetching: false as const }
170+
: current;
167171
matches.setCached([
168-
...matches.getState().cachedMatches.filter((match) => match.id !== current.id),
169-
current,
172+
...matches.getState().cachedMatches.filter((match) => match.id !== cached.id),
173+
cached,
170174
]);
171175
const route = compiled.byId.get(current.routeId);
172176
if (route) {
173-
loading.scheduleGc(current, route);
177+
loading.scheduleGc(cached, route);
174178
}
175179
};
176180

‎test/router-loading.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,37 @@ describe("router loading", () => {
324324
});
325325
});
326326

327+
it("preloads a cached route after its revalidation was cancelled", async () => {
328+
const refresh = deferred<string>();
329+
const router = createRouter({
330+
routes: [
331+
{
332+
id: "a",
333+
path: "/a",
334+
component: () => "a",
335+
loader: (context: string, { signal }) => {
336+
signal.throwIfAborted();
337+
return context === "refresh" ? refresh.promise : context;
338+
},
339+
},
340+
{ id: "b", path: "/b", component: () => "b" },
341+
],
342+
});
343+
344+
await router.navigate("a", "seed");
345+
const refreshing = router.revalidate("refresh");
346+
await router.navigate("b", "away");
347+
expect(router.getState().cachedMatches[0]?.isFetching).toBe(false);
348+
await router.preloadRoute("a", "replacement");
349+
refresh.resolve("obsolete");
350+
await refreshing;
351+
352+
expect(router.getState().cachedMatches).toMatchObject([
353+
{ routeId: "a", status: "success", data: "replacement", isFetching: false },
354+
]);
355+
router.stop();
356+
});
357+
327358
it("refetches a named route even when its cached match is fresh", async () => {
328359
const loader = vi.fn((context: string, { cause }: { cause: RouteLoadCause }) => ({
329360
context,

0 commit comments

Comments
 (0)