prefetch
and preload
directives has preload
(they look, respectively, as the “magic comments” of webpackPrefetch
and webpackPreload
to import()
commands). With their help, the browser can give hints about resources that the user may need in the near future. The browser loads such resources in advance, which allows the user to improve the experience of working with the site.prefetch
directive tells the browser that the specified resource may be needed in the future to navigate the site.prefetch
resource hint causes the corresponding resources to be loaded when the browser is idle.preload
directive informs the browser that the resource marked by it will definitely be needed in this session of working with the site, but it will be accessed a little later. At the same time, Chrome even displays a warning when a similar resource is not used 3 seconds after the download.preload
hint are loaded as normal resources, however, their loading starts a little before the user accesses them.preload
directive allows preload
to load resources that will be needed soon and avoid the “waterfall effect”, when everything you need to display a page starts loading immediately after clicking on the appropriate link. This technology allows you to split the page loading into two stages, when you first load the HTML code, and then everything else. At the same time, this does not mean additional load on the network channel.prefetch
used during browser downtime to speed up future interactions with the site. Its use may mean the need for additional traffic costs in the event that the user does not use the downloaded resources.import()
command) to download only those parts of the application that the user currently needs.HomePage
), which has a login button ( LoginButton
), which opens a modal window for entering credentials ( LoginModal
). After the user logs in, he is shown the DashboardPage page. On this page there may be other buttons needed to go to other pages, but we focus on the fact that the goal of the user is this page.import("LoginModal")
command on the page with a login button, and an import("DashboardPage")
command on the page for entering credentials.import("LoginModal")
command import("LoginModal")
, the import(/* webpackPrefetch: true */ "LoginModal")
command import(/* webpackPrefetch: true */ "LoginModal")
. Similarly, on the page for entering credentials, instead of the import("DashboardPage")
command import("DashboardPage")
you need to use the import(/* webpackPrefetch: true */ "DashboardPage")
construction import(/* webpackPrefetch: true */ "DashboardPage")
.webpackPrefetch
hint), a fragment of the application representing the control panel page will be put in the download queue.<link rel="prefetch" href="login-chunk.js">
will be formed on the HTML page. The application fragment representing the login page is loaded on demand, so webpack will take care to embed a tag like <link rel="prefetch" href="dashboard-chunk.js">
into the page after the login page is loaded .import(/* webpackPrefetch: true */ "...")
construct import(/* webpackPrefetch: true */ "...")
to describe the resources that are supposed to be loaded when the browser is idle. There is a similar command, import(/* webpackPreload: true */ "...")
, to describe the preload resources. If you compare these two approaches, you can identify many differences:import()
command. This may make sense if, for example, a component depends on a large library, which must be present in a separate fragment of the application. For example, the chart output component, ChartComponent
, uses a large chart library - ChartingLibrary
. The component, during the download, shows the download indicator ( LoadingIndicator
) and immediately executes the import(/* webpackPreload: true */ "ChartingLibrary")
command import(/* webpackPreload: true */ "ChartingLibrary")
. As a result, when loading a page that uses a ChartComponent
is requested, it is requested using the <link rel="preload">
, and loading a fragment of the application with the library. Assuming that the page with the component is smaller than the application fragment with the library and its loading is completed faster, this page will appear with the loading indicator, which will be displayed until the download of the application fragment with the library is completed. This will slightly reduce the load time for everything needed, since the system performs loading in parallel, without breaking this process into two separate actions. This will be especially noticeable where network connections are characterized by high latency.webpackPreload
design can degrade performance, so you need to use it carefully.preload
or prefetch
, you will probably be better off using prefetch
. In this case, the above is true for the import()
webpack command. On ordinary HTML pages, preload
probably more suitable for preload
.import()
commands with the directive webpackPrefetch
. However, it should be borne in mind that all resources loaded in this way will fight for bandwidth.prefetch
and preload
.webpackPrefetch: true
construct, you can, instead of a boolean value, use a number. As a result, the webpack will load the fragments in the specified order. For example, a resource with webpackPrefetch: 42
will be loaded before a resource with webpackPrefetch: 1
, which, in turn, will be loaded before the resource with webpackPrefetch: true
, and this resource will be loaded to the resource with webpackPrefetch: -99999
(this looks like z-order
) . In fact, true
here is the same as 0.webpackPreload
, but you hardly need it.import()
commands request the loading of the same resource, while using both the prefetch
hints and the preload
hints?preload
directive takes precedence over prefetch
, and prefetch
used if there are no other instructions for a specific webpack resource. If several commands are used to preload the same resource, the one with higher priority is activated.prefetch
and preload
not supported in the browser?prefetch
and preload
are called “prompts”, so even if the browser supports them, it can, for some reason, ignore them.prefetch
and preload
on some page that is the entry point to the site do not work. What can be wrong?prefetch
and preload
for resources loaded on demand. When prefetch
or preload
are used in import()
commands in site fragments that are input points, the HTML generation system is responsible for adding the <link>
tags to the resulting code.entrypoints[].childAssets
.prefetch
and preload
on each import()
command?import()
commands related to the resources a user will need with a high degree of probability. It is not necessary to load Internet channels in vain, because for some users they are far from unlimited. Therefore, use these directives only when they are really needed.prefetch
and preload
are used to work with materials that are loaded on demand. As a result, if you do not use this opportunity, no additional load on the system is created.import()
command. Can I add a few such comments to it? import( /* webpackChunkName: "test", webpackPrefetch: true */ "LoginModal" ) // import( /* webpackChunkName: "test" */ /* webpackPrefetch: true */ "LoginModal" ) //
import()
commands. Can users of my webpack library benefit from using prefetch
?import()
command and rollup will save them. If the finished project is collected using a webpack, it will take advantage of these comments. If the webpack will not be used, they will simply be removed by the minifier.prefetch
hint to some import()
command will improve performance. Should I add this hint?prefetch
?prefetch
directives allow you to specify the order of loading resources, they depend on the location in the application in which the user is located. In addition, the prefetch mechanism is more cost-effective for bandwidth and loads data only when the browser is idle.prefetch
, rather than caching the entire application in one go.preload
in this situation?import()
with webpackPreload: true
. And do not forget to add <link rel="preload">
for the resources necessary for the application entry point to work properly (before the tags for the children).prefetch
and preload
. Their correct application helps to improve the user experience of working with the site, and the wrong can lead to unnecessary costs of traffic, which is especially important for cases when working with a web resource is conducted on a device whose capabilities are limited. We hope the new webpack features will come in handy for you in creating quick and convenient web projects.Source: https://habr.com/ru/post/354188/
All Articles