Skip to content

Commit b3e9489

Browse files
authored
docs: add some examples for prefetch / preload (#2302)
1 parent e7c67f4 commit b3e9489

File tree

4 files changed

+230
-40
lines changed

4 files changed

+230
-40
lines changed

website/docs/en/config/performance/prefetch.mdx

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,80 @@ interface PrefetchOption {
1616

1717
- **Default:** `undefined`
1818

19-
Configure the [`<link rel="prefetch">`](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/prefetch) tag for the static assets generated by Rsbuild.
19+
Inject the [`<link rel="prefetch">`](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/prefetch) tags for the static assets generated by Rsbuild.
2020

2121
## What is prefetch
2222

2323
The prefetch keyword for the rel attribute of the `<link>` element provides a hint to browsers that the user is likely to need the target resource for future navigation, and therefore the browser can likely improve the user experience by preemptively fetching and caching the resource.
2424

25-
## Boolean Type
25+
## Enable prefetch
2626

27-
When setting `performance.prefetch` to `true`, Rsbuild will prefetch resources according to the following configuration:
27+
When `performance.prefetch` is set to `true`, Rsbuild will use the following default options to prefetch resources. This means prefetching all asynchronous resources on the current page, including asynchronous JS and its associated CSS, image, font, and other resources.
2828

2929
```js
30-
{
30+
const defaultOptions = {
3131
type: 'async-chunks',
32-
}
32+
};
33+
```
34+
35+
For example, if you dynamically import other modules in the entry file:
36+
37+
```js title="index.js"
38+
import('./foo');
39+
import('./bar');
40+
```
41+
42+
The tags injected in HTML are as follows:
43+
44+
```html
45+
<html>
46+
<head>
47+
<title>Rsbuild App</title>
48+
<script defer src="/static/js/index.js"></script>
49+
<!-- Generated prefetch tags -->
50+
<link href="/static/js/async/src_bar_js.js" rel="prefetch" />
51+
<link href="/static/js/async/src_foo_js.js" rel="prefetch" />
52+
</head>
53+
</html>
54+
```
55+
56+
## Inject Manually
57+
58+
The `performance.prefetch` can only inject the prefetch tags for static resources generated by Rsbuild. If you need to prefetch other resources, you can manually add tags through [html.tags](/config/html/tags) :
59+
60+
```js title="rsbuild.config.ts"
61+
export default {
62+
html: {
63+
tags: [
64+
{
65+
tag: 'link',
66+
attrs: {
67+
rel: 'prefetch',
68+
href: 'https://example.com/some-script.js',
69+
},
70+
},
71+
],
72+
},
73+
};
74+
```
75+
76+
The injected HTML tag is as follows:
77+
78+
```html
79+
<link rel="prefetch" href="https://example.com/some-script.js" />
3380
```
3481

35-
## Object Type
82+
## Options
3683

37-
When the value of `performance.prefetch` is `object` type, the Rsbuild will enable the prefetch capability for the specified resource according to the current configuration.
84+
When the value of `performance.prefetch` is `object` type, the Rsbuild will enable the prefetch capability for the specified resource according to the current options.
3885

3986
### prefetch.type
4087

4188
The `type` field controls which resources will be pre-fetched, and supports secondary filtering of specified resources through `include` and `exclude`.
4289

4390
Currently supported resource types are as follows:
4491

45-
- `async-chunks`: prefetch all asynchronous resources (on the current page), including asynchronous js and its associated css, image, font and other resources.
92+
- `async-chunks`: prefetch all asynchronous resources (on the current page), including asynchronous JS and its associated CSS, image, font and other resources.
4693
- `initial`: prefetch all non-async resources (on the current page). It should be noted that if the current script has been added to the html template, no additional pre-fetching will be performed.
4794
- `all-chunks`: prefetch all resources (on the current page), including all asynchronous and non-asynchronous resources.
4895
- `all-assets`: prefetch all resources, and resources of other pages will be included in the MPA scenario.

website/docs/en/config/performance/preload.mdx

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,83 @@ interface PreloadOption {
1616

1717
- **Default:** `undefined`
1818

19-
Configure the [`<link rel="preload">`](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/preload) tag for the static assets generated by Rsbuild.
19+
Inject the [`<link rel="preload">`](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/preload) tags for the static assets generated by Rsbuild.
2020

2121
## What is preload
2222

2323
The preload value of the `<link>` element's rel attribute lets you declare fetch requests in the HTML's `<head>`, specifying resources that your page will need very soon, which you want to start loading early in the page lifecycle, before browsers' main rendering machinery kicks in.
2424

2525
This ensures they are available earlier and are less likely to block the page's render, improving performance. Even though the name contains the term load, it doesn't load and execute the script but only schedules it to be downloaded and cached with a higher priority.
2626

27-
## Boolean Type
27+
## Enable preload
2828

29-
When setting `performance.preload` to `true`, Rsbuild will preload resources according to the following configuration:
29+
When `performance.preload` is set to `true`, Rsbuild will use the following default options to preload resources. This means preloading all asynchronous resources on the current page, including asynchronous JS and its associated CSS, image, font, and other resources.
3030

3131
```js
32-
{
32+
const defaultOptions = {
3333
type: 'async-chunks',
34-
}
34+
};
35+
```
36+
37+
For example, if you dynamically import other modules in the entry file:
38+
39+
```js title="index.js"
40+
import('./foo');
41+
import('./bar');
42+
```
43+
44+
The tags injected in HTML are as follows:
45+
46+
```html
47+
<html>
48+
<head>
49+
<title>Rsbuild App</title>
50+
<script defer src="/static/js/index.js"></script>
51+
<!-- Generated preload tags -->
52+
<link href="/static/js/async/src_bar_js.js" rel="preload" as="script" />
53+
<link href="/static/js/async/src_foo_js.js" rel="preload" as="script" />
54+
</head>
55+
</html>
56+
```
57+
58+
## Inject Manually
59+
60+
The `performance.preload` can only inject the preload tags for static resources generated by Rsbuild. If you need to preload other resources, you can manually add tags through [html.tags](/config/html/tags) :
61+
62+
```js title="rsbuild.config.ts"
63+
export default {
64+
html: {
65+
tags: [
66+
{
67+
tag: 'link',
68+
attrs: {
69+
rel: 'preload',
70+
as: 'script',
71+
href: 'https://example.com/some-script.js',
72+
},
73+
},
74+
],
75+
},
76+
};
77+
```
78+
79+
The injected HTML tag is as follows:
80+
81+
```html
82+
<link rel="preload" as="script" href="https://example.com/some-script.js" />
3583
```
3684

37-
## Object Type
85+
## Options
3886

39-
When the value of `performance.preload` is `object` type, the Rsbuild will enable the preload capability for the specified resource according to the current configuration.
87+
When the value of `performance.preload` is `object` type, the Rsbuild will enable the preload capability for the specified resource according to the current options.
4088

4189
### preload.type
4290

4391
The `type` field controls which resources will be pre-fetched, and supports secondary filtering of specified resources through `include` and `exclude`.
4492

4593
Currently supported resource types are as follows:
4694

47-
- `async-chunks`: preload all asynchronous resources (on the current page), including asynchronous js and its associated css, image, font and other resources.
95+
- `async-chunks`: preload all asynchronous resources (on the current page), including asynchronous JS and its associated CSS, image, font and other resources.
4896
- `initial`: preload all non-async resources (on the current page). It should be noted that if the current script has been added to the html template, no additional pre-fetching will be performed.
4997
- `all-chunks`: preload all resources (on the current page), including all asynchronous and non-asynchronous resources.
5098
- `all-assets`: preload all resources, and resources of other pages will be included in the MPA scenario.

website/docs/zh/config/performance/prefetch.mdx

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,87 @@ interface PrefetchOption {
1616

1717
- **默认值:** `undefined`
1818

19-
为 Rsbuild 构建生成的静态资源配置 [`<link rel="prefetch">`](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Attributes/rel/prefetch) 标签。
19+
为 Rsbuild 构建生成的静态资源注入 [`<link rel="prefetch">`](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Attributes/rel/prefetch) 标签。
2020

2121
## 什么是 prefetch
2222

2323
`<link>` 元素的 rel 属性中的 prefetch 关键字是为了提示浏览器,用户未来的浏览有可能需要加载目标资源,所以浏览器有可能通过事先获取和缓存对应资源,优化用户体验。
2424

25-
## Boolean 类型
25+
## 启用 prefetch
2626

27-
当设置 `performance.prefetch``true`,Rsbuild 将根据如下配置对资源进行预获取:
27+
当设置 `performance.prefetch``true`,Rsbuild 将使用以下默认选项,对资源进行预获取,这表示 prefetch 当前页面的所有异步资源,包含异步 JS 及其关联的 CSS、image、font 等资源。
2828

2929
```js
30-
{
30+
const defaultOptions = {
3131
type: 'async-chunks',
32-
}
32+
};
33+
```
34+
35+
比如,在入口文件中动态引入了其他模块:
36+
37+
```js title="index.js"
38+
import('./foo');
39+
import('./bar');
40+
```
41+
42+
在 HTML 中注入的标签为:
43+
44+
```html
45+
<html>
46+
<head>
47+
<title>Rsbuild App</title>
48+
<script defer src="/static/js/index.js"></script>
49+
<!-- 生成的 prefetch 标签 -->
50+
<link href="/static/js/async/src_bar_js.js" rel="prefetch" />
51+
<link href="/static/js/async/src_foo_js.js" rel="prefetch" />
52+
</head>
53+
</html>
54+
```
55+
56+
## 手动注入
57+
58+
`performance.prefetch` 只能为 Rsbuild 构建生成的静态资源注入 prefetch 标签,如果你需要 prefetch 其他资源,可以通过 [html.tags](/config/html/tags) 手动添加标签:
59+
60+
```js title="rsbuild.config.ts"
61+
export default {
62+
html: {
63+
tags: [
64+
{
65+
tag: 'link',
66+
attrs: {
67+
rel: 'prefetch',
68+
href: 'https://example.com/some-script.js',
69+
},
70+
},
71+
],
72+
},
73+
};
74+
```
75+
76+
注入的 HTML 标签如下:
77+
78+
```html
79+
<link rel="prefetch" href="https://example.com/some-script.js" />
3380
```
3481

35-
## Object 类型
82+
## 选项
3683

37-
`performance.prefetch` 的值为 object 类型时,Rsbuild 会根据当前配置对指定资源开启 prefetch 能力。
84+
`performance.prefetch` 的值为 object 类型时,Rsbuild 会根据当前选项对指定资源开启 prefetch 能力。
3885

3986
### prefetch.type
4087

4188
`type` 字段控制了哪些资源会被预获取,同时支持通过 `include``exclude` 对指定资源进行二次过滤。
4289

4390
目前支持的资源类型如下:
4491

45-
- `async-chunks`: 预获取所有异步资源(当前页面),包含异步 JS 及其关联的 CSS、image、font 等资源。
46-
- `initial`: 预获取所有非异步资源(当前页面)。需要注意的是,如果当前脚本已经被添加到 HTML 模版中,则不会进行额外的预获取。
47-
- `all-chunks`: 预获取所有资源(当前页面),包含所有异步和非异步资源。
48-
- `all-assets`: 预获取所有资源,MPA 场景下会包含其他页面的资源。
92+
- `async-chunks`: prefetch 所有异步资源(当前页面),包含异步 JS 及其关联的 CSS、image、font 等资源。
93+
- `initial`: prefetch 所有非异步资源(当前页面)。需要注意的是,如果当前脚本已经被添加到 HTML 模版中,则不会进行额外的预获取。
94+
- `all-chunks`: prefetch 所有资源(当前页面),包含所有异步和非异步资源。
95+
- `all-assets`: prefetch 所有资源,MPA 场景下会包含其他页面的资源。
4996

5097
### 示例
5198

52-
当你希望对当前页面中所有 png 格式的图片资源进行预获取时,可以配置如下:
99+
当你希望对当前页面中所有 png 格式的图片资源进行 prefetch 时,可以配置如下:
53100

54101
```js
55102
export default {

website/docs/zh/config/performance/preload.mdx

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,90 @@ interface PreloadOption {
1616

1717
- **默认值:** `undefined`
1818

19-
为 Rsbuild 构建生成的静态资源配置 [`<link rel="preload">`](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Attributes/rel/preload) 标签。
19+
为 Rsbuild 构建生成的静态资源注入 [`<link rel="preload">`](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Attributes/rel/preload) 标签。
2020

2121
## 什么是 preload
2222

2323
`<link>` 元素的 rel 属性的 preload 值允许你在 HTML 的 `<head>` 中声明获取请求,指定页面很快就需要的资源,这些资源是你希望在页面生命周期的早期就开始加载的,早于浏览器的主要渲染机制启动。
2424

2525
这可以确保它们更早可用,并且不太可能阻塞页面的渲染,从而提高性能。尽管名称中包含 "load" 一词,但它并不加载和执行脚本,而只是安排脚本以更高的优先级进行下载和缓存。
2626

27-
## Boolean 类型
27+
## 启用 preload
2828

29-
当设置 `performance.preload``true`,Rsbuild 将根据如下配置对资源进行预加载:
29+
当设置 `performance.preload``true`,Rsbuild 将使用以下默认选项,对资源进行预获取,这表示 preload 当前页面的所有异步资源,包含异步 JS 及其关联的 CSS、image、font 等资源。
3030

3131
```js
32-
{
32+
const defaultOptions = {
3333
type: 'async-chunks',
34-
}
34+
};
35+
```
36+
37+
比如,在入口文件中动态引入了其他模块:
38+
39+
```js title="index.js"
40+
import('./foo');
41+
import('./bar');
42+
```
43+
44+
在 HTML 中注入的标签为:
45+
46+
```html
47+
<html>
48+
<head>
49+
<title>Rsbuild App</title>
50+
<script defer src="/static/js/index.js"></script>
51+
<!-- 生成的 preload 标签 -->
52+
<link href="/static/js/async/src_bar_js.js" rel="preload" as="script" />
53+
<link href="/static/js/async/src_foo_js.js" rel="preload" as="script" />
54+
</head>
55+
</html>
56+
```
57+
58+
## 手动注入
59+
60+
`performance.preload` 只能为 Rsbuild 构建生成的静态资源注入 preload 标签,如果你需要 preload 其他资源,可以通过 [html.tags](/config/html/tags) 手动添加标签:
61+
62+
```js title="rsbuild.config.ts"
63+
export default {
64+
html: {
65+
tags: [
66+
{
67+
tag: 'link',
68+
attrs: {
69+
rel: 'preload',
70+
as: 'script',
71+
href: 'https://example.com/some-script.js',
72+
},
73+
},
74+
],
75+
},
76+
};
77+
```
78+
79+
注入的 HTML 标签如下:
80+
81+
```html
82+
<link rel="preload" as="script" href="https://example.com/some-script.js" />
3583
```
3684

37-
## Object 类型
85+
## 选项
3886

39-
`performance.preload` 的值为 object 类型时,Rsbuild 会根据当前配置对指定资源开启 preload 能力。
87+
`performance.preload` 的值为 object 类型时,Rsbuild 会根据当前选项对指定资源开启 preload 能力。
4088

4189
### preload.type
4290

4391
`type` 字段控制了哪些资源会被预加载,同时支持通过 `include``exclude` 对指定资源进行二次过滤。
4492

4593
目前支持的资源类型如下:
4694

47-
- `async-chunks`: 预加载所有异步资源(当前页面),包含异步 JS 及其关联的 CSS、image、font 等资源。
48-
- `initial`: 预加载所有非异步资源(当前页面)。需要注意的是,如果当前脚本已经被添加到 HTML 模版中,则不会进行额外的预加载。
49-
- `all-chunks`: 预加载所有资源(当前页面),包含所有异步和非异步资源。
50-
- `all-assets`: 预加载所有资源,MPA 场景下会包含其他页面的资源。
95+
- `async-chunks`: preload 所有异步资源(当前页面),包含异步 JS 及其关联的 CSS、image、font 等资源。
96+
- `initial`: preload 所有非异步资源(当前页面)。需要注意的是,如果当前脚本已经被添加到 HTML 模版中,则不会进行额外的预加载。
97+
- `all-chunks`: preload 所有资源(当前页面),包含所有异步和非异步资源。
98+
- `all-assets`: preload 所有资源,MPA 场景下会包含其他页面的资源。
5199

52100
### 示例
53101

54-
当你希望对当前页面中所有 png 格式的图片资源进行预加载时,可以配置如下:
102+
当你希望对当前页面中所有 png 格式的图片资源进行 preload 时,可以配置如下:
55103

56104
```js
57105
export default {

0 commit comments

Comments
 (0)