From 49e99f00181dc97740868cea5c4563ae212966ad Mon Sep 17 00:00:00 2001 From: Roel Bruggink Date: Tue, 17 Nov 2026 21:12:51 +0100 Subject: [PATCH] fix(query): stamp preload entry adoption once (#739) A navigate read that adopts a preload-intent cache entry re-stamped cached[0] on every read and published it into the entry version signal, so subscribers re-ran, read again, and re-ran again (ABANDONED_FLIGHTS). Adopt the entry once and stamp that adoption; later reads of the same promise write the same value. --- .changeset/preload-entry-adoption-version.md | 5 + src/data/query.ts | 6 ++ test/query-preload-adoption-rerun.spec.tsx | 99 ++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 .changeset/preload-entry-adoption-version.md create mode 100644 test/query-preload-adoption-rerun.spec.tsx diff --git a/.changeset/preload-entry-adoption-version.md b/.changeset/preload-entry-adoption-version.md new file mode 100644 index 00000000..c979f9d6 --- /dev/null +++ b/.changeset/preload-entry-adoption-version.md @@ -0,0 +1,5 @@ +--- +"@solidjs/router": patch +--- + +Fix `query()` re-running subscribers when it adopts a preload cache entry: stamp the entry's version once at adoption instead of on every navigate read (#739). diff --git a/src/data/query.ts b/src/data/query.ts index 16b47e11..a8e6f78d 100644 --- a/src/data/query.ts +++ b/src/data/query.ts @@ -253,6 +253,12 @@ export function query any>(fn: T, name: string): Cac } if (cached[3] === "preload" && intent !== "preload") { cached[0] = now; + cached[3] = intent; + // Adopting a preload entry reuses the same promise. Stamp that adoption + // once: later navigate reads write this same value, so they are no-ops. + // Re-stamping cached[0] on every read made each subscriber re-run write a + // new version and re-run the others (#739). + !isServer && intent === "navigate" && cached[4][1](cached[0]); } let res = cached[1]; if (intent !== "preload") { diff --git a/test/query-preload-adoption-rerun.spec.tsx b/test/query-preload-adoption-rerun.spec.tsx new file mode 100644 index 00000000..d82a6587 --- /dev/null +++ b/test/query-preload-adoption-rerun.spec.tsx @@ -0,0 +1,99 @@ +// @vitest-environment jsdom +// #739: navigating into a preloaded route re-runs the destination's async +// computations. +// +// A hover preload reads the route's `query()` with intent "preload", so the cache +// entry is stamped `cached[3] = "preload"`. The navigation that follows reads the +// same entry with intent "navigate" and takes the reuse branch in +// `src/data/query.ts`: `cached[0] = now` runs on every read, and that stamp is +// published into the entry's version signal. The subscribed async memo re-runs, +// reads again (new `now`), and re-runs again. +// +// The loop ends when two reads land in the same millisecond, so the clock is +// pinned here: +1ms per `Date.now()` call, frozen after 10 calls. +import { createMemo, Loading } from "solid-js"; +import { render } from "@solidjs/web"; +import { vi } from "vitest"; +import { createRouter, memoryHistory, query, useNavigate, usePreloadRoute } from "../src/index.js"; + +const wait = (ms: number) => new Promise(r => setTimeout(r, ms)); + +describe("query() preload entry adoption (#739)", () => { + const originalScrollTo = window.scrollTo; + beforeEach(() => { + window.scrollTo = vi.fn(); + }); + afterAll(() => { + window.scrollTo = originalScrollTo; + }); + + test("navigation into a preloaded route runs the async memo once", async () => { + const realNow = Date.now; + let tick = 1_000_000; + const clock = vi + .spyOn(Date, "now") + .mockImplementation(() => (tick = Math.min(tick + 1, 1_000_010))); + + let runs = 0; + const getValue = query(async () => { + await wait(50); + return 42; + }, "preload-adoption-value"); + + function Page() { + const value = createMemo(async () => { + runs++; + return await getValue(); + }); + return ( + loading

}> +

value: {value()}

+
+ ); + } + + let open!: () => void; + function Home() { + const preloadRoute = usePreloadRoute(); + const navigate = useNavigate(); + open = () => { + // What `preloadLinks: intentPreload()` does on pointer dwell. + preloadRoute("/page/", { preloadData: true }); + navigate("/page/"); + }; + return

Home

; + } + + const Router = createRouter({ + routes: [ + { path: "/", component: Home }, + { path: "/page/", component: Page, preload: () => void getValue() } + ], + history: memoryHistory("/") + }); + + const div = document.createElement("div"); + document.body.appendChild(div); + const dispose = render(() => , div); + try { + for (let i = 0; i < 20 && !div.textContent?.includes("Home"); i++) await wait(10); + expect(div.textContent).toContain("Home"); + + open(); + + for (let i = 0; i < 100; i++) { + await wait(20); + if (div.textContent?.includes("value: 42")) break; + } + expect(div.textContent).toContain("value: 42"); + + // Let queued re-runs start. + await wait(300); + expect(runs).toBe(1); + } finally { + dispose(); + div.remove(); + clock.mockRestore(); + } + }, 20000); +});