Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger prefetching after page is fully loaded

My scenario is:

  • user visits domain.com (home page)
  • domain.com/products page contains large image library and quite large CSS and JS libraries
  • when user visits domain.com and the home page has fully loaded, we start to prefetch resources & if possible at least some % of images from the archive.

Currently on some pages JS "eats" quite a lot of resources therefor triggering prefetch in some cases during page load is not the best answer - as it will cause a small lag when user interacts with JS created events and elements.

My questions are:

  1. Is it even possible (will it work) to trigger <link rel="prefetch" href="image.png"> or CSS file to be added to <head> so it can prefetch data from another page after current page is fully loaded?
  2. Should I do it similar like rendering additional stylesheet using JS where I add new tag within <head> as a stylesheet file so it can then render.. or is there another way?
like image 430
richardev Avatar asked May 20 '18 17:05

richardev


People also ask

How to preload assets html?

The rel="preload" attribute value is used to preload assets. It can be applied to several file formats, including CSS, JS, fonts, images, and more. Depending on the type of file you would like to preload, the corresponding as attribute may also need to be included along with rel="preload" .

What is prefetch and preload?

Preload is an early fetch instruction to the browser to request a resource needed for a page (key scripts, Web Fonts, hero images). Prefetch serves a slightly different use case — a future navigation by the user (e.g between views or pages) where fetched resources and requests need to persist across navigations.

What is preload in html?

The HTML preload Attribute is used to specify the way the author thinks the media should be loaded when the page loads. The preload attribute allows the author to portray to the browser about the way the user experience of a website should be implemented.

Can you preload CSS?

So, now how to you preload your CSS and other external resources? The answer is simpler than you think. Currently, you load CSS and JS through the link tag. So, to preload those resources, all you need is to change the rel attribute to preload and add the as=”style” attribute (or as=”script” for JavaScript).


1 Answers

You might use Cache Storage to prefetch (precache) assets. I work on an open-source project which uses this approach. Although, to serve precached assets you need a service worker. The logic of finding assets in my project looks like this.

The demo of this project is here. Also, I wrote an article which explains technical details of the project.

Assets get prefetched once the lib is loaded, so I don't wait for the entire page load. Maybe I should use requestIdleCallback to wait until the browser is idle.

Hopefully, it gives you some inspiration.

like image 176
Dmitriy Nesteryuk Avatar answered Sep 27 '22 21:09

Dmitriy Nesteryuk