Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<item>.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
Expand Down
56 changes: 56 additions & 0 deletions hosted/src/pages/[slug].astro
Original file line number Diff line number Diff line change
@@ -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, 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, 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 cmsTitle = page ? resolveStarterSiteIdentity(await getSiteSettings()).siteTitle : undefined;

const seo = page && getSeoMeta(page, {
siteTitle: cmsTitle === "My Site" ? site.name : cmsTitle,
siteUrl: Astro.url.origin,
path: Astro.url.pathname.replace(/\/$/, ""),
});
---

{page && seo ? (
<Base
title={seo.title}
pageTitle={seo.ogTitle}
description={seo.description}
canonical={seo.canonical}
content={{ collection: "pages", id: contentId(page.data.id), slug }}
>
<div class="page wrap section">
<article>
<h1 {...page.edit.title}>{page.data.title}</h1>

<PortableText value={portableText(page.data.content)} />
</article>
</div>
</Base>
) : <ExampleLink />}
31 changes: 29 additions & 2 deletions scripts/gallery-sync.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ 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);
// 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() {
const list = async (directory, pattern) =>
Expand All @@ -55,6 +72,7 @@ async function manifest() {
"src/pages/[...example].astro",
"src/pages/blocks/[name].astro",
"src/pages/canvas/[name].astro",
...hosted,
...(await list("public/gallery", /\.svg$/)),
];
}
Expand Down Expand Up @@ -349,7 +367,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);
Expand All @@ -361,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 = [
Expand All @@ -374,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)
Expand All @@ -398,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` : ""}.`);
Expand Down
31 changes: 31 additions & 0 deletions src/components/gallery/ExampleLink.astro
Original file line number Diff line number Diff line change
@@ -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";
---

<Canvas title="Example link">
<main data-example-destination>
<h1>Example link</h1>
<p>On a real site this goes to <code>{Astro.url.pathname + Astro.url.search}</code>.</p>
<a href="/#blocks" target="_top">Browse the gallery</a>
</main>
</Canvas>

<style>
main {
inline-size: min(var(--width-text), 100% - 2 * var(--space-gutter));
margin-inline: auto;
padding-block: var(--section-gap);
overflow-wrap: anywhere;
}
h1 {
margin-block: 0;
font-size: var(--step-3);
}
a {
display: inline-flex;
align-items: center;
min-block-size: 44px;
}
</style>
28 changes: 2 additions & 26 deletions src/pages/[...example].astro
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
---
import Canvas from "../layouts/Canvas.astro";
import ExampleLink from "../components/gallery/ExampleLink.astro";

import catalog from "../data/gallery-catalog.json";

const path = Astro.url.pathname.replace(/\/$/, "");
if (!catalog.exampleDestinations.includes(path)) return new Response("Not found", { status: 404 });
---

<Canvas title="Example link">
<main data-example-destination>
<h1>Example link</h1>
<p>On a real site this goes to <code>{Astro.url.pathname + Astro.url.search}</code>.</p>
<a href="/#blocks" target="_top">Browse the gallery</a>
</main>
</Canvas>

<style>
main {
inline-size: min(var(--width-text), 100% - 2 * var(--space-gutter));
margin-inline: auto;
padding-block: var(--section-gap);
overflow-wrap: anywhere;
}
h1 {
margin-block: 0;
font-size: var(--step-3);
}
a {
display: inline-flex;
align-items: center;
min-block-size: 44px;
}
</style>
<ExampleLink />
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "astro/tsconfigs/strict",
"include": [".astro/types.d.ts", "**/*"],
"exclude": ["dist", "public/r"]
"exclude": ["dist", "public/r", "hosted"]
}
Loading