question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Big flash of unstyled content (FOUC) during HMR

See original GitHub issue

Describe the bug

Problem

When my styles change, the page flashes without any styling for a second or so, it’s verry jarring I thought it was actually reloading.

Cause

After some debugging I found out the cause:

When a css stylesheet is loaded through <link> (i.e. in the main HTML page), the HMR logic is simply to update its href but that can cause issues, here’s what happens in my project:

  • Multiple css are HMR at the same time, some are imported by JS, the main one is <link> in index.html.
  • At HMR time, Vite fetches the imported ones, to later replace <style> nodes.
  • At the same time it fetches the updated style sources, Vite simply updates the href of <link>. This triggers a network request by the browser and on my project, it takes ~700ms to complete.
  • Before that completes, Vite receives some responses and starts updating <style> tags. This triggers a CSS repaint by the browser.
  • Because the <link href> was changed but has not completed fetch yet, the browser just ignores it. Old CSS rules are not applied anymore.
  • And this is why there is a lot of unstyled content on my page…
  • Until the <link> fetch completes, and browser repaints again with all CSS rules back in place.

Fix ideas

To avoid FOUC, one should not change href or remove <link> tags before the new styles are loaded. Maybe Vite could add a new <link> tag with the new href just after the old one, and remove the old one when the new one finishes loading.

Reproduction

Sorry, no repro on this one. The setup is just to have link and imported style and trigger a reload of all, which is automatic if you use tailwind 😉 To make the effect clearly visible, you need to have larger styles in <link> (so it takes longer to reload than the styles), or use something to breakpoint the network request, e.g. Fiddler. Just let the style request go through and block (for a while) the <link> request.

You’ll see that once the <style> request completes, the page repaints without styles, and it’s fixed when the <link> request completes.

System Info

Vite 2.9.2

Used Package Manager

npm

Logs

No response

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:3
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jods4commented, Jun 8, 2022

@sapphi-red I’ll try when it’s merged into a published release, I don’t have the time to make my own builds right now.

0reactions
sapphi-redcommented, Jun 8, 2022

@jods4 I confirmed #8495 fixes for me on firefox. Would you check if it fixes for you, too?

Read more comments on GitHub >

github_iconTop Results From Across the Web

What the FOUC is happening: Flash of Unstyled Content
This phenomenon is known as Flash of Unstyled Content (FOUC), and it is a topic covered by a question included in the hp5b...
Read more >
SvelteKit split second unstyled html - Stack Overflow
The flashes of unstyled content should go away in production mode, when you deploy with an adapter. This stock SvelteKit template does not ......
Read more >
Fixing Flash of Unstyled Content (FOUC) in your Webpack 4 ...
The fix to the FOUC was to include a Webpack Plugin called Extract Text Loader. This plugin basically extracts all the css that...
Read more >
Hot reload with React.js - announce - Meteor forums
This is fixing the flash of unstyled content you would have until your app is rendered. This is not the only way to...
Read more >
Dealing with FOUC (Flash of Unstyled Content) - Slicedbread
Sometimes when a page takes a few seconds to load, either due to the page having a lot of image/CSS/javascript assets or the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found