fix(admin): use collection urlPattern for preview button fallback URL (#181)

* fix(admin): use collection urlPattern for preview button fallback URL

The preview button hardcoded fallback URLs as /${collection}/${slug},
ignoring the collection's urlPattern setting. Collections with custom
URL patterns (e.g. urlPattern: "/biljke/{slug}" on a "biljka" collection)
would open a 404 instead of the correct page.

Thread urlPattern through the manifest and use it in the ContentEditor
preview fallback.

Fixes #167

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Signed-off-by: Filip Ilic <ilic.filip@gmail.com>

* chore: add changeset for preview URL pattern fix

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Signed-off-by: Filip Ilic <ilic.filip@gmail.com>

---------

Signed-off-by: Filip Ilic <ilic.filip@gmail.com>
Co-authored-by: Matt Kane <mkane@cloudflare.com>
This commit is contained in:
Filip Ilić
2026-04-04 21:00:28 +02:00
committed by GitHub
parent 3674d4857a
commit 9d10d2791f
5 changed files with 16 additions and 2 deletions

View File

@@ -363,6 +363,11 @@ export function ContentEditor({
const handlePreview = async () => {
if (!item?.id) return;
const contentUrl = (s: string) => {
const pattern = manifest?.collections[collection]?.urlPattern;
return pattern ? pattern.replace("{slug}", s) : `/${collection}/${s}`;
};
setIsLoadingPreview(true);
try {
const result = await getPreviewUrl(collection, item.id);
@@ -371,11 +376,11 @@ export function ContentEditor({
window.open(result.url, "_blank", "noopener,noreferrer");
} else {
// Fallback to direct URL if preview not configured
window.open(`/${collection}/${slug || item.id}`, "_blank", "noopener,noreferrer");
window.open(contentUrl(slug || item.id), "_blank", "noopener,noreferrer");
}
} catch {
// Fallback to direct URL on error
window.open(`/${collection}/${slug || item?.id}`, "_blank", "noopener,noreferrer");
window.open(contentUrl(slug || item?.id || ""), "_blank", "noopener,noreferrer");
} finally {
setIsLoadingPreview(false);
}