Skip to content

@ogygia/rewrite

Page rewrites

rewrite({ page }) gets the whole document or region answer as one string, for a pass that can't work element by element. Several rewrites run in the order you list them, on one marking of the HTML.

On this page

Some passes need the whole page: a print formatter, a translation proxy, a tool that edits markup across elements. rewrite({ page }) from @ogygia/rewrite gets the whole document, or the whole region answer, as one string, and returns the new one. It may be async.

In the example, a rewrite runs every page through a print formatter and leaves region answers alone.

import { rewrite } from '@ogygia/rewrite';
import { toPrintable } from '$lib/server/printable';

export const print = rewrite({
  page: async (html, ctx) =>
    ctx.kind === 'document' ? toPrintable(html) : html
});

Streaming

A document a page rewrite runs on is held until its last chunk and rewritten once, so a streamed page stops streaming. If you only need to touch some elements, { match, render } keeps the stream: it renders each chunk as it goes.

Failures

A page function that throws, or that drops ogygia’s og-h marks from an element ogygia restores, sends the HTML as ogygia rendered it and says rewrite_page_failed, once for that document or answer. In dev it throws instead, so a broken rewrite shows at once.

Carry every og-* attribute and comment along wherever the function moves a node, the same rule restored elements follow.

Plain strings

A page rewrite also has .html(str, ctx?), which runs the function on a plain string outside any request, with no marks: for a proxy or a script. ctx is optional, and each field you leave out has a default: kind is 'document', csr is false, and event, url and signal come from a GET for /.

import { mkdir, readFile, writeFile } from 'node:fs/promises';
import { rewrite } from '@ogygia/rewrite';

const stamp = rewrite({
  page: (html) =>
    html.replace(
      '</body>',
      '<footer>Printed copy</footer></body>'
    )
});

const html = await readFile(
  'build/prerendered/about.html',
  'utf8'
);
await mkdir('print', { recursive: true });
await writeFile(
  'print/about.html',
  await stamp
    .html
    (html)
);

Multiple rewrites

List them in order, all before ogygia.handle(). They run in that order on one marking of the HTML: a later one sees an earlier one’s output, marks intact. A rewrite listed after ogygia.handle() fails with rewrite_after_ogygia.

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

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

Conditional rewrites

A rewrite is recorded per request, so a rewrite for some requests only needs no option. Wrap it in a handle that calls it on those requests and passes the rest straight to resolve.

import type { Handle } from '@sveltejs/kit';
import { sequence } from '@sveltejs/kit/hooks';
import * as ogygia from 'ogygia/server';
import { rewrite } from '@ogygia/rewrite';
import { components } from '$lib/server/components';
import { renderFaq } from '$lib/server/faq';

const faq = rewrite({ match: 'my-faq', render: renderFaq });

const faqWhen: Handle = (input) =>
  input.event.url.pathname.startsWith('/help')
    ? faq(input)
    : input.resolve(input.event);

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