@ogygia/rewrite
Testing
A rewrite's .check() runs the whole round trip on one piece of markup in a unit test, and in dev ogygia compares every restored element with what Svelte rendered and names the first node that differs.
On this page
A render that drops a mark or reorders a child breaks hydration only in the browser, so you want to catch it before it ships. Every rewrite has .check() for a unit test, and dev checks each restored element on every page load.
check()
.check(html, { kind, csr, event }) runs the whole round trip on one piece of markup: mark, your render, settle, then (with a DOM, as in Vitest browser mode or jsdom) parse, restore, and compare each restored element with Svelte’s original. kind defaults to 'region', csr to false, and event (the request your render sees) to a GET for /. It restores into a document of its own and leaves nothing in the page running the test: no sheets in its head, no logged restore warnings.
It returns:
html: what ogygia would send;head: what went into<head>(for a document) or what the answer carries for the head;restored: how many elements ogygia restores;failed: each element whose render failed, as{ name, error }, whereerrorstarts with the message code;dom: the restored DOM, ornullwithout a DOM;mismatches: each restored element whose light DOM isn’t exactly what Svelte rendered (empty without a DOM).
Run it in Vitest browser mode or under jsdom. In Node there’s no DOM to restore into, so dom is null, mismatches is always empty, and a broken restore still passes.
In the example, a test checks that every element renders and comes back exactly. kind: 'region' makes every element a restored one, the strictest case.
// @vitest-environment jsdom
import { expect, it } from 'vitest';
import { components } from './components';
const markup = '<my-button><span>Sign in</span></my-button>';
it('restores the light DOM exactly', async () => {
const r = await components.check(markup, { kind: 'region' });
expect(r.failed).toEqual([]);
expect(r.mismatches).toEqual([]);
expect(r.restored).toBeGreaterThan(0);
});Dev check
In dev, ogygia sends each restored element’s original children with the page, and the restore compares them: node types, tags, text, comments and attributes. A difference is an error in the console that names the first node that differs (restore_markup_broken), and an ogygia:restore-mismatch event. The event is dispatched on the element and bubbles, and its detail is { host, diff }: the tag name and the same text.
[ogygia] the rewrite broke hydration markup: <my-button> > [1]: Svelte has <i>, the restored markup has nothingMessage codes
The messages you can meet with a rewrite, and where each is explained. handle_transform_moved comes from ogygia itself; the rest are @ogygia/rewrite‘s.
| Code | Kind | When |
|---|---|---|
rewrite_plugin_missing | error | A rewrite handle runs, but rewrite() isn’t in the Vite config (Installation). |
rewrite_after_ogygia | error | A rewrite is listed after ogygia.handle() (Server handle). |
handle_transform_moved | error | ogygia.handle({ transform }) was passed (Why not transformPageChunk). |
rewrite_element_failed | warning | An element’s render still failed alone (Failures). |
rewrite_marks_lost | warning | A render dropped og-h (Failures). |
rewrite_element_split | warning | A matched element spans two chunks of a streamed page (Streaming). |
rewrite_page_failed | error | A page function threw or dropped marks (Failures). |
restorer_unloaded | error | The restorer chunk failed to download (Restoration). |
restore_markup_broken | error | Dev: a restored element’s light DOM differs from Svelte’s (Dev check). |
restore_late | warning | A component runtime upgraded the element first (Browser warnings). |
restore_host_invalid | warning | A marked element can’t have a shadow root (Browser warnings). |
restore_sheet_missing | warning | og-shadow names a key no sheet carries (Stylesheets). |
restore_handoff_unknown | warning | og-handoff is neither adopt nor replace (Handoff). |
rule_rewrite_after_cancel | rule (dev) | A cancelled request’s rewrite kept going. A bug in ogygia: please report it. |