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

@@ -0,0 +1,6 @@
---
"@emdash-cms/admin": patch
"emdash": patch
---
fix(admin): use collection urlPattern for preview button fallback URL

View File

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

View File

@@ -55,6 +55,7 @@ export interface AdminManifest {
labelSingular: string; labelSingular: string;
supports: string[]; supports: string[];
hasSeo: boolean; hasSeo: boolean;
urlPattern?: string;
fields: Record< fields: Record<
string, string,
{ {

View File

@@ -28,6 +28,7 @@ export interface ManifestCollection {
labelSingular: string; labelSingular: string;
supports: string[]; supports: string[];
hasSeo: boolean; hasSeo: boolean;
urlPattern?: string;
fields: Record< fields: Record<
string, string,
{ {

View File

@@ -1180,6 +1180,7 @@ export class EmDashRuntime {
labelSingular: collection.labelSingular || collection.label, labelSingular: collection.labelSingular || collection.label,
supports: collection.supports || [], supports: collection.supports || [],
hasSeo: collection.hasSeo, hasSeo: collection.hasSeo,
urlPattern: collection.urlPattern,
fields, fields,
}; };
} }