Skip to content

feat(vrowzer): report preview load failures with session identity - #59

Merged
kazupon merged 4 commits into
mainfrom
feat/preview-load-errors
Oct 1, 2026
Merged

kazupon merged 4 commits into
mainfrom
feat/preview-load-errors

Conversation

@kazupon

@kazupon kazupon commented Oct 1, 2026

Copy link
Copy Markdown
Owner

Closes #44

Adds a previewLoadError event, so that a host can tell which preview session failed to load before its application code could start.

API

VrowzerEventMap gains previewLoadError, with a PreviewLoadErrorInfo payload:

  • id: the session ID, taken from the host's record of the session, never from the report
  • stage: 'html' (fetching the preview HTML) or 'script' (loading an initial script)
  • message: always present, even when the browser gives no details
  • url, status and error (name and message): only when known

Hosts that subscribe with on('*') also receive the new event. Existing events do not change.

How it works

  • Each session gets a new load token on every mount() and reload(). The bootstrap srcdoc embeds the token and the host origin.

  • The bootstrap reports failures with parent.postMessage():

    • an HTML response with an error status;
    • a rejected fetch or another bootstrap exception;
    • each initial module or classic script that fires error. A script error no longer counts as a successful load.
  • The host accepts a report only when all of these hold:

    • its token matches the session's current document;
    • it comes from that session's iframe;
    • its origin is the host's.

    Reports from replaced or removed documents are therefore ignored. The token only tells loads apart. It is not a secret, because preview code runs in the same document.

  • The payload keeps only known fields, with bounded string lengths. blob: URLs of inline modules are dropped.

  • The host listens for messages only while sessions are mounted.

Rendering does not change: an error response body is still rendered, and the remaining scripts still run. Two smaller changes in the bootstrap:

  • It is now wrapped in an IIFE, so that its helpers do not become preview globals.
  • An inline module's Blob URL is now also revoked when the script fails to load.

Runtime errors thrown by the application are out of scope.

Tests

  • Unit tests (index.test.ts). All 7 new tests failed before the change. They cover:
    • the token in srcdoc and its renewal on reload;
    • the session ID;
    • ignored reports: an old token, another session's token, an unknown token, another source, another origin, another message type, malformed reports, and a removed session;
    • field filtering and length limits;
    • the listener lifecycle.
  • Integration tests (vrowzer.integration-test.ts). All 7 new tests failed before the change, because no report arrived.
    • Every session loads the same preview URL, so each test makes a single session fail by pointing its bootstrap at another URL right after mount().
    • Cases: an HTML error status, a rejected HTML fetch, a missing entry module, a module graph that fails to load, a cross-origin classic script, a healthy session next to a failing one, and recovery after fixing the input and reloading the session.
  • The last commit corrects a comment from fix(vite-dev-server): use Vite's error handler for Service Worker requests #58. Until a new file reaches the Workers, the preview answers with index.html (200) through the SPA fallback, not 404. This was checked in the browser.

The README has a new Events section, and the API references were regenerated.

Validated locally with:

  • vp run test:unit: 116 files, 1,848 tests
  • vp run test:integration:vrowzer: 51 passed, 1 conditional skip
  • vp run test:e2e: 46 passed, 6 existing test.skip
  • oxlint (no new warnings), the format check of the changed files, and typecheck

Each preview session now gets a load token, renewed on every mount and reload and embedded in the bootstrap document together with the host origin. While sessions are mounted, the host listens for vrowzer:preview-load-error messages and emits previewLoadError only when the token matches a session's current document, the message comes from that session's iframe, and the origin is the host's. The payload keeps only known fields with bounded lengths, and its id comes from the host's record. The bootstrap does not send reports yet.
The bootstrap now reports to the host when the preview HTML returns an error status or cannot be fetched, and when an initial script fails to load, instead of treating a script error as success. Rendering does not change: an error response body is still rendered, and the remaining scripts still run. The bootstrap is wrapped in an IIFE so that its helpers do not leak into the preview's globals, and an inline module's Blob URL is also revoked when it fails to load. The integration tests point one session's bootstrap at another URL right after mount(), so that only that session fails.
Add an Events section to the README that describes previewLoadError, its payload, and how to recover by reloading the session, and regenerate the API references for PreviewLoadErrorInfo and VrowzerEventMap.
…ynced

Until a new file reaches the Workers, the preview does not answer 404 for it: the SPA fallback serves index.html with status 200. Checked in the browser by fetching a missing .ts file from the preview.
@kazupon
kazupon merged commit 6564956 into main Oct 1, 2026
4 checks passed
@kazupon
kazupon deleted the feat/preview-load-errors branch October 1, 2026 03:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Report preview bootstrap failures with session identity

1 participant