Add CSS entrypoints to the manifest#9
Merged
Merged
Conversation
Member
|
vitejs/vite#8495 has been merged and is scheduled to be released in Vite |
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. |
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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@vitedirective. 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
renderChunkhook to record the appropriate manifest entries and then hooking into thewriteBundlehook 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
hrefof 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.