Skip to content

Conversation

Aslemammad
Copy link
Collaborator

@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
Collaborator 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
Member

@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
Collaborator 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
Member

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
Collaborator 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
Member

dai-shi commented Jan 28, 2024

Ah, that's it. I forgot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants