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
6 changes: 4 additions & 2 deletions src/bundler/HTMLScanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,10 @@
TagHandler::new("script[src]", "src", ImportKind::Stmt),
// CSS Stylesheets
TagHandler::new("link[rel='stylesheet'][href]", "href", ImportKind::At),
// CSS Assets
TagHandler::new("link[as='style'][href]", "href", ImportKind::At),
// CSS preloads — `<link rel="preload" as="style">` is a fetch hint, not an
// applied stylesheet. Emit the file standalone and rewrite the href (same
// as font/image preloads) instead of merging it into the page's CSS chunk.
Comment thread
robobun marked this conversation as resolved.
Resolved
TagHandler::new("link[as='style'][href]", "href", ImportKind::Url),

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

View check run for this annotation

Claude / Claude Code Review

Preloaded CSS emitted as raw copy — @import/url() dependencies are dropped

Forcing `link[as='style']` through `Loader::File` emits the preloaded CSS as a raw byte copy without ever entering the CSS parser, so any `@import` / `url()` / `@font-face` inside it is left verbatim and its target is never scanned or emitted — runtime 404s. Before this change those dependencies were resolved (mis-timed, but present); now the async-CSS pattern the PR targets breaks as soon as the preloaded stylesheet references a font or image, which is the common case. The preloaded CSS needs t
Comment thread
robobun marked this conversation as resolved.
Resolved
// Font files
TagHandler::new(
"link[as='font'][href], link[type^='font/'][href]",
Expand Down
1 change: 0 additions & 1 deletion src/bundler/bundle_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6355,13 +6355,12 @@
// so that `shouldCopyForBundling()` returns true and the asset is emitted.
// Only do this for HTML sources — CSS url() imports should retain their original behavior.
if loader == Loader::Html
&& import_record.kind == ImportKind::Url
&& !resolved_loader.should_copy_for_bundling()
&& !resolved_loader.is_javascript_like()
&& !resolved_loader.is_css()
&& resolved_loader != Loader::Html
{
break 'brk Loader::File;

Check failure on line 6363 in src/bundler/bundle_v2.rs

View check run for this annotation

Claude / Claude Code Review

Same CSS file as both preload and stylesheet loses CSS processing

When the **same** `.css` file is referenced by both `<link rel="preload" as="style">` and `<link rel="stylesheet">` (e.g. the loadCSS / non-render-blocking pattern) and the preload appears first, the Url-kind record now wins the `resolve_queue.get_or_put(path.text)` dedup and — with the `!resolved_loader.is_css()` guard removed — forces `Loader::File`. The subsequent At-kind record hits `found_existing` and never upgrades the loader, so the file is copied raw: `@import`/`url()` inside it are not
Comment thread
robobun marked this conversation as resolved.
Resolved
}
break 'brk resolved_loader;
};
Expand Down
77 changes: 77 additions & 0 deletions test/bundler/bundler_html.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1011,4 +1011,81 @@ body {
expect(htmlContent).toMatch(/href=".*\.webmanifest"/);
},
});

// <link rel="preload" as="style"> is a fetch-only hint; it must NOT be merged
// into the page's applied stylesheet. It should be emitted as a standalone
// asset with its href rewritten, like as="font"/as="image" preloads.
itBundled("html/preload-as-style", {
outdir: "out/",
files: {
"/index.html": `
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="./applied.css">
<link rel="preload" as="style" href="./later.css">
</head>
<body>
<script src="./a.js"></script>
</body>
</html>`,
"/applied.css": "h1 { font-weight: bold; }",
"/later.css": "body { color: red; }",
"/a.js": "console.log(1)",
},
entryPoints: ["/index.html"],
onAfterBundle(api) {
const html = api.readFile("out/index.html");

// The preload tag must survive with its href rewritten to a hashed filename.
const preload = html.match(
/<link rel="preload" as="style" href="(?:\.\/|\/)?(later-[a-zA-Z0-9]+\.css)">/,
);
expect(preload).not.toBeNull();

// The preloaded CSS must be emitted as its own file, not folded into the
// page bundle.
const laterContent = api.readFile("out/" + preload![1]);
expect(laterContent).toContain("color");
expect(laterContent).toContain("red");

// The applied stylesheet bundle must NOT contain the preloaded rules.
const stylesheet = html.match(
/<link rel="stylesheet"[^>]* href="(?:\.\/|\/)?([^"]+\.css)"/,
);
expect(stylesheet).not.toBeNull();
expect(stylesheet![1]).not.toBe(preload![1]);
const bundleContent = api.readFile("out/" + stylesheet![1]);
expect(bundleContent).toContain("font-weight");
expect(bundleContent).not.toContain("color");
expect(bundleContent).not.toContain("red");
},
});

// When there is no rel="stylesheet" on the page at all, a preload-as-style
// reference must not cause a rel="stylesheet" link to be injected.
itBundled("html/preload-as-style-only", {
outdir: "out/",
files: {
"/index.html": `
<!DOCTYPE html>
<html>
<head>
<link rel="preload" as="style" href="./later.css">
</head>
<body>
<script src="./a.js"></script>
</body>
</html>`,
"/later.css": "body { color: red; }",
"/a.js": "console.log(1)",
},
entryPoints: ["/index.html"],
onAfterBundle(api) {
const html = api.readFile("out/index.html");

expect(html).toMatch(/<link rel="preload" as="style" href="(?:\.\/|\/)?later-[a-zA-Z0-9]+\.css">/);
expect(html).not.toMatch(/rel="stylesheet"/);
},
});
});
Loading