@genga-cms/sveltekit
Genga for SvelteKit. The site reads a Genga server over its public API (remote mode), or runs Genga in the SvelteKit server (embedded mode). Editors see the real page in the preview of the Genga admin, and Svelte blocks follow each keystroke.
| Package | SvelteKit | Svelte | Node |
|---|---|---|---|
@genga-cms/sveltekit |
^3.4.1 |
^5.57.1 |
>=22.17 |
The version of @genga-cms/sveltekit is the version of genga.
Set up
// src/lib/server/genga.ts: remote mode (or `createGenga()` of genga/server for embedded mode)import { createRemoteSource } from '@genga-cms/sveltekit';import config from '../../../genga.config.mjs';export const genga = createRemoteSource({ url: process.env.GENGA_URL!, schema: config });
// src/hooks.server.ts: first in `sequence`export const handle = sequence(gengaHandle(genga));
// src/routes/blog/[slug]/+page.server.tsexport const load = (event) => loadPage<PostData>(event, genga, 'posts', event.params.slug);<script lang="ts"> import { Blocks, gengaPreview } from '@genga-cms/sveltekit/components'; let { data } = $props(); const page = gengaPreview(() => data.doc, () => data.preview, () => data.adminOrigin);</script>
<h1>{page.current.data.title}</h1><Blocks blocks={page.current.data.body} registry={blocks} preview={data.preview} />blocks is defineBlockComponents(config, { hero: Hero }). On the Genga server, set previewUrl
of each collection to https://www.example.com{path}, GENGA_PREVIEW_ORIGINS to the site origin,
and GENGA_WEBHOOKS to [{"url":"https://www.example.com/api/genga/webhook","secret":"whsec_…"}].
The site reads the same secret from GENGA_WEBHOOK_SECRET.
How it works
- Hook.
gengaHandle(source, { webhookPath, webhookSecret, onEvent })receives the publish webhook: it checks the Standard Webhooks signature, purges the tags of the event, then callsonEvent(purge a CDN there). In embedded mode it also serves the admin app, the admin API and the media withgenga.handle. Behind a TLS proxy, setORIGINfor the CSRF check of SvelteKit, andBODY_SIZE_LIMITabovemedia.maxUploadBytesfor the uploads. - Reads.
loadPagereads the published document, or the draft for a preview URL of the editor (the token in remote mode, the admin session in embedded mode). It sets the Genga headers, redirects a renamed slug with 301 and throws a 404. The remote source keeps published reads in memory with their Genga cache tags, for one hour at most (maxAge). - Preview.
gengaPreviewconnects the page to the editor:currentfollows each change, and the token goes from the address bar to memory. - Blocks.
<Blocks>renders each block in a<svelte:boundary>.<RichText>renders the HTML of the allowlist renderer of Genga.
Limits of this release
- The read cache is in the memory of one process: each instance needs its own webhook.
- In a preview, a client navigation loses the token: the editor opens each page with its own.
- No
refreshAll()after an autosave, no rebuild hook for a prerendered site (useonEvent), and no reconciliation with/api/v1/events: a missed webhook expires aftermaxAge.