@ogygia/rewrite
Restored elements
An element your render gets with restored true sits in markup Svelte hydrates. Render it in scoped form with slot wrappers and og-shadow, and before anything hydrates ogygia moves your render into a shadow root and puts Svelte's light DOM back.
On this page
An element render gets with restored: true carries og-h. It sits in markup Svelte will hydrate, so ogygia puts it back for Svelte before anything hydrates. Your render still paints: it moves into the element’s shadow root, and Svelte’s own children go back in the light DOM, where hydration expects them.
ogygia restores an element when it sits in markup Svelte owns (an island, or the whole body of a page Kit hydrates), when Svelte-owned markup or a hole sits inside it, and always in a region answer. An element with restored: false is yours: render it any way you like.
Render shape
Render a restored element in “scoped” form, with these additions:
<slot>wrappers. Where the component would show the host’s children, wrap them in a real<slot>element. Outside a shadow root a<slot>renders as if it weren’t there, so the page paints exactly like your scoped render.og-shadow="key …"names the sheets for the host’s root (see Stylesheets).og-keep="attr …"(optional) lists attributes you added that must survive the restore. A kept attribute must stay true of the live element, since a hole answer copies attributes onto live elements.og-handoff="adopt"or"replace"(optional) says how the restored root is handed to the component. See Handoff.
An og-h element without og-shadow is left exactly as Svelte wrote it, and its og-keep and og-handoff are taken off too. Every other og-* mark is ogygia’s: carry it along wherever you move a node, and don’t put a marker of your own between an ogygia mark comment and its node.
In the example, a my-button comes back with its children inside a <slot>, its root’s sheet named, and one added attribute kept.
<my-button
og-h="0"
og-shadow="my-button.shadow"
og-keep="icon-name"
icon-name="star"
class="sc-my-button-h"
>
<span class="btn sc-my-button">
<slot class="sc-my-button sc-my-button-s"
><!--og-c 0.0 t-->Sign in</slot
>
</span>
</my-button>Restoration
For each restored element, all in one task, ogygia:
- opens a shadow root with its sheets;
- moves the rendered tree into it, so its
<slot>wrappers become real slots; - puts Svelte’s children back in the light DOM, exactly as Svelte rendered them;
- sets the element’s attributes back to Svelte’s, except those in
og-keep.
Elements go in restore order: an element inside another before the one it’s in, and sibling elements last to first (reverse document order).
This runs before anything reads the markup: inline, right after the chunk that has the element; on a region answer before it goes in; on the next page before a router swap. A page where nothing was restored ships no restorer, and loads it later only when a region answer needs it. If that download fails, answers go in as they came (restorer_unloaded); a reload loads it again.
A tag the HTML parser would restructure where your render put it (an <a> inside the page’s <a>, a <div> in a <p>) ships as a stand-in and comes back as the real element.
Declarative roots
When the element isn’t defined yet, the root ogygia gives it is a declarative one, so when the component calls attachShadow() later it gets that root back, emptied. A region answer’s or an incoming page’s elements always get a declarative root (in browsers that can make one), even when their component is defined already: they’re restored before they go in, and where the runtime morphs them in, the copy it inserts gets one too.
So code that hooks attachShadow sees only the roots ogygia makes with attachShadow(): an element of the page that’s defined already, or any element in a browser that can’t make a declarative root. It never sees a declarative one. To find restored roots, listen for ogygia:restored instead.
Handoff
Most component libraries look for a root before they make one (this.shadowRoot ?? this.attachShadow(…)). They find the restored root, with the server’s tree in it, and render into it. The plan’s og-handoff says what happens to the server’s tree then:
adopt(the default, or no attribute): it stays. The component hydrates it or keeps it. A component that renders next to whatever its root holds shows it twice.replace: it’s a placeholder. The first time the component writes into the root once it’s upgraded, ogygia removes the nodes it moved in from your render that are still directly in the root. A component that wraps or moves them keeps them. This runs in a mutation observer’s callback, in the microtask after the write, so before paint. The sheets stay, and so does anything else added to the root.
In the example, a card whose library renders next to what its root holds asks for replace.
What counts as a write
A write is a node added to the root while the element is defined, as seen when the observer’s callback runs. The root is watched from the moment it holds your tree, before a region answer or an incoming page goes in. So a component that’s defined already and renders as its element goes in counts too: its root holds only its own render by the time ogygia:restored fires.
- Nodes added to an element’s root while its own
ogygia:restoredevent is dispatched belong to the listener and never count, even when the listener defines the element or makes its component render. The two can’t be told apart. To hand off from a listener, make the component render after the event, for example inqueueMicrotask(() => …). - Nodes added to another element’s root during that event count for that element.
- Nodes another script adds before the element is defined, in the same task as its
customElements.define(), do count, so they can start the handoff before the component renders. - An element that was upgraded before the restore reached it keeps its own root and ignores
og-handoff.
Any other value, an empty one too, is treated as adopt, and the browser warns restore_handoff_unknown.
<my-card
og-h="0"
og-shadow="my-card.shadow"
og-handoff="replace"
>
…
</my-card>Restored event
Each restored element hears ogygia:restored once the whole restore is done, in restore order. It bubbles and is composed, so the document hears it from an element inside a shadow root too. Its detail is { host, root }: host is the element’s restore key and root its shadow root.
When it fires, root holds the server’s tree and its sheets, the element’s light DOM is Svelte’s again, and its attributes are Svelte’s, plus og-keep and, on a custom element, the class tokens your render added. An element upgraded before the restore reached it, or one that can’t have a root, gets no event.
Region answers and incoming pages
A region answer’s or an incoming page’s elements are restored before they go in, and hear the event once they’re in the page. When the element is already defined (the usual case after the page loads), going in upgrades it first, so the event comes after its component ran and its constructor saw the root. With replace, its first write has handed off by then.
An element that doesn’t go into the page within about 30 seconds never hears it. When a morph keeps the element already in the page, the answer’s copy of it hears nothing.
The document’s own elements
The document’s own elements are restored while the page parses, before any module script runs, and the event isn’t replayed. To hear it for them, register the listener in a classic script in <head>, for example with script().
document.addEventListener('ogygia:restored', (e) => {
const { host, root } = (
e as CustomEvent<{ host: string; root: ShadowRoot }>
).detail;
// …
});<script>
import * as ogygia from 'ogygia';
</script>
<svelte:head>
{@html ogygia.script(() => {
document.addEventListener('ogygia:restored', (e) => {
const { root } = e.detail;
// …
});
})}
</svelte:head>Browser warnings
Four things can still go wrong in the browser. Each is said once per page and element name (or sheet key, or handoff value):
- A component runtime upgrades the element first. Then the element already has its own shadow root. ogygia keeps that root, puts the light DOM back into Svelte’s form and drops the server’s rendered copy (
restore_late). Until the component renders, that part of the page is empty. Load the component runtime after the page, so the restore reaches the element first. - The element can’t have a shadow root. Its name isn’t a valid custom element name, or it’s a built-in element. It keeps the server markup with ogygia’s marks taken off (
restore_host_invalid). This can happen when a sanitizer letsog-*attributes through on markup you didn’t render. - A sheet is missing. The element names a key in
og-shadowthat the page carries no sheet for, so its root goes without it (restore_sheet_missing). - The handoff value is unknown.
og-handoffis neitheradoptnorreplace, so the element is handed off asadopt(restore_handoff_unknown).