Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 13 additions & 1 deletion src/bundler/HTMLScanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
}
}

pub(crate) const TAG_HANDLERS: [TagHandler; 16] = [
pub(crate) const TAG_HANDLERS: [TagHandler; 20] = [
// Module scripts with src
TagHandler::new("script[src]", "src", ImportKind::Stmt),
// CSS Stylesheets
Expand All @@ -207,6 +207,18 @@
),
// Web Workers
TagHandler::new("link[as='worker'][href]", "href", ImportKind::Stmt),
// Preloaded scripts (<link rel="preload" as="script">)
TagHandler::new("link[as='script'][href]", "href", ImportKind::Stmt),
// Module preloads (<link rel="modulepreload">)
TagHandler::new("link[rel='modulepreload'][href]", "href", ImportKind::Stmt),

Check failure on line 213 in src/bundler/HTMLScanner.rs

View check run for this annotation

Claude / Claude Code Review

preload as=script / modulepreload → ImportKind::Stmt eagerly executes fetch-only hints

Mapping `link[as='script']` and `link[rel='modulepreload']` to `ImportKind::Stmt` bundles the target into the entry chunk and executes it on page load, but per the HTML spec these are fetch-only hints — the browser never runs them without a separate `<script>`/`import()`. The new test demonstrates the divergence: `preloaded.js` has no `<script src>` in the fixture, yet its `console.log` ships in the executed entry bundle; any page that preloads a script for later dynamic injection now runs it ea
Comment thread
robobun marked this conversation as resolved.
Resolved
// Generic preload/prefetch targets (fetch/track/document/embed/object)
TagHandler::new(
"link[as='fetch'][href], link[as='track'][href], link[as='document'][href], link[as='embed'][href], link[as='object'][href]",
"href",
ImportKind::Url,
),
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
// Prefetch hints (<link rel="prefetch">)
TagHandler::new("link[rel='prefetch'][href]", "href", ImportKind::Url),

Check failure on line 221 in src/bundler/HTMLScanner.rs

View check run for this annotation

Claude / Claude Code Review

Blanket rel=prefetch selector breaks navigation prefetch and eagerly executes prefetched JS/CSS

The blanket `link[rel='prefetch'][href]` selector captures navigation prefetches — the primary real-world use of `rel=prefetch` — not just hashed assets. `<link rel="prefetch" href="/dashboard">` now fails the build with "Could not resolve", and `<link rel="prefetch" href="./page2.js">` (or `.css`) is bundled into the current page's entry chunk and **executed on load** (the hint tag is silently removed), violating prefetch's fetch-only semantics. Consider gating on `link[rel='prefetch'][as][href

Check warning on line 221 in src/bundler/HTMLScanner.rs

View check run for this annotation

Claude / Claude Code Review

Overlapping link selectors fire multiple handlers on one element

Nit: some of the new selectors overlap, so one `<link>` can match two `TAG_HANDLERS` and produce two ImportRecords — e.g. `<link rel="modulepreload" as="script" href=...>` (spec-valid; Vite emits it) matches both `link[as='script']` and `link[rel='modulepreload']`, and `<link rel="prefetch" as="image" href=...>` matches both `link[as='image']` and `link[rel='prefetch']`. Scan/rewrite stay index-aligned and the rewrite pass keys on the resolved loader (not `ImportKind`), so output is still correc
Comment thread
robobun marked this conversation as resolved.
Resolved
Comment thread
robobun marked this conversation as resolved.
Resolved
// Manifest files
TagHandler::new("link[rel='manifest'][href]", "href", ImportKind::Url),
// Icons
Expand Down
71 changes: 71 additions & 0 deletions test/bundler/bundler_html.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1011,4 +1011,75 @@ body {
expect(htmlContent).toMatch(/href=".*\.webmanifest"/);
},
});

// <link rel="prefetch">, <link rel="modulepreload"> and <link rel="preload"> with
// as={script,fetch,track,document,embed,object} must not ship dangling source-path
// hrefs. Assets are emitted + hashed; JS preloads are folded into the entry chunk.
itBundled("html/link-preload-prefetch", {
outdir: "out/",
files: {
"/index.html": `
<!DOCTYPE html>
<html>
<head>
<link rel="prefetch" href="./hero.png">
<link rel="modulepreload" href="./mod.js">
<link rel="preload" as="script" href="./preloaded.js">
<link rel="preload" as="fetch" href="./data.json" crossorigin>
<link rel="preload" as="track" href="./captions.vtt">
</head>
<body>
<img src="./hero.png">
<script type="module" src="./mod.js"></script>
</body>
</html>`,
"/mod.js": `console.log("mod");`,
"/preloaded.js": `console.log("preloaded");`,
"/hero.png": "PNG",
"/data.json": `{"k":1}`,
"/captions.vtt": "WEBVTT",
},
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
entryPoints: ["/index.html"],
onAfterBundle(api) {
const html = api.readFile("out/index.html");

// No raw source paths should survive in the output; every one of these
// would have been a guaranteed 404 before.
expect(html).not.toContain('href="./hero.png"');
expect(html).not.toContain('href="./mod.js"');
expect(html).not.toContain('href="./preloaded.js"');
expect(html).not.toContain('href="./data.json"');
expect(html).not.toContain('href="./captions.vtt"');

// prefetch: asset is copied + hashed and the href is rewritten.
expect(html).toMatch(/<link rel="prefetch" href="[^"]*hero-[a-z0-9]+\.png">/);

// preload as=fetch / as=track: assets are copied + hashed and the href is rewritten.
expect(html).toMatch(/<link rel="preload" as="fetch" href="[^"]*data-[a-z0-9]+\.json" crossorigin>/);
expect(html).toMatch(/<link rel="preload" as="track" href="[^"]*captions-[a-z0-9]+\.vtt">/);

// modulepreload / preload as=script: the referenced module is bundled into the
// entry chunk, so the hint tag is dropped (there is no separate file to preload).
expect(html).not.toMatch(/rel="modulepreload"/);
expect(html).not.toMatch(/as="script"/);

// The emitted files exist and carry the original bytes.
const hero = html.match(/href="(?:\.\/|\/)?(hero-[a-z0-9]+\.png)"/);
const data = html.match(/href="(?:\.\/|\/)?(data-[a-z0-9]+\.json)"/);
const vtt = html.match(/href="(?:\.\/|\/)?(captions-[a-z0-9]+\.vtt)"/);
expect(hero).not.toBeNull();
expect(data).not.toBeNull();
expect(vtt).not.toBeNull();
api.expectFile("out/" + hero![1]).toBe("PNG");
api.expectFile("out/" + data![1]).toBe(`{"k":1}`);
api.expectFile("out/" + vtt![1]).toBe("WEBVTT");

// Both preloaded scripts end up in the single entry JS chunk.
const js = html.match(/src="(?:\.\/|\/)?(index-[a-z0-9]+\.js)"/);
expect(js).not.toBeNull();
const jsContent = api.readFile("out/" + js![1]);
expect(jsContent).toContain('"mod"');
expect(jsContent).toContain('"preloaded"');
},
});
});
Loading