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

Add CSS entrypoints to the manifest #9

Merged
merged 1 commit into from Jun 10, 2022
Merged

Add CSS entrypoints to the manifest #9

merged 1 commit into from Jun 10, 2022

Conversation

jessarcher
Copy link
Member

@jessarcher jessarcher commented Jun 9, 2022

Vite does not currently include CSS entry points in the manifest because the standard approach to loading CSS with Vite is to import it via JavaScript.

The problem with loading CSS via JavaScript with Blade, is that in HMR mode, the CSS is injected into the page via JavaScript, and browsers do not block rendering for this, which leads to a "flash of unstyled content" (FOUC) on every page load. It's not an issue with SPAs because the JavaScript also renders the HTML.

This is not an issue in production because we can determine from the manifest the appropriate <link rel="stylesheet"> tags to create from any JavaScript entry points.

To solve this in HMR mode, we need to output <link rel="stylesheet"> tags for CSS, which means we need to specify the CSS files as entry points to the @vite directive. Once we're specifying CSS as entry points, they need to exist in the manifest to be able to load them in production.

There is an open PR to Vite to include CSS entries in the manifest, however, this may not make it into Vite 2 (or even 3 potentially). A solution, at least for the time being, is for our plugin to augment the manifest with CSS entry points. It does this by listening to the renderChunk hook to record the appropriate manifest entries and then hooking into the writeBundle hook to update the manifest. We can hopefully remove this workaround once the above PR is released.

Other backend integration plugins follow a similar approach.

There is currently a separate issue with Vite in HMR where where it currently only updates the href of the <link> tag with a new timestamp when a change occurs. This leads to a FOUC every time an HMR change is made, which is arguably worse than only on page load. Thankfully, @timacdonald has an open PR to Vite which keeps the existing <link> in place until the replacement has loaded. Once that is merged, this will be good to merge also.

@timacdonald
Copy link
Member

vitejs/vite#8495 has been merged and is scheduled to be released in Vite 2.9.11

@jessarcher jessarcher marked this pull request as ready for review June 9, 2022 23:47
@jessarcher
Copy link
Member Author

This is good to go then! The Vite team is understandably not keen on backporting vitejs/vite#6649 to Vite 2, so we will need this workaround at least temporarily.

@timacdonald
Copy link
Member

Once this is merged and assuming vitejs/vite#6649 is merged, we will want to rebase the Vite 3 branch and remove this code.

@jessarcher jessarcher merged commit 6583cba into main Jun 10, 2022
@jessarcher jessarcher deleted the css-manifest branch June 10, 2022 04:33
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.

None yet

2 participants