Skip to content

Serve public files given to the runtime from the Service Worker #62

Closed

Description

@kazupon

Summary

  • Goal: Serve files under the public directory (/public/) at the preview root when they are passed to ready() or added with addFile(), 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, so servePublicMiddleware skips every other public file.
  • Effect: A request for a public file gets the SPA fallback index.html with status 200 instead of the file. For example, /__preview__/logo.svg does 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.

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'), then fetch('/__preview__/file-sync-public.txt') from the preview: status 200, and the body is the transformed index.html.
  • With '/public/initial-public.txt' added to the files given to ready(), fetching /__preview__/initial-public.txt gave 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() or deleteFile() 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. After deleteFile() resolves, it is no longer served.
  • The limitation is removed from the README and the JSDoc of addFile().

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions