refactor: Ensure module preload polyfill is inlined into main bundle #147
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.
This has been a fairly long-standing issue in the prerenderer that I've unfortunately just not had the time to track down.
The issue is that Rollup/Vite notices both the web entry chunk & the prerender chunk (containing, at a realistic minimum, rts) both depend on a number of shared modules (
preact
, amongst others) and so it will extract out those shared modules to a separate chunk. This gives us an app entry module like the following:You can see this if you load up the Preact docs site, the first JS file that's downloaded is just a static import of our actual app & Vite's modulepreload polyfill -- not ideal.
Vite has always added a modulepreload link with the real bundle path, and with this entry being so tiny I wasn't too concerned with the delay it'd cause, but it probably is a wasted ~20+ms on average which is far from optimal.
This PR corrects the behavior, ensuring our bundle & the preload polyfill are merged. This means we have to patch the preload polyfill with a window check too, as the polyfill is an IIFE and the prerender chunk now relies on it, but that should be fine.