Newsletter
Join the Community
Subscribe to our newsletter for the latest news and updates
A SvelteKit library for previewing draft CMS content via a secret query parameter and cookie, inspired by Next.js Preview Mode.
SvelteKit Preview Mode is a drop-in SvelteKit library that lets developers preview draft or unpublished CMS content by adding a secret query parameter to any route. It implements a preview workflow inspired by Next.js Preview Mode, but built for the SvelteKit handle hook, and it is released as an open-source package under the ISC License.
SvelteKit Preview Mode is a TypeScript library that you install into an existing SvelteKit application with pnpm add sveltekit-preview-mode -D. It takes a PREVIEW_SECRET environment variable as input and, through a custom handle hook, checks incoming requests for a matching ?secret= query parameter. When the secret matches, it sets an isPreview flag on locals, persists the state in a cookie named __preview_mode by default, and exposes the status to client-side code through $page.data.isPreview. The repository is maintained by Darby Manning and includes ready-to-run examples for the Hygraph and Contentful CMSs.
handle hook integration — Wraps SvelteKit's request lifecycle so every route is checked for the preview secret without per-route code.__preview_mode cookie so preview state survives navigation and browser sessions.<PreviewBanner /> component you can drop into +layout.svelte to show editors when preview mode is active.cookieName, cookieOpts, exitPreviewQueryParam, and secretTokenQueryParam via the PreviewModeOptions interface.+layout.server.ts load function returns locals.exitPreviewQueryParam and locals.isPreview so client routes can react to preview state./preview/[slug] workaround for prerendered routes.?secret=your-secret appended; the cookie keeps preview mode active across pages./preview/[slug] route that redirects to the static page when not in preview mode, preserving SSG builds.<PreviewBanner /> component to display an exit-preview link that appends ?exit-preview to the current URL.After installing the library, you import previewMode in src/hooks.server.ts and pass it a previewSecret option loaded from $env/static/private. The handle function checks the secret query parameter against that value; on success it sets the isPreview flag and writes the cookie. To exit, add ?exit-preview to any route, which clears the flag and updates the cookie.
Yes, it is an open-source library released under the ISC License, and the repository accepts contributions via issues or pull requests.
Add a ?secret=PREVIEW_SECRET query parameter to any route. The secret must match the previewSecret value you configured in the previewMode handle function.
No, it relies on a server to analyze query parameters and cookies, so it is turned off during build. The README suggests creating a dynamic /preview/[slug] route that redirects back to the static route when preview mode is inactive.
Any CMS that supports draft preview URLs. The repository includes examples for Hygraph and Contentful, and you can adapt them to other providers.
Add the query parameter ?exit-preview to any route. The default exit parameter name can be changed with the exitPreviewQueryParam option.
