fix: avoid module preload polyfill for zero js html #4999
Merged
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.
Description
After #4555, we closed #3127 but @DylanPiercey found out that the module preload polyfill is still loaded when there isn't any JS.
This PR adds a guard avoiding adding the polyfill in html entries that don't have any script module (for example, a simple 404 page).
The user may add modules in pre plugins, but these are executed before the guard. If the user adds a module in a post plugin, they are executed after the preloads are inserted so these tags will still not need the polyfill internally.
The only case would be if the user is adding a tag + preload by hand in a post plugin, but I can't see why someone would want to avoid these scripts be processed by Vite. In the worst case scenario, this will only mean a that some browsers will not take into account that preload. We could document this gotcha if someone thinks this is something users will end up hitting.
Additional context
An alternative is to always add the polyfill and remove it when analyzing the generated chunk. I tried to do so and it isn't straightforward. And the same caveat would apply here, the user may still add something by hand as a post plugin transforming the html.
What is the purpose of this pull request?