feat: Add preview middleware for prerender plugin #132
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.
As of v5, Vite's preview server will no longer make any attempt to serve anything but
/index.html
for any request missing a file extension --/foo
will not result in/foo/index.html
being returned.This tends to result in hydration mismatches that users might not expect or recognize; for example, in our demo here (which largely mirrors our template for
create-preact
) we assign anactive
class for items in the header to show which route is active. If the user loads/404
in their browser, both the "Home" and "404" header items will have theactive
class. It's easy for users to confuse this as a hydration issue with Preact or Preact-ISO when the reality is that the wrong HTML file was returned.To fix this, we can add a simple middleware that converts
/foo
into/foo/index.html
during Vite's preview mode, and if the file isn't found, it falls back to a config option (like/404
) or to/index.html
. Naming suggestions certainly welcome & appreciated, not sure if these are as clear as they could be?Edit: To avoid any potential issues, this is intentionally not enabled by default if the user prerenders their app. Will slip that into the next major though, as I do think it's a better default.
Reference:
preact-www
preact-iso
issue from this morning