Skip to content
Merged
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/hydration-trusts-server-link-state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/router": patch
---

Link claims trust the server's link state at hydration: an anchor the server marked is registered without resolving its URL, and is resolved on the first routing change. Anchors with an authored `aria-current`, and pages rendered without server link state, are resolved at claim as before.
10 changes: 7 additions & 3 deletions src/claims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ export function setupLinkClaims(
if (existing) return refresh(a, existing);
// server HTML (hydrating, or already in the document) pairs the router's
// `aria-current` with `data-active` and never writes over an authored one
const hydrating = serverState && isHydrating();
const rec = {
owned:
serverState &&
(isHydrating() || a.isConnected) &&
(hydrating || (serverState && a.isConnected)) &&
a.getAttribute("aria-current") === "page" &&
a.hasAttribute("data-active")
};
Expand All @@ -170,7 +170,11 @@ export function setupLinkClaims(
registry.add(a);
onCleanup(() => registry.delete(a));
}
refresh(a, rec);
// A hydrating anchor already carries the server's state for the location
// hydrating now, unless the server skipped it for an authored
// `aria-current`. Skipping the refresh keeps URL resolution off the load
// path; the sweep resolves it on the first routing change.
if (!hydrating || (!rec.owned && a.getAttribute("aria-current") !== null)) refresh(a, rec);
})
);
}
Expand Down
13 changes: 13 additions & 0 deletions test/server/hydration-link-state.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,20 @@ describe("link state across hydration", () => {
for (const [text, value] of Object.entries(expected))
expect([text, state(anchor(document, text))]).toEqual([text, value]);

// anchors resolve against the document; record which ones hydration parses
const resolved: string[] = [];
const NativeURL = window.URL;
window.URL = new Proxy(NativeURL, {
construct(target, args) {
if (args[1] === document.baseURI) resolved.push(args[0]);
return new target(...args);
}
});
window.eval(code);
window.URL = NativeURL;
// the server's state is trusted: only the anchor it skipped for an
// authored aria-current is resolved at hydration
expect(resolved).toEqual(["/about"]);
const app = window.hydrationTest;
// the boundary's anchor streams in after hydration starts
stream.release();
Expand Down
Loading