Skip to content

Commit 89209fc

Browse files
committed
fix: incorrect output of preload tag if "crossorigin: true", #139
1 parent 533a26e commit 89209fc

17 files changed

Lines changed: 171 additions & 22 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 4.12.1 (2025-01-12)
4+
5+
- fix: incorrect output of preload tag if "crossorigin: true", #139
6+
- fix: if `as=font` is used in preload and the `crossorigin` is not defined,
7+
it will be added automatically, because the `crossorigin` is mandatory for `font` type
8+
39
## 4.12.0 (2025-01-12)
410

511
- feat(release): add support for the `?inline` query to load assets as data URL

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2495,6 +2495,17 @@ The descriptions of the properties:
24952495
e.g. `attributes: { crossorigin: true }`, `attributes: { media: '(max-width: 900px)' }`.\
24962496
Defaults `{}`.
24972497
2498+
> [!NOTE]
2499+
>
2500+
> The `true` value of a property will be rendered as an attribute w/o a value.
2501+
>
2502+
> For example, the `attributes: { crossorigin: true }` will be rendered to:
2503+
> ```html
2504+
> <link rel="preload" ... crossorigin />
2505+
> ```
2506+
>
2507+
> Setting the [crossorigin](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin) attribute to an empty value, like `crossorigin` or `crossorigin=""`, is the same as `anonymous`.
2508+
24982509
If you define the `attributes` than you can write the `as`, `rel` and `type` properties in the `attributes`.
24992510
25002511
For example:
@@ -2592,7 +2603,9 @@ preload: [
25922603
25932604
> ℹ️ **Note**
25942605
>
2595-
> Font preloading requires the `crossorigin` attribute to be set.
2606+
> Font preloading [requires](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/preload#cors-enabled_fetches) the `crossorigin` attribute to be set.\
2607+
> If the `crossorigin` property is not defined, it will be added for the `font` type automatically.
2608+
>
25962609
> See [font preload](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/preload#what_types_of_content_can_be_preloaded).
25972610
25982611
#### Preload tags order
@@ -5229,7 +5242,7 @@ module.exports = {
52295242
preload: [
52305243
{
52315244
test: /\.(woff2|woff)$/,
5232-
attributes: { as: 'font', crossorigin: true },
5245+
attributes: { as: 'font' },
52335246
},
52345247
],
52355248
}),
@@ -5266,8 +5279,8 @@ The generated HTML contains the preload tag with the font:
52665279
<head>
52675280
<title>Demo</title>
52685281
<!-- preload fonts detected in style -->
5269-
<link rel="preload" href="fonts/myfont.woff2" as="font" type="font/woff2" crossorigin="true" />
5270-
<link rel="preload" href="fonts/myfont.woff" as="font" type="font/woff" crossorigin="true" />
5282+
<link rel="preload" href="fonts/myfont.woff2" as="font" type="font/woff2" crossorigin />
5283+
<link rel="preload" href="fonts/myfont.woff" as="font" type="font/woff" crossorigin />
52715284
<!-- compiled style -->
52725285
<link href="css/style.1f4faaff.css" rel="stylesheet" />
52735286
</head>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html-bundler-webpack-plugin",
3-
"version": "4.12.0",
3+
"version": "4.12.1",
44
"description": "Generates complete single-page or multi-page website from source assets. Build-in support for Markdown, Eta, EJS, Handlebars, Nunjucks, Pug. Alternative to html-webpack-plugin.",
55
"keywords": [
66
"html",

src/Plugin/Preload.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ class Preload {
120120
if (conf.rel) attrs.rel = conf.rel;
121121
if (conf.type) attrs.type = conf.type;
122122

123+
// for the `font` type, the `crossorigin` attribute is mandatory, if it is not defined, set to default value
124+
if (as === 'font' && !('crossorigin' in attrs)) attrs.crossorigin = true;
125+
123126
// whether the 'type' property exist regardless of a value;
124127
// if the property exists and have the undefined value, exclude this attribute in generating preload tag
125128
const hasType = 'type' in conf || (conf.attributes && 'type' in conf.attributes) || optionalTypeBy.has(attrs.as);
@@ -179,7 +182,8 @@ class Preload {
179182

180183
let tag = `<link`;
181184
for (const [key, value] of Object.entries(attrs)) {
182-
if (value != null) tag += ` ${key}="${value}"`;
185+
if (value === true) tag += ` ${key}`;
186+
else if (value != null) tag += ` ${key}="${value}"`;
183187
}
184188
tag += '>';
185189

test/cases/option-preload-attributes/expected/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
<head>
44
<title>Home</title>
55
<link rel="preload" href="assets/css/home.bundle.css" as="style">
6-
<link rel="preload" href="assets/fonts/open-sans-regular.woff2" as="font" type="font/woff2" crossorigin="true">
7-
<link rel="preload" href="assets/fonts/MaterialIcons-Regular.woff2" as="font" type="font/woff2" crossorigin="true">
6+
<link rel="preload" href="assets/fonts/open-sans-regular.woff2" as="font" type="font/woff2" crossorigin="">
7+
<link rel="preload" href="assets/fonts/MaterialIcons-Regular.woff2" as="font" type="font/woff2" crossorigin="">
88
<link href="assets/css/home.bundle.css" rel="stylesheet">
99
</head>
1010
<body>

test/cases/option-preload-attributes/webpack.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ module.exports = {
3131
},
3232
{
3333
test: /\.(eot|ttf|woff2?)$/,
34-
attributes: { as: 'font', crossorigin: true }, // test `as` in attributes
34+
// test: `as` in attributes
35+
// test: the empty string `''` value of `crossorigin` should be rendered as `crossorigin=""`
36+
attributes: { as: 'font', crossorigin: '' },
3537
},
3638
],
3739
}),

test/cases/option-preload-font/expected/assets/css/home.bundle.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Home</title>
5+
<link rel="preload" href="assets/css/home.bundle.css" as="style">
6+
<link rel="preload" href="assets/fonts/open-sans-regular.woff2" as="font" type="font/woff2" crossorigin>
7+
<link rel="preload" href="assets/fonts/MaterialIcons-Regular.woff2" as="font" type="font/woff2" crossorigin>
8+
<link href="assets/css/home.bundle.css" rel="stylesheet">
9+
</head>
10+
<body>
11+
<h1>Home</h1>
12+
<div>
13+
<span class="mi">home</span>
14+
<span class="mi">settings</span>
15+
</div>
16+
</body>
17+
</html>

0 commit comments

Comments
 (0)