SvelteKit
Use Edytor in SvelteKit, with server rendering, readonly pages, editors connected to a room and headless document work on the server.
Edytor works in SvelteKit with no extra configuration: SvelteKit’s Vite plugin compiles the .svelte files the package ships. This page covers what the component renders during server-side rendering (SSR).
<Edytor> renders on the server in every case, with no guard in your page. An editor with a value server-renders it. An editor connected to a room (room, with or without server) server-renders the snapshot you pass it, or nothing; it opens IndexedDB and its socket only in the browser, then shows the room’s live content in place of the snapshot. To have the document in the first HTML, load its snapshot in your server load:
import { documentSnapshot } from 'edytor';
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ params, fetch }) => {
const snapshot = await documentSnapshot({
server: 'wss://rooms.example.com/rooms',
room: params.id,
fetch
});
return { room: params.id, snapshot: snapshot ?? undefined };
};
<script lang="ts">
import { Edytor } from 'edytor';
let { data } = $props();
</script>
<Edytor server="wss://rooms.example.com/rooms" room={data.room} snapshot={data.snapshot} />
The snapshot is read-only until the room answers, then the live editor replaces it. Without a snapshot the server HTML holds the editor’s place and the content appears once the browser has it (from IndexedDB on a return visit). See first paint from a snapshot.
What renders on the server
<Edytor> renders its block tree only once its document has content. During SSR that depends on the props:
| Props | Server output |
|---|---|
readonly with a value |
The full document: every block element, its text and marks, with contenteditable="false" and aria-readonly="true". |
room or sync |
Its snapshot, read-only, or nothing. The provider attaches only in the browser, and the document stays pending until it answers. |
document that is not ready yet |
Nothing, for the same reason. |
value only (editable), or no props |
The block tree, with contenteditable="true". It hydrates in the browser and edits from there. |
A view releases the document it created when the server render ends, so a server-rendered editor keeps no document or presence timer alive after the request.
Some things never run on the server: event listeners, the selection, block handles, menus and the overlay layer, remote carets and plugins’ onEdytorAttached / onBlockAttached hooks. They start when the component mounts in the browser. The server HTML therefore has no handles. It does carry the root, block and mark attributes (data-edytor, data-edytor-type, data-edytor-id, data-edytor-mark), so styles that target them apply before hydration; data-edytor-text on text segments is added on mount.
Readonly pages
To show a document nobody edits on the page, render a readonly editor. Load the JSON on the server and pass it as value:
import type { JSONDoc } from 'edytor';
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ params }) => {
const value: JSONDoc = await loadNote(params.id); // your storage
return { value };
};
<script lang="ts">
import { Edytor, createCodePlugin } from 'edytor';
let { data } = $props();
</script>
<Edytor readonly value={data.value} plugins={[createCodePlugin()]} />
Pass the same plugins you use to edit. A block type no plugin defines renders as a plain block (its text and children) and logs a warning in development.
A readonly view given room and server (or sync) is a live viewer instead: it loads the room’s content in the browser and shows every later edit. Its server render waits for the room as an editable view’s does, so pass the content you loaded as snapshot to paint it at once (see Readonly mode).
See Readonly mode for what still works in the browser.
Working with documents on the server
Load and change documents on the server without the component through edytor/crdt/edytor. It has no Svelte in its import graph, so it runs in +page.server.ts, +server.ts and hooks:
import { json } from '@sveltejs/kit';
import { loadDocument } from 'edytor/crdt/edytor';
import type { RequestHandler } from './$types';
export const GET: RequestHandler = async ({ params }) => {
const bytes: Uint8Array = await readStoredUpdate(params.id); // your storage
const document = loadDocument(bytes);
const value = document.toJSON();
document.destroy();
return json(value);
};
document.encode() gives the bytes to store; loadDocument(bytes) restores them. The document API is described in the document reference.