Skip to content

@ogygia/rewrite

Rewrite

@ogygia/rewrite runs your own pass over the HTML ogygia sends, such as a server render of web components or a rewrite of the whole page, even inside islands. rewrite() is a Kit handle, and before anything hydrates ogygia puts back whatever your pass moved in Svelte's markup.

On this page

Some apps run one more pass over the HTML after Svelte renders it: a server render for web components, a translation proxy, a tool that edits markup. Islands make that hard. An island hydrates against the exact markup Svelte rendered, so a pass that moves a node, trims a space or wraps a child breaks the hydration walk, and the island throws its server DOM away.

@ogygia/rewrite lets that pass run anyway. ogygia marks the HTML before your pass, reads what it did after, and in the browser puts back exactly what hydration needs before anything hydrates. Apps without such a pass don’t install the package and ship none of this.

In the example, a handle sends every my-* element on the page to a web-component renderer and puts what it returns in their place.

import { sequence } from '@sveltejs/kit/hooks';
import * as ogygia from 'ogygia/server';
import { rewrite } from '@ogygia/rewrite';
import { renderElement } from '$lib/server/renderer';

const components = rewrite({
  match: 'my-',
  render: (elements) =>
    elements.map((el) => renderElement(el.html))
});

export const handle = sequence(components, ogygia.handle());

Installation

Install the package, then list its Vite plugin beside ogygia(), before sveltekit(). The plugin takes no options. If you leave it out, every request that runs a rewrite fails with rewrite_plugin_missing, because nothing would put back what your pass moved.

The plugin and the handle share a name, rewrite, from two import paths: @ogygia/rewrite/vite for the plugin and @ogygia/rewrite for the handle.

pnpm add @ogygia/rewrite
import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { rewrite } from '@ogygia/rewrite/vite';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [ogygia(), rewrite(), sveltekit()]
});

Server handle

rewrite() from @ogygia/rewrite makes a Kit handle. List it before ogygia.handle() in sequence(). ogygia reads the request’s rewrites when its own handle starts, so a rewrite listed after it fails with rewrite_after_ogygia.

It takes one of two shapes:

  • { match, render } sends the custom elements match picks to render, a batch at a time, and puts each result in the element’s place. The rest of the page goes out byte for byte. See Custom elements.
  • { page } gets the whole document, or a whole region answer, as one string. See Page rewrites.

Both run on pages and on region answers (the HTML a server island or a lake fetches), so a web component inside a hole renders too. Several rewrites run in the order you list them, on one marking of the HTML (Multiple rewrites).

With postrender, list postrender.handle() first, then your rewrites, then ogygia.handle() (Combining packages).

import { sequence } from '@sveltejs/kit/hooks';
import * as ogygia from 'ogygia/server';
import { postrender } from '@ogygia/postrender';
import { components } from '$lib/server/components';

export const handle = sequence(
  postrender.handle(),
  components,
  ogygia.handle()
);

How it works

Your pass runs on HTML ogygia has marked: each region boundary, and each custom element ogygia will have to put back for Svelte (og-h). After your pass, ogygia reads what moved. In the browser, a small restorer runs before any island wakes and puts each marked element’s light DOM back exactly as Svelte rendered it, with your render moved into a shadow root. So the page paints your render, and Svelte hydrates its own markup.

An element is marked when it sits in markup Svelte hydrates (an island, or the whole body of a page Kit hydrates), when an island or a hole sits inside it, and always in a region answer. Any other element is yours to render any way you like, and nothing is restored. A page where nothing was restored ships no restorer. Restored elements covers what a render of a marked element must keep.

Why not transformPageChunk

Kit’s resolve(event, { transformPageChunk }) sees only the page, never a hole’s answer, and whatever it reshapes inside an island breaks hydration with nothing to put it back. Use rewrite() for any pass over ogygia’s HTML.

The old ogygia.handle({ transform }) option is gone: passing it fails with handle_transform_moved.

Integrations

Package contents

PageWhat it covers
Custom elementsrewrite({ match, render }), batches, streaming, stylesheets and failures.
Restored elementsWhat a render of a restored element returns, how the restore runs, and what can go wrong there.
Page rewritesrewrite({ page }), .html(), and several rewrites in one app.
Testing.check() in a test, and the dev check that compares every restored element.
@ogygia/rewriteThe generated API reference for the handle and its types.
@ogygia/rewrite/viteThe generated API reference for the plugin.

Moved sections

These sections used to be on this page, when it was the single Server transforms page. Each now lives on its own page: