fix: register files generated by Vite plugins in static file cache#3534
Conversation
|
Nice fix — the core change in
|
Keep main's new tests and append PR's vite-plugin-pwa tests. Take main's deno.lock (will be regenerated). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Normalize registered pathnames when comparing (strip leading /) to prevent double-registration of files with inconsistent path formats - Remove brittle workbox assertion from build test - Remove permanently-ignored dev server test (dead code) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Fixes #3487
This includes
After analyzing the problem for a while for the Vite's
devserver 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-pwaservice 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.tsthat 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.tsto scan client output directoryvite-plugin-pwaintegrationvite-plugin-pwato dependencies for testingTesting
All 117 tests pass. New tests verify:
vite-plugin-pwaduring 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: trueas optional, mainly for testing. Most development doesn't need the actual service worker.Final Recommendation
Skip the dev server fix for now:
ignoredwith a comment explaining it's a known issue