fix(vite): register files generated by Vite plugins in static file cache #3534
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.
Possible fix #3487
This includes
After analyzing the problem for a while for the Vite's
dev
server and how Fresh handles this, the conclusion seems to be that this is an edge case. So it will not work withvite dev
, only in "production" where actual generated fixed files get served. Detailed reasoning on the bottom.Generated pull request message below with details:
Problem
Files generated by Vite plugins (e.g.,
vite-plugin-pwa
service workers, manifests) were created in_fresh/client/
but returned 404 when accessed via HTTP. Fresh's static file serving only registered files from the Vite manifest and public directory, missing plugin-generated files.Solution
Added a directory scan in
server_snapshot.ts
that walks_fresh/client/
after the client build completes. Any files not already registered (excluding.vite/
directory) are added to the static file cache, making them accessible via HTTP.Changes
packages/plugin-vite/src/plugins/server_snapshot.ts
to scan client output directoryvite-plugin-pwa
integrationvite-plugin-pwa
to dependencies for testingTesting
All 117 tests pass. New tests verify:
vite-plugin-pwa
during buildAnalysis: Post-Hook Approach for Dev Server
Current Architecture
Fresh currently registers its middleware BEFORE Vite's built-in middlewares (pre-middleware). This means:
The Problem
This aggressive interception blocks Vite plugins from serving their own files. My attempt to forward 404s didn't work because once Fresh responds, the middleware chain ends -
next()
isn't called in the response path.The Post-Hook Solution
NOT RECOMMENDED for the following reasons:
Architectural Mismatch: Fresh's design philosophy is to control routing aggressively. Using post-hook would fundamentally change this to "Vite first, Fresh fallback."
Hidden Dependencies: The current code has implicit assumptions about execution order (checking module graph, static files early). Moving to post-hook could break these.
Unintended Side Effects: Other Vite plugins might interfere with Fresh routes before Fresh sees them.
Better Alternative: Targeted Pass-Through
Instead of post-hook, I recommend a whitelist approach - explicitly pass known file patterns to Vite BEFORE trying Fresh SSR:
However, this still has the same problem: we can't "try Vite and fallback to Fresh" without post-hook.
Recommended Solution: Do Nothing for Dev Mode
After careful consideration, I believe:
static/
for dev testingThe vite-plugin-pwa documentation typically shows dev mode with
devOptions.enabled: true
as optional, mainly for testing. Most development doesn't need the actual service worker.Final Recommendation
Skip the dev server fix for now:
ignored
with a comment explaining it's a known issue