Expand rel's that trigger a link's onload

Was only "stylesheet", not also includes "preload" and "modulepreload"
This commit is contained in:
Karl Seguin
2026-03-18 07:43:54 +08:00
parent 8372b45cc5
commit 3c29e7dbd4
2 changed files with 34 additions and 5 deletions

View File

@@ -84,3 +84,24 @@
testing.eventually(() => testing.expectEqual(true, result)); testing.eventually(() => testing.expectEqual(true, result));
} }
</script> </script>
<script id="refs">
{
const rels = ['stylesheet', 'preload', 'modulepreload'];
const results = rels.map(() => false);
rels.forEach((rel, i) => {
let link = document.createElement('link')
link.rel = rel;
link.href = '/nope';
link.onload = () => results[i] = true;
document.documentElement.appendChild(link);
});
testing.eventually(() => {
results.forEach((r) => {
testing.expectEqual(true, r);
});
});
}
</script>

View File

@@ -93,13 +93,21 @@ pub fn linkAddedCallback(self: *Link, page: *Page) !void {
} }
const element = self.asElement(); const element = self.asElement();
// Exit if rel not set.
const rel = element.getAttributeSafe(comptime .wrap("rel")) orelse return; const rel = element.getAttributeSafe(comptime .wrap("rel")) orelse return;
// Exit if rel is not stylesheet. const loadable_rels = std.StaticStringMap(void).initComptime(.{
if (!std.mem.eql(u8, rel, "stylesheet")) return; .{ "stylesheet", {} },
// Exit if href not set. .{ "preload", {} },
.{ "modulepreload", {} },
});
if (loadable_rels.has(rel) == false) {
return;
}
const href = element.getAttributeSafe(comptime .wrap("href")) orelse return; const href = element.getAttributeSafe(comptime .wrap("href")) orelse return;
if (href.len == 0) return; if (href.len == 0) {
return;
}
try page._to_load.append(page.arena, self._proto); try page._to_load.append(page.arena, self._proto);
} }