At the present time, modern browsers like Chrome, Safari, or even Edge have built-in technologies to improve web performance significantly, helping to enhance the user experience on the website. right on the client side.
When it comes to improving web performance, you often think of complex caching techniques, will have to perform optimally on the server side, have to edit multiple source code in multiple layer layers, or many other modules. each other, to integrate the confusing caching handling into it. Sometimes the optimal solution is really simple, when you know about it, it turns out nothing is too big or complicated, especially the optimization on the client side: you just need to render 1 more. Some tags or metadata tags in the view layer are enough for the browser to understand and automatically optimize your web performance.
preload, prefetch, preconnect are actually instructions that tell the browser how to handle, rendering a tag have rel = "preload" (or prefetch, preconnect) that we will render in the view layer or via HTTP headers, their purpose is to inform the browser how to optimize the fetch of resources (images, js, css, font, ...) from the server, to reduce the time spent processing network round trip round trips, taking advantage of the time users are reading information on the page to prefetch resources of the website.
Preload
Preload (preloading in order of priority) is an indicator that gives you more control over the priority order of fetching specific resources in the browser, for the current navigation. The preload instruction is defined in 1 tag . Usually this instruction is used to preload important priority resources such as images, CSS, JS and font files, and want to be preloaded before the browser performs rendering. This ensures that resources are always loaded, and hardly blocks the rendering process of the page, thus improving page load performance.
Note that preload is completely different from prefetch, because it focuses on the user's current navigation, which preloads resources resources to be set with high priority, while prefetch concentrates. into prefetching resources for the next navigation the user is able to access (these resources have a lower priority). A special point to note here is that preload does not block the onload event.
Benefits of preload
The benefits of preload directives are as follows:
- Allows the browser to set the priority for loading resources.
- Gives the browser the ability to identify the resource type so that it can indicate whether the same resource can be reused in the future.
- The browser browser can determine whether request requests comply with the content privacy policy content security policy or not by reference to what is specified in the as attribute.
- The browser can include appropriate accept headers based on the resource type (such as image / webp).
How to use preload
Below is an example of how to preloading an image first.
Next is the preloading fonts preloading example. Note: if you pre-load links with resources in another domain, you need to add the crossorigin property.
Next is an example of preloading a stylesheet using a tag or dynamically generated by JS.
You can use attributes as
To instruct the browser to type the type of content that needs to be preloaded:
- Set resource priority sequences more accurately.
- Try to reuse the same resource for future requests if possible.
- Allow application of content privacy policy content security policyto resource.
- Set up properly
Accept
request headers when making requests to load resources.
There are many types of content types that can be preloaded, as well as many types of attribute values as
as follows:
audio
: Audio file.document
: 1 HTML document embedded insidegood
.
embed
: 1 resource embedded inside.
fetch
: resource type can be loaded by fetch or XHR request like ArrayBuffer or JSON file.font
: Font file.image
: Image file.object
: 1 resource embedded inside.
script
: JavaScript file.style
: Stylesheet.track
: WebVTT file.worker
: 1 JavaScript web worker or shared worker.video
: Video file.
Ability to support preload directives of browsers
Preload is supported from Chrome 50 version starting April 2016 (https://www.chromestatus.com/feature/5757468554559488) and also support from Opera 37+ (http://caniuse.com/#search=preload). And it has not yet been confirmed to be supported in Mozilla Firefox and Microsoft Edge.
To learn more and more clearly about the preload directive, you can read it here: https://www.keycdn.com/support/preload-directive
Prefetch
Prefetch is a mechanism that allows the browser to prefetch resources beforehand (these resources are rated at low priority when the browser performs the fetch) that may be needed in the future, and saved. Store them in the browser cache. Once a page has actually been loaded, the browser begins to prefetch resources that may be accessed by users in the future. If the user clicks on a pre-loaded link, immediately the page content corresponding to that link will be preloaded. There are 3 types of prefetching: link, DNS, and prerendering (we'll cover more details below).
1. Link Prefetching
The link prefetching allows the browser to prefetch resources, and store them in the cache. The browser will fetch the links contained in the HTML or in the HTTP header as follows:
- HTML:
- HTTP Header: Link: ; rel = prefetch
“This technique has the potential to speed up many highly interactive websites, but it will not always work for every website. For some sites, it is difficult to determine which page users will be able to access next. For some other websites, the data may become uninteresting if it is preloaded. You should also be careful not to prefetch files too early or you may slow down the page that the user has viewed. - Google Developers ”
Link prefetch is supported by most browsers, except Safari, IOS Safari, and Opera Mini. Chrome and Firefox also specify which resources are prefetched resources in the network panel if you want to test them.
2. DNS Prefetching
TheDNS prefetching mechanism allows the browser to perform an implicit DNS fetch on the page while the user is browsing the page. This minimizes the DNS fetch delay, DNS is always available when a user clicks on a link.
“DNS requests use extremely small bandwidth, but the latency when fetching DNS is relatively high, especially on mobile networks. By prefetching DNS results, latency can be significantly reduced in certain cases, such as when a user clicks on the link. In some cases, the delay may be reduced to one second.-Mozilla Developer Network”
Example of using DNS prefetching:
One interesting thing: Google Chrome also implements a mechanism similar to DNS prefetching when you type in its address bar. You can check this when visiting chrome: // dns /
You can also use built-in modules like the Pagespeed filter insert_dns_prefetch to automatically generate tags in the header for all domains.
DNS prefetch is also supported by most modern browsers except Opera Mini.
3. Prerendering
Prerendering (shown first) is also quite similar to prefetching (prefetching) which will preload the entire set of resources on the page that users can access to the next. The difference is that prerendering actually loads in front of the entire page, including the resources included in a document. Consider the following example:
“The prerender pre-visual mechanism can be controlled by your application to determine which redirect to the next HTML page is made: The browser will load and process the predefined resource to load just like 1 HTML response. To load other types of content with appropriate request headers or if the resource is not HTML, the application can use the prefetch mechanism.-W3C”
You should note that the prerendering mechanism comes with relatively large resource preloading, and can cause bandwidth waste, especially on mobile devices. You should also note that you cannot test it with the Chrome DevTools tool, instead you can only know if a page has been prerendered before by visiting chrome: // net-internals / # prerender . Or you can test it by using the page prerender-test.appspot.com.
Prerender is only supported by a few browsers, except Mozilla Firefox, Safari, IOS Safari, Opera Mini, and Edge.
There are several drawbacks to using this mechanism, such as privacy criticism.
- Web access statistics can be inflated compared to reality.
- Users may face more security risks by having to download more pages, or download resources from unwanted sites.
- Users may violate their own network or organization's usage policy by pre-fetching unlicensed content.
To better understand you read here: prefetching.
Preconnect
Preconnect (makes the connection first) allows the browser to establish connections early before an actual HTTP request is sent to the server. Connections of this type include: DNS lookups, TLS negotiations, TCP handshakes. This technique minimizes roundtrip round-trip latency and saves users time.
“Preconnect is an important tool in your optimal toolkit… It can minimize the roundtrips round trip cost of requests - in some cases, it can reduce request latency by hundreds or even. thousands of milliseconds-Ilya Grigorik”
A preconnect can be added directly to an attribute of the link tag in HTML. It can also be set via HTTP headers or can be dynamically generated by JS. Below is an example to enable preconnect for a CDN URL.
Below is an example of using preconnect with Google Fonts. By adding the preconnect directive to fonts.gstatic.com, it makes the font initialization request load in parallel with the CSS request, allowing the font request to be sent immediately. Want to see more details, you visit eliminating RTTS with preconnect.
In the case of not using preconnect, the browser after loading the entire HTML to know it needs more CSS resource located in fonts.googleapis.com. After downloading this CSS resource, the browser knows that the page needs 2 more fonts, and it starts to initialize to load these 2 fonts from fonts.gstatic.com - first, the browser still needs to perform the DNS steps again. , TCP, and TLS handshakes even when socket connections are available for multichannel requests on HTTP / 2 connections.
In case, when we add preconnect directive to guide browser, the page will need to load resources from fonts.gstatic.com. As a result, the browser established a socket connection in parallel with the CSS request, and completed the request to load resources ahead of time, allowing font requests to be sent almost immediately.
Preconnect is supported by several browsers, except Internet Explorer, and Opera Mini.
To better understand you read here: preconnect.
0 Comments