Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML - Why is my preloaded resource loading again?

This is my example code:

  <body>
    <link rel="preload" href="/fonts/Roboto-Regular.woff2" as="font" />
    <style>
      @font-face {
        font-family: "Roboto";
        src: url("/fonts/Roboto-Regular.woff2") format("woff2");
      }
      * {
        font-family: "Roboto", sans-serif;
      }
    </style>

    <section>
      <h2>Hello World</h2>
    </section>
  </body>

What I expect:

Roboto-Regular.woff2 gets downloaded once. The second access should take <5ms since it's reading from cache.

What I actually see:

Roboto-Regular.woff2 gets fully downloaded twice. The second access took as much time as the first one.

Hard Refresh Waterfall

The blue bar here is Content Download.

What I have tried:

The solution suggested here: preloaded images get loaded again. But I quickly realised my issue has nothing to do with cache. I have configured serverside cache-control, as seen here in the response header for the file:

Response Header

The previous waterfall screenshot is taken after a hard refresh (ctrl F5); this one is taken after a soft refresh:

Soft Refresh Waterfall

This time both requests simply got their response from cache, which shows that cache has nothing to do with the whole situation.

So what has gone wrong here?

My environment:

Windows 10 Pro N 1909

Google Chrome 78.0.3904.108 x64

like image 263
cyqsimon Avatar asked Dec 16 '19 09:12

cyqsimon


People also ask

How do I know if preload is working?

In all processes, you will find preload, check if it is on or not. If it is on then preload is working and if it is off then preload is not working (you can turn it on).

Is preload render blocking?

The preload is competing with the render-blocking file for bandwidth. As a result, the download takes longer and the page renders more slowly. The page renders 0.9s faster without the preload. The downside of this change is that the JavaScript file will now finish loading later.

Should you preload CSS files?

Preloading CSS files # Waiting for JavaScript to execute before loading non-critical CSS can cause delays in rendering when users scroll, so it's a good idea to use <link rel="preload"> to initiate the download sooner.

What does the preload attribute do in HTML?

The preload attribute specifies if and how the author thinks that the media file should be loaded when the page loads. The preload attribute allows the author to provide a hint to the browser about what he/she thinks will lead to the best user experience.


1 Answers

It turns out it's related to CORS.

I accidentally stumbled across the answer today when I noticed several new warnings in the console that weren't there before.

New warnings

These warnings basically described my issue exactly as they are. I did a quick Google search and landed here: Preloading content with rel="preload", which said:

One interesting case where this applies, even if the fetch is not cross-origin, is font files. Because of various reasons, these have to be fetched using anonymous mode CORS.

As suggested, I simply threw in crossorigin to my preload declaration:

<link rel="preload" href="/fonts/Roboto-Regular.woff2" as="font" crossorigin />

And my preload worked.

Preload Working


Also

If you are preloading a resource with MIME type of fetch such as .json, you also need to include the crossorigin attribute in your preload declaration for similar reasons (I assume) (Reference).

fetch requires crossorigin too

like image 101
cyqsimon Avatar answered Oct 29 '22 05:10

cyqsimon