Skip to content

Commit 32163a9

Browse files
authored
fix(web): a stylesheet removed after its grace releases the gate waiting on it (#4442)
acquireAsset's release removes the element 100 ms later. A mount inside that grace (a useHead stylesheet under <Loading>) warms the same registry entry, finds the element still connected and still loading, and gates on its load. When the grace ran out first, the element was removed before it loaded; a removed link fires neither load nor error, so the gate waited forever and the boundary stayed on its fallback. trackAssetLoad keeps the load's settle on the entry, and the removal settles a pending load the way an error does. The gate's retry warms the sheet again on a new entry and waits on the new element. The same race made served-error-no-reask.spec.tsx's stylesheet case fail locally whenever the [streamed] run warmed the sheet inside the grace of the [loaded] run's release.
1 parent 99d9821 commit 32163a9

3 files changed

Lines changed: 59 additions & 4 deletions

File tree

‎.changeset/asset-grace-rewarm.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/web": patch
3+
---
4+
5+
A stylesheet released while it is still loading no longer hangs a gate that warmed it again inside the removal grace. The release schedules the element's removal 100 ms later; a mount in between (a `useHead` stylesheet under `<Loading>`) warmed the same registry entry and waited on that element's load, and the removal took the element out before it loaded. A removed link fires neither load nor error, so the boundary stayed on its fallback. The removal now settles a pending load the way an error does, and the gate warms the sheet again on a new element and waits on that one.

‎packages/web/src/client.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,10 +1828,10 @@ function trackAssetLoad(entry, adopted) {
18281828
}
18291829
entry.loadState = "pending";
18301830
entry.loadPromise = new Promise(resolve => {
1831-
const settle = state => {
1831+
const settle = (entry.settle = state => {
18321832
entry.loadState = state;
18331833
resolve();
1834-
};
1834+
});
18351835
el.addEventListener("load", () => settle("loaded"), { once: true });
18361836
el.addEventListener("error", () => settle("errored"), { once: true });
18371837
});
@@ -1966,6 +1966,11 @@ export function acquireAsset(descriptor) {
19661966
entry.timer = setTimeout(() => {
19671967
assetRegistry.delete(key);
19681968
entry.element && entry.element.remove();
1969+
// A removed element fires neither load nor error, so a gate that
1970+
// warmed this entry inside the grace would wait on it forever. Release
1971+
// it the way an error does; its retry warms the sheet again, on a new
1972+
// entry.
1973+
if (entry.loadState === "pending") entry.settle("errored");
19691974
}, ASSET_REMOVAL_GRACE);
19701975
};
19711976
}

‎packages/web/test/use-head-css-gating-client.spec.tsx‎

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
// streaming's `$dfs` gate. jsdom never fetches, so load events are
1212
// dispatched manually and server-loaded sheets model `.sheet` directly —
1313
// same technique as the runtime's own head.spec.js.
14-
import { describe, expect, test } from "vitest";
14+
import { describe, expect, test, vi } from "vitest";
1515
import { createSignal, isPending, Loading, flush } from "solid-js";
16-
import { render, useHead } from "../src/index.js";
16+
import { acquireAsset, render, useHead } from "../src/index.js";
1717

1818
function Route(props: { name: string; css: string }) {
1919
useHead({ tag: "link", props: { rel: "stylesheet", href: props.css } });
@@ -187,6 +187,51 @@ describe("useHead stylesheet reveal gating — client integration", () => {
187187
dispose();
188188
});
189189

190+
test("a sheet released while loading and warmed again inside the removal grace still reveals", async () => {
191+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
192+
try {
193+
// An owner that acquires without the gate (a router, a hydration
194+
// commit) releases the sheet before it has loaded: the element stays
195+
// for the removal grace.
196+
const release = acquireAsset({ type: "style", href: "/gate-regrace.css" });
197+
release();
198+
199+
// A new mount inside the grace warms the same entry, still loading,
200+
// and gates on it.
201+
const div = document.createElement("div");
202+
const dispose = render(
203+
() => (
204+
<Loading fallback={<span>Loading...</span>}>
205+
<Route name="again" css="/gate-regrace.css" />
206+
</Loading>
207+
),
208+
div
209+
);
210+
flush();
211+
expect(div.innerHTML).toContain("Loading...");
212+
213+
// The grace runs out while it waits. The removed element never fires
214+
// load or error, so the gate must not keep waiting on it: it warms the
215+
// sheet again and holds on the new element.
216+
vi.advanceTimersByTime(100);
217+
await microtasks();
218+
flush();
219+
await microtasks();
220+
const link = headLink("/gate-regrace.css");
221+
expect(link).not.toBe(null);
222+
expect(link.getAttribute("rel")).toBe("preload");
223+
expect(div.innerHTML).toContain("Loading...");
224+
225+
await settleLoad("/gate-regrace.css");
226+
expect(div.innerHTML).toContain("<main>again</main>");
227+
expect(link.getAttribute("rel")).toBe("stylesheet");
228+
dispose();
229+
vi.runAllTimers();
230+
} finally {
231+
vi.useRealTimers();
232+
}
233+
});
234+
190235
test("adopted server-emitted sheet: counts as loaded, no re-fetch, no gate stall", async () => {
191236
// Hydration parity: by reveal time the server-emitted sheet is already
192237
// applied. jsdom never loads resources, so model the loaded state.

0 commit comments

Comments
 (0)