Skip to content

Live Preview

The admin’s Live Preview button opens your frontend with a preview_token query parameter. The token is signed, scoped to one entry, and expires after 15 minutes.

  1. In the CMS, set the preview URL in Settings → API. The entry template must contain {slug} and may also use {contentType}, e.g. https://your-site.com/blog/{slug}. Pages have their own template, which must contain {route}.
  2. In Astro, create a request-scoped client in middleware:
import { defineMiddleware } from 'astro:middleware';
import { createKenresoftClient, getPreviewToken } from '@kenresoft-cms/astro';
export const onRequest = defineMiddleware((context, next) => {
context.locals.cms = createKenresoftClient({
url: import.meta.env.PUBLIC_KENRESOFT_CMS_URL,
previewToken: getPreviewToken(context.url),
});
return next();
});
  1. Use it in pages:
const post = await Astro.locals.cms.entries.get({ contentType: 'blog-post', slug });

The page must render on demand: add export const prerender = false; where needed. Live Preview shows the saved entry, so save before previewing.

Without a valid token the normal public API still returns 404 for drafts.