Skip to content
Closed
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
5 changes: 5 additions & 0 deletions .changeset/preload-entry-adoption-version.md
Original file line number Diff line number Diff line change
@@ -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).
6 changes: 6 additions & 0 deletions src/data/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ export function query<T extends (...args: any) => 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") {
Expand Down
99 changes: 99 additions & 0 deletions test/query-preload-adoption-rerun.spec.tsx
Original file line number Diff line number Diff line change
@@ -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 fallback={<p>loading</p>}>
<p>value: {value()}</p>
</Loading>
);
}

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 <h1>Home</h1>;
}

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(() => <Router />, 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);
});