<html lang="en">
<head>
<link rel="stylesheet" href="css/style.css" type="text/css"></link>
</head>
<body>
<div><img src="photo.jpg"></div>
<script src="js/app.js"></script>
</body>
</html>
What are the critical resources, critical path length, total size for the above example?
- 3 number of critical resources
- 11 total critical KB
- 2 minimum critical path length(roundtrips)
Tip 1. Minify, compress, cache (for HTML, CSS, JavaScript)
- (1) Minimize bytes
- (2) Reduce critical resources
- (3) Shortten CRP length
Tip 2. Minimize use of render blocking resources (for CSS)
(1) Use Media queries on link to unblock rensering
- printer.css
@media print {
...
}
- HTML
<link rel="stylesheet" media="print" href="printer.css" />
(2) Use inline CSS
Tip 3. Minimize use of parser blocking resources (for JavaScript)
We can use async, defer, and module to avoid blocking.
The following figure shows the difference between the async and the defer. The order is async/blocking, HTML parsing, defer.
In the following code, the order should be test.js, inline script, inline module, test2.js.
//3
<script type="module">
addTextToBody("Inline module executed");
</script>
//1
<script src="test1.js"></script>
//4
<script defer src="test2.js"></script>
//2
<script defer>
console.log("Inline script executed");
</script>
(1) Defer JavaScript execution
- The browser renders and downloads JavaScript files at the same time. (non-blocking)
- The browser will execute JavaScript code after rendering.
- The inline script will ignore defer. For the following code, For the following code, the order should be inline script, test.js
<script defer src="test.js"></script>
The following optimized template is the code I found from the internet with dns-prefetch, preload, and defer.
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<link rel="dns-prefetch" href="//cdn1.com/">
<link rel="preload" href="//js.cdn1.com/test.js" as="script">
</head>
<body>
<script type="text/javascript" src="//js.cdn1.com/test.js" defer></script>
</body>
</html>
(2) Use async attribute on script
- The browser renders and downloads JavaScript files at the same time. (non-blocking)
- The browser will execute JavaScript code before rendering.
<script async src="test.js"></script>
Articles
There are some of my articles and released projects. Feel free to check if you like!
- My blog-posts for software developing
- Facebook page
- My web resume
- Twitter bot
- Side project - Daily Learning
Top comments (0)