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/template-anchor-url.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/router": patch
---

Link preloading and click handling resolve an anchor's `href` against `document.baseURI`. An anchor cloned from a `<template>` keeps its raw `href` (e.g. `/about`) until it is adopted into the page, so `eagerPreload`'s idle flush threw `Invalid URL` when it reached one that a transition still held off the page (#704).
3 changes: 2 additions & 1 deletion src/data/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ export function setupNativeEvents({
const rel = (a.getAttribute("rel") || "").split(/\s+/);
if (a.hasAttribute("download") || rel.includes("external")) return;

const url = svg ? new URL(href, document.baseURI) : new URL(href);
// a clone from a `<template>` keeps a raw `href` until it is adopted (#704)
const url = new URL(href, document.baseURI);
// Skip non-http(s) schemes (blob:, mailto:, tel:, data:, ...). blob: URLs
// inherit the page origin, so the origin check below won't reject them. #382
if (url.protocol !== "https:" && url.protocol !== "http:") return;
Expand Down
11 changes: 11 additions & 0 deletions test/data/events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1105,5 +1105,16 @@ describe("tap and ambient preload strategies", () => {
vi.runAllTimers();
expect(preloaded()).toEqual(["/b:false"]);
});

test("an anchor cloned from a template preloads before it is adopted (#704)", () => {
mount(eagerPreload({ all: true }));
const t = document.createElement("template");
t.innerHTML = '<a href="/a">a</a>';
const a = t.content.firstChild!.cloneNode(true) as HTMLAnchorElement;
const disposeLink = createRoot(d => (claimElement(a), d));
expect(() => vi.runAllTimers()).not.toThrow();
expect(preloaded()).toEqual(["/a:false"]);
disposeLink();
});
});
});
Loading