Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: FOUC #427

Merged
merged 7 commits into from
Jan 28, 2024
Merged

fix: FOUC #427

merged 7 commits into from
Jan 28, 2024

Conversation

Aslemammad
Copy link
Contributor

@Aslemammad Aslemammad commented Jan 26, 2024

part of #344

Since the style scripts are modules, they run after the HTML parse phase, which causes the styles to flash.

What I do in this PR, is I get the styles and I inject them as normal style tags in the first load and then for HMR, later they'll be removed automatically.

Why not script tags?

The module type of the script tags and them importing things is limiting and won't let me to use blocking script tags.
blocking:

<script />

non-blocking:

<script type="module" />

And since the client-transformed CSS (that supports HMR) in vite is a JS module, we cannot use normal script tags. What can be done is server-transforming CSS, so we get the transformed plain CSS and then inject it in the style tags.

I also remove the spinner.

Copy link

codesandbox-ci bot commented Jan 26, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 0adec32:

Sandbox Source
Vanilla Typescript Configuration
React Configuration
React TypeScript Configuration

Copy link

vercel bot commented Jan 26, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
waku ✅ Ready (Inspect) Visit Preview Jan 28, 2024 10:11am

Comment on lines +33 to +36
// avoid HMR flash by first applying the new and removing the old styles
if (style) {
queueMicrotask(style.remove);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering why you queued style.remove function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we do not have a FOUC in HMR, if we don't do this, styles will be removed, for a small bit of time, there are no styles until the new script tag applies the new styles. By queuing, we first apply the new script styles and then after that we remove the old styles.

Copy link
Owner

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good.

npm run examples:dev:01_template works well.
Do you notice npm run website:dev still has FOUC at first?

@Aslemammad
Copy link
Contributor Author

Thank you

Do you notice npm run website:dev still has FOUC at first?

I don't on my system! Is it still there?

@dai-shi
Copy link
Owner

dai-shi commented Jan 28, 2024

@Aslemammad Yeah, as I tried, the <style> tag isn't present in the HTML.

Can anyone else try?

@Aslemammad
Copy link
Contributor Author

@Aslemammad Yeah, as I tried, the <style> tag isn't present in the HTML.

Can anyone else try?

Have u tried linking waku or updating it in the website directory? since the version is fixed there! Let me know

@dai-shi
Copy link
Owner

dai-shi commented Jan 28, 2024

Ah, that's it. I forgot.

@dai-shi dai-shi merged commit 764e030 into dai-shi:main Jan 28, 2024
28 checks passed
@Aslemammad Aslemammad mentioned this pull request Feb 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants