From 20c2d62050a3b051d3a04e8ed8d55351523f1b5b Mon Sep 17 00:00:00 2001 From: Jake Ruesink Date: Wed, 2 Dec 2026 22:50:41 -0600 Subject: [PATCH 1/2] fix(sbo-41): hosted gallery example links on one-segment paths On the Site Boulder draft, Starter v14's CMS route src/pages/[slug].astro outranks [...example].astro for one-segment paths and rewrites a missing page to /404, which keeps status 404, so links like /menu 404'd on the hosted gallery. The gallery now owns the draft's [slug].astro: Starter v14's file unchanged except that, when no CMS page exists and the path is a declared example destination, it renders the same example page as [...example].astro with status 200. CMS pages still win; any other path keeps the true 404. The example markup moves into components/gallery/ExampleLink.astro, which both routes render. The file imports the draft's EmDash runtime, so it lives at hosted/src/pages/[slug].astro, excluded from the local site and astro check, and gallery-sync writes it without the hosted/ prefix. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 2 +- hosted/src/pages/[slug].astro | 56 ++++++++++++++++++++++++ scripts/gallery-sync.mjs | 8 +++- src/components/gallery/ExampleLink.astro | 31 +++++++++++++ src/pages/[...example].astro | 28 +----------- tsconfig.json | 2 +- 6 files changed, 98 insertions(+), 29 deletions(-) create mode 100644 hosted/src/pages/[slug].astro create mode 100644 src/components/gallery/ExampleLink.astro diff --git a/AGENTS.md b/AGENTS.md index a5a5346..64ada72 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -16,7 +16,7 @@ Astro Blocks is a shadcn-compatible **source registry** of native `.astro` websi - `src/components/blocks/`: sections (headers, heroes, walkthroughs, features, content, footers). `src/components/ui/`: primitives. `src/styles/tokens.css`: the token contract. - Composed pages are optional gallery inspiration, never registry items. Add one only when its art direction is exceptional; the blocks come first. The Lake weekend decision guide was retired from the gallery and stays at tag `v0.1.4`. - `src/components/examples/.astro`: one usage example per item. The gallery renders it live and shows it as the usage code, so docs cannot drift from the preview. -- `src/pages/`, `src/components/gallery/`, `src/styles/gallery-*.css`, `src/data/`: the gallery site, synced to a Site Boulder draft. +- `src/pages/`, `src/components/gallery/`, `src/styles/gallery-*.css`, `src/data/`: the gallery site, synced to a Site Boulder draft. `hosted/` holds gallery files that import the draft's EmDash runtime, such as its `[slug].astro` (Starter v14's CMS route plus the example-link fallback); the sync writes each without the `hosted/` prefix, and the local site never builds them. - Generated, never hand-edited: `public/fonts/*.css`, `src/data/font-pack.json` and the `font-*` entries in `registry.json` (`scripts/build-fonts.mjs`), `public/r/*.json` (`shadcn build`), `src/data/catalog.json` (props, slots, usage and hashes derived from source, which Site Boulder's registry reads), and `src/data/gallery-catalog.json` (the same without usage, which the gallery reads; its block pages show each example's own source). ## Conventions the build enforces diff --git a/hosted/src/pages/[slug].astro b/hosted/src/pages/[slug].astro new file mode 100644 index 0000000..a5f6a4c --- /dev/null +++ b/hosted/src/pages/[slug].astro @@ -0,0 +1,56 @@ +--- +import { contentId, portableText } from "../utils/cms-fields"; +import { getEmDashEntry, getSeoMeta, decodeSlug, getSiteSettings } from "emdash"; +import { PortableText } from "emdash/ui"; +import Base from "../layouts/Base.astro"; +import { resolveStarterSiteIdentity } from "../utils/site-identity"; +import ExampleLink from "../components/gallery/ExampleLink.astro"; + +import site from "../data/site.json"; +import catalog from "../data/gallery-catalog.json"; + +const slug = decodeSlug(Astro.params.slug); + +if (!slug) { + return Astro.rewrite("/404"); +} + +const { entry: page, cacheHint } = await getEmDashEntry("pages", slug); + +// Astro Blocks gallery, not Starter v14: this route outranks [...example].astro for one-segment paths, and a +// rewrite to /404 keeps status 404. So a usage example's destination (such as /menu) with no CMS page renders +// the gallery's example page here with status 200. A CMS page still wins, and any other path stays a true 404. +const example = !page && catalog.exampleDestinations.includes(Astro.url.pathname.replace(/\/$/, "")); + +if (!page && !example) { + return Astro.rewrite("/404"); +} + +if (page && Astro.cache?.enabled) Astro.cache.set(cacheHint); +const { siteTitle: cmsTitle } = resolveStarterSiteIdentity(await getSiteSettings()); +const siteTitle = cmsTitle === "My Site" ? site.name : cmsTitle; + +const seo = page && getSeoMeta(page, { + siteTitle, + siteUrl: Astro.url.origin, + path: Astro.url.pathname.replace(/\/$/, ""), +}); +--- + +{page && seo ? ( + +
+
+

{page.data.title}

+ + +
+
+ +) : } diff --git a/scripts/gallery-sync.mjs b/scripts/gallery-sync.mjs index 457a6b1..7706c40 100644 --- a/scripts/gallery-sync.mjs +++ b/scripts/gallery-sync.mjs @@ -30,6 +30,11 @@ const pendingFile = `${tokenFile}.pending`; const scope = "openid offline_access site:read site:draft site:preview"; const receipt = { calls: 0, requestBytes: 0, responseBytes: 0, phases: {} }; +// Gallery files that run only on the draft, because they import its EmDash runtime. Each one lives under hosted/ +// here, outside the local site, and is written to the draft at the same path without that prefix. +const hosted = ["src/pages/[slug].astro"]; +const source = (path) => (hosted.includes(path) ? `hosted/${path}` : path); + // Gallery-owned files. Blocks arrive through registry_add; the draft keeps its own Base and tokens. async function manifest() { const list = async (directory, pattern) => @@ -55,6 +60,7 @@ async function manifest() { "src/pages/[...example].astro", "src/pages/blocks/[name].astro", "src/pages/canvas/[name].astro", + ...hosted, ...(await list("public/gallery", /\.svg$/)), ]; } @@ -349,7 +355,7 @@ async function sync() { // The write phase sends exactly these contents, so a file edited mid-sync can't slip past the checks. const contents = new Map(); for (const path of paths) { - contents.set(path, await readFile(join(root, path), "utf8")); + contents.set(path, await readFile(join(root, source(path)), "utf8")); next.set(path, bytes(contents.get(path))); } const removed = [...present, ...leaving].reduce((sum, path) => sum + bytes(current.get(path).content), 0); diff --git a/src/components/gallery/ExampleLink.astro b/src/components/gallery/ExampleLink.astro new file mode 100644 index 0000000..58c1b4a --- /dev/null +++ b/src/components/gallery/ExampleLink.astro @@ -0,0 +1,31 @@ +--- +// The page a usage example's realistic link (such as /menu) opens. [...example].astro renders it, and +// on the hosted draft so does hosted/src/pages/[slug].astro, which outranks it for one-segment paths. +import Canvas from "../../layouts/Canvas.astro"; +--- + + +
+

