feat(preview): implement preview mode for content management
Deploy / verify (push) Successful in 41s
Deploy / deploy (push) Successful in 1m3s

- Added support for a preview mode in the CMS, allowing draft content to be viewed without being indexed.
- Updated hooks and server logic to handle preview parameters, adjusting cache control and robots tags accordingly.
- Enhanced layout to display a preview notification when in preview mode, improving user experience for content editors.
This commit is contained in:
Peter Meier
2026-04-23 11:11:27 +02:00
parent 9216138a8c
commit 50bb0ef398
5 changed files with 61 additions and 7 deletions
+12 -4
View File
@@ -229,6 +229,8 @@ export type ContentGetOptions = {
locale?: string;
/** Felder, deren Referenzen aufgelöst werden (z. B. ["row1Content", "row2Content", "row3Content"]). */
resolve?: string[];
/** Signierter Preview-Token (HMAC), entsperrt Drafts für genau diesen Eintrag. Bypass Cache. */
preview?: string;
};
/** Führenden + abschließenden Slash entfernen (CMS "slug": "/about"; Route-Param mit trailing slash). */
@@ -245,13 +247,14 @@ export async function getPageBySlug(
): Promise<PageEntry | null> {
const resolveKey = options?.resolve?.join(",") ?? "";
const key = `page:slug:${slug}:${options?.locale ?? ""}:${resolveKey}`;
return cached("page", key, async () => {
const fetchFn = async () => {
const base = getBaseUrl();
const trySlug = async (s: string): Promise<Response> => {
const u = new URL(`${base}/api/content/page/${encodeURIComponent(s)}`);
if (options?.locale) u.searchParams.set("_locale", options.locale);
if (options?.resolve?.length)
u.searchParams.set("_resolve", options.resolve.join(","));
if (options?.preview) u.searchParams.set("preview", options.preview);
return fetch(u.toString());
};
const normSlug = normalizePageSlug(slug);
@@ -277,7 +280,9 @@ export async function getPageBySlug(
);
}
return (await res.json()) as PageEntry;
});
};
if (options?.preview) return fetchFn();
return cached("page", key, fetchFn);
}
/** Für Page-URLs wird `slug` bevorzugt (nicht _slug). Führender Slash wird ignoriert. */
@@ -435,13 +440,14 @@ export async function getPostBySlug(
): Promise<PostEntry | null> {
const resolveKey = options?.resolve?.join(",") ?? "";
const key = `post:slug:${slug}:${options?.locale ?? ""}:${resolveKey}`;
return cached("post", key, async () => {
const fetchFn = async () => {
const base = getBaseUrl();
const trySlug = async (s: string): Promise<Response> => {
const u = new URL(`${base}/api/content/post/${encodeURIComponent(s)}`);
if (options?.locale) u.searchParams.set("_locale", options.locale);
if (options?.resolve?.length)
u.searchParams.set("_resolve", options.resolve.join(","));
if (options?.preview) u.searchParams.set("preview", options.preview);
return fetch(u.toString());
};
const normSlug = normalizePostSlug(slug);
@@ -467,7 +473,9 @@ export async function getPostBySlug(
);
}
return (await res.json()) as PostEntry;
});
};
if (options?.preview) return fetchFn();
return cached("post", key, fetchFn);
}
/** Für Post-URLs wird slug bevorzugt (nicht _slug). Führender Slash wird ignoriert. */