From 9d10d2791fe16be901d9d138e434bd79cf9335c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Ili=C4=87?= Date: Sat, 4 Apr 2026 21:00:28 +0200 Subject: [PATCH] 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) Signed-off-by: Filip Ilic * chore: add changeset for preview URL pattern fix Co-Authored-By: Claude Opus 4.6 (1M context) Signed-off-by: Filip Ilic --------- Signed-off-by: Filip Ilic Co-authored-by: Matt Kane --- .changeset/fix-preview-url-pattern.md | 6 ++++++ packages/admin/src/components/ContentEditor.tsx | 9 +++++++-- packages/admin/src/lib/api/client.ts | 1 + packages/core/src/astro/types.ts | 1 + packages/core/src/emdash-runtime.ts | 1 + 5 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 .changeset/fix-preview-url-pattern.md diff --git a/.changeset/fix-preview-url-pattern.md b/.changeset/fix-preview-url-pattern.md new file mode 100644 index 0000000..53369c7 --- /dev/null +++ b/.changeset/fix-preview-url-pattern.md @@ -0,0 +1,6 @@ +--- +"@emdash-cms/admin": patch +"emdash": patch +--- + +fix(admin): use collection urlPattern for preview button fallback URL diff --git a/packages/admin/src/components/ContentEditor.tsx b/packages/admin/src/components/ContentEditor.tsx index d7ddf01..d55d82e 100644 --- a/packages/admin/src/components/ContentEditor.tsx +++ b/packages/admin/src/components/ContentEditor.tsx @@ -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); } diff --git a/packages/admin/src/lib/api/client.ts b/packages/admin/src/lib/api/client.ts index 721b113..b46a849 100644 --- a/packages/admin/src/lib/api/client.ts +++ b/packages/admin/src/lib/api/client.ts @@ -55,6 +55,7 @@ export interface AdminManifest { labelSingular: string; supports: string[]; hasSeo: boolean; + urlPattern?: string; fields: Record< string, { diff --git a/packages/core/src/astro/types.ts b/packages/core/src/astro/types.ts index c216b12..e5b8b6f 100644 --- a/packages/core/src/astro/types.ts +++ b/packages/core/src/astro/types.ts @@ -28,6 +28,7 @@ export interface ManifestCollection { labelSingular: string; supports: string[]; hasSeo: boolean; + urlPattern?: string; fields: Record< string, { diff --git a/packages/core/src/emdash-runtime.ts b/packages/core/src/emdash-runtime.ts index e5a7411..810b0bb 100644 --- a/packages/core/src/emdash-runtime.ts +++ b/packages/core/src/emdash-runtime.ts @@ -1180,6 +1180,7 @@ export class EmDashRuntime { labelSingular: collection.labelSingular || collection.label, supports: collection.supports || [], hasSeo: collection.hasSeo, + urlPattern: collection.urlPattern, fields, }; }