Example link

+

On a real site this goes to {Astro.url.pathname + Astro.url.search}.

+ Browse the gallery +
+
+ + diff --git a/src/pages/[...example].astro b/src/pages/[...example].astro index 853403e..3d891d7 100644 --- a/src/pages/[...example].astro +++ b/src/pages/[...example].astro @@ -1,5 +1,5 @@ --- -import Canvas from "../layouts/Canvas.astro"; +import ExampleLink from "../components/gallery/ExampleLink.astro"; import catalog from "../data/gallery-catalog.json"; @@ -7,28 +7,4 @@ const path = Astro.url.pathname.replace(/\/$/, ""); if (!catalog.exampleDestinations.includes(path)) return new Response("Not found", { status: 404 }); --- - -
-

Example link

-

On a real site this goes to {Astro.url.pathname + Astro.url.search}.

- Browse the gallery -
-
- - + diff --git a/tsconfig.json b/tsconfig.json index ce5e7bb..b3bf8ea 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,5 @@ { "extends": "astro/tsconfigs/strict", "include": [".astro/types.d.ts", "**/*"], - "exclude": ["dist", "public/r"] + "exclude": ["dist", "public/r", "hosted"] } From 66ef0e87ddcc97b5d65784bbdee7e847a36211fa Mon Sep 17 00:00:00 2001 From: Jake Ruesink Date: Wed, 2 Dec 2026 23:14:29 -0600 Subject: [PATCH 2/2] SBO-41: gallery [slug] fallback only after an error-free CMS lookup; sync never replaces a customized route - A failed CMS lookup keeps the starter's 404 instead of serving the example page. - Site settings are read only on the CMS branch. - gallery-sync replaces the draft's [slug].astro only when it is an untouched starter copy (v1-v15 hashes) or a committed gallery version, and aborts if it changed after planning. Co-Authored-By: Claude Opus 5.5 --- hosted/src/pages/[slug].astro | 12 ++++++------ scripts/gallery-sync.mjs | 23 ++++++++++++++++++++++- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/hosted/src/pages/[slug].astro b/hosted/src/pages/[slug].astro index a5f6a4c..14d38ff 100644 --- a/hosted/src/pages/[slug].astro +++ b/hosted/src/pages/[slug].astro @@ -15,23 +15,23 @@ if (!slug) { return Astro.rewrite("/404"); } -const { entry: page, cacheHint } = await getEmDashEntry("pages", slug); +const { entry: page, cacheHint, error } = await getEmDashEntry("pages", slug); // Astro Blocks gallery, not Starter v14: this route outranks [...example].astro for one-segment paths, and a // rewrite to /404 keeps status 404. So a usage example's destination (such as /menu) with no CMS page renders -// the gallery's example page here with status 200. A CMS page still wins, and any other path stays a true 404. -const example = !page && catalog.exampleDestinations.includes(Astro.url.pathname.replace(/\/$/, "")); +// the gallery's example page here with status 200. A CMS page still wins, a failed lookup keeps the starter's +// 404 (it could be hiding a real page), and any other path stays a true 404. +const example = !page && !error && catalog.exampleDestinations.includes(Astro.url.pathname.replace(/\/$/, "")); if (!page && !example) { return Astro.rewrite("/404"); } if (page && Astro.cache?.enabled) Astro.cache.set(cacheHint); -const { siteTitle: cmsTitle } = resolveStarterSiteIdentity(await getSiteSettings()); -const siteTitle = cmsTitle === "My Site" ? site.name : cmsTitle; +const cmsTitle = page ? resolveStarterSiteIdentity(await getSiteSettings()).siteTitle : undefined; const seo = page && getSeoMeta(page, { - siteTitle, + siteTitle: cmsTitle === "My Site" ? site.name : cmsTitle, siteUrl: Astro.url.origin, path: Astro.url.pathname.replace(/\/$/, ""), }); diff --git a/scripts/gallery-sync.mjs b/scripts/gallery-sync.mjs index 7706c40..6d29205 100644 --- a/scripts/gallery-sync.mjs +++ b/scripts/gallery-sync.mjs @@ -34,6 +34,18 @@ const receipt = { calls: 0, requestBytes: 0, responseBytes: 0, phases: {} }; // here, outside the local site, and is written to the draft at the same path without that prefix. const hosted = ["src/pages/[slug].astro"]; const source = (path) => (hosted.includes(path) ? `hosted/${path}` : path); +// A hosted file replaces a draft route the starter created. The sync replaces it only when the draft copy is an +// untouched Site Boulder starter copy (sha256 of every starter's version, v1-v15) or a committed gallery version; +// anything else was customized on the draft and must be merged by hand. +const starterCopies = { + "src/pages/[slug].astro": new Set([ + "8dcbe33f24c7e21e48c591829d3a1e415f5931445388bbd6f2b67bd4b6384207", // v1-v2 + "37008ee01fbf2f293c479f0716c11bdd44524a6ec56d0c424b3ca18131117334", // v3-v4 + "b84020c36ad4892d76850860c0116d8bfe01bfe4b5218eb731b24f42d06e9e03", // v5-v6 + "16c1933773d3a51b590f43d05339576e8f789851df167873a68ddb1833932a84", // v7-v15 + ]), +}; +const sha256 = (text) => createHash("sha256").update(text).digest("hex"); // Gallery-owned files. Blocks arrive through registry_add; the draft keeps its own Base and tokens. async function manifest() { @@ -367,6 +379,12 @@ async function sync() { `plan: read ${present.length} managed files${leaving.length ? ` and ${leaving.length} retired` : ""} (${current.reread} needed a second read); source ${listing.budget.bytes} of ${listing.budget.limitBytes} bytes now, ${projected} after this sync; ${files} of ${listing.budget.limitFiles} files${leaving.length ? ` (${peak} before the retired go)` : ""}.`, ); const edited = leaving.filter((path) => !released(path).has(current.get(path).content)); + const customized = hosted.filter((path) => { + const draft = current.get(path)?.content; + return draft !== undefined && draft !== contents.get(path) && !starterCopies[path]?.has(sha256(draft)) && !released(source(path)).has(draft); + }); + // The write phase must find these exact draft contents again before replacing them. + const guarded = new Map(hosted.filter((path) => current.has(path)).map((path) => [path, current.get(path).content])); // Each registry_add call writes its items' new closure files, and one item's closure can't be split across // calls; edit_site writes gallery files one by one at most. const oversized = [ @@ -380,11 +398,12 @@ async function sync() { peak > listing.budget.limitFiles && `it needs ${peak} files${leaving.length ? " before the retired ones go" : ""} and the draft allows ${listing.budget.limitFiles}`, oversized.length && `the platform refuses a file over ${maxFileBytes} bytes and a registry_add call over ${maxAddWrites} files or ${maxFileBytes} bytes, so ${oversized.join(", ")} would fail after earlier writes landed`, edited.length && `${edited.join(", ")} changed on the draft after a release synced it, so the sync won't remove it; merge or delete it by hand`, + customized.length && `${customized.join(", ")} on the draft is neither a starter copy nor a committed gallery version, so the sync won't replace it; merge it by hand`, ].filter(Boolean); if (problems.length) throw new Error(`This sync can't land: ${problems.join("; ")}. No registry or gallery files were written.`); // Removed with the revision read here, so a change made after planning fails the delete instead of being lost. const retire = leaving.map((path) => ({ path, expectedRevision: current.get(path).revision, expectedFileId: current.get(path).fileId })); - return { listing, contents, retire }; + return { listing, contents, retire, guarded }; }); if (!dryRun) @@ -404,6 +423,8 @@ async function sync() { const content = plan.contents.get(path); const existing = current.get(path); if (existing?.content === content) continue; + if (plan.guarded.has(path) && existing?.content !== plan.guarded.get(path)) + throw new Error(`${path} changed on the draft after planning; rerun the sync. No gallery files were written.`); writes.push(existing ? { path, content, expectedRevision: existing.revision, expectedFileId: existing.fileId } : { path, content, expectedRevision: 0 }); } console.log(`${paths.length} gallery files: ${writes.length} to write, ${paths.length - writes.length} unchanged${plan.retire.length ? `; ${plan.retire.length} retired to remove` : ""}.`);