File tree 3 files changed +26
-1
lines changed
3 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -281,6 +281,7 @@ export default defineConfig({
281
281
items : [
282
282
{ text : "概述" , link : "/browser/http/introduct" } ,
283
283
{ text : "跨域" , link : "/browser/http/crossorigin" } ,
284
+ { text : "async defer" , link : "/browser/http/async-defer" } ,
284
285
] ,
285
286
} ,
286
287
] ,
Original file line number Diff line number Diff line change
1
+ ---
2
+ layout : doc
3
+ outline : deep
4
+ ---
5
+ ::: raw
6
+ 当浏览器加载html遇到` <script></script> ` ,必须立即执行该脚本,如果是外部脚本` <script src=""></script> ` ,等待下载完再执行
7
+
8
+ 所以可以把脚本放在页面底部
9
+
10
+ 但是对于长的html,只有下载了完整的html才会注意到该脚本,然后开始下载它,对于低速网络,会造成明显的延迟。
11
+
12
+ 幸运的是,这里有两个特性可以为我们解决这个问题:defer 和 async。(仅用于外部脚本)
13
+ :::
14
+ ## defer
15
+ 脚本将在后台下载,不阻塞页面。等待dom解析完毕,但在 ` DOMContentLoaded ` 事件之前执行
16
+ ::: tip DOMContentLoaded
17
+ ::: raw
18
+ 当 HTML 文档已完全解析且所有延迟脚本(` <script defer src="…" />和<script type="module" /> ` )已下载并执行时,将触发此事件
19
+
20
+ :::
21
+ 具有defer特性的脚本按照其相对顺序执行
22
+
23
+ ## async
24
+ 也不会阻塞页面,async脚本不会等待其他脚本,async脚本可能在 ` DOMContentLoaded ` 之前或之后执行
Original file line number Diff line number Diff line change @@ -40,7 +40,7 @@ HTTP2允许在单个tcp内处理多个请求和响应,这是通过将信息包
40
40
有几种模型:短期连接、持久连接和HTTP 流水线
41
41
42
42
后面两种是1.1新引入的
43
- <img src =" https://developer.mozilla.org/en-US/docs/Web/HTTP/…on_management_in_HTTP_1 .x/http1_x_connections.png " alt =" 比较三种 HTTP/1.x 连接模型的性能:短暂连接、持久连接和 HTTP 流水线。 " width =" 1012 " height =" 670 " loading =" lazy " >
43
+ <img src =" https://developer.mozilla.org/en-US/docs/Web/HTTP/Connection_management_in_HTTP_1 .x/http1_x_connections.png " alt =" 比较三种 HTTP/1.x 连接模型的性能:短暂连接、持久连接和 HTTP 流水线。 " width =" 1012 " height =" 670 " loading =" lazy " >
44
44
45
45
### 短暂连接(1.0默认模型)
46
46
HTTP/1.0 默认为每一对 HTTP 请求/响应都打开一个单独的 TCP 连接,通过设置connection除了close之外任何值会变成持久化
You can’t perform that action at this time.
0 commit comments