Repository navigation
Serve public files given to the runtime from the Service Worker #62
Copy link
Copy link
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
Summary
- Goal: Serve files under the public directory (
/public/) at the preview root when they are passed toready()or added withaddFile(), as Vite does. - Current gap: The Service Worker builds its list of public files once, when its dev server starts. At that point its virtual filesystem holds only
/public/.gitkeep. The list is never updated, soservePublicMiddlewareskips every other public file. - Effect: A request for a public file gets the SPA fallback
index.htmlwith status 200 instead of the file. For example,/__preview__/logo.svgdoes not return/public/logo.svg. - Request: Keep the Service Worker's public file list in sync with its virtual filesystem, as the Web Worker already does in
setupHMR(). - Verification status: Reproduced in the browser on the integration playground (see Details).
- Related: Found while implementing #42 (#61). The README and the JSDoc of
addFile()describe this as a current limitation. That note should be removed with the fix.
Details
Current implementation, verification, requested behavior, and acceptance criteria
Current implementation
Reviewed at ba60d7b.
- When the Service Worker script is evaluated, its entry writes
/public/.gitkeepand starts the dev server. The runtime sends the files given toready()afterwards, withV_FS_INIT. createServer()from@vrowzer/vite-dev-server/service-workercallsinitPublicFiles()during startup and waits for it.initPublicFiles()reads the public directory once.- The server registers
servePublicMiddleware(server, publicFiles), which skips any path that is not in the set. - The server receives the file sync subscriber's watcher through
watcherFactory, but nothing listens to its events, so the set never changes. - The Web Worker updates its own set on add and unlink, as Vite does. The Web Worker does not serve public files, though.
Verification
I checked this on the integration playground (packages/vrowzer/integration/playground) with temporary changes, which were reverted afterwards.
- After
ready(),addFile('/public/file-sync-public.txt', 'public text'), thenfetch('/__preview__/file-sync-public.txt')from the preview: status 200, and the body is the transformedindex.html. - With
'/public/initial-public.txt'added to the files given toready(), fetching/__preview__/initial-public.txtgave the same result.
Requested behavior
- Public files given to
ready(), and files added under/public/later, are served at the preview root. - After a public file is deleted, it is no longer served.
- When
addFile()ordeleteFile()resolves, the change is already visible to requests (Return promises when file changes are visible to preview reads #42). The Service Worker acknowledges a file change right after applying it, so the list should be updated synchronously when the watcher event is emitted.
Acceptance criteria
- A public file given to
ready()is served with its content. - A public file added with
addFile()is served once the promise resolves. AfterdeleteFile()resolves, it is no longer served. - The limitation is removed from the README and the JSDoc of
addFile().
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working
Activity