Skip to content

Pages of records and a catalog

Every record of a collection — a product, a dish, an article — may have a page of its own. You write one template component. The owner adds a record in the editor, gets its page at once and edits it like any other page of the site.

It needs @seamlet/astro 1.1 or newer (shows and productFields from 1.2).

recordPages names the collection and the address of its pages: [record] is the place of the record in the address.

src/schema.ts
import {
collection,
defineSchema,
number,
recordPages,
slug,
text,
type ContentOf,
} from "@seamlet/astro/schema";
export const schema = defineSchema({
menu: collection({
label: "Меню",
itemTitle: "name",
item: {
name: text({ label: "Название" }),
price: number({ label: "Цена, ₽", min: 0 }),
slug: slug({ label: "Адрес страницы" }),
},
}),
});
export type Content = ContentOf<typeof schema>;
export type Drink = Content["menu"][number];
export const drinkPages = recordPages(schema, {
collection: "menu",
path: "/menu/[record]",
});

recordPages checks the collection by its types: a name the schema does not have does not compile. A collection inside a group is a path with dots: catalog.products.

src/seamlet.ts
import { defineSeamletSite } from "@seamlet/astro/site";
import DrinkPage from "./components/DrinkPage.astro";
import HomePage from "./components/HomePage.astro";
import MenuPage from "./components/MenuPage.astro";
import { initialContent } from "./initial-content";
import { drinkPages, schema } from "./schema";
export default defineSeamletSite({
schema,
initialContent,
pages: {
"/": { title: "Главная", component: HomePage },
"/menu": { title: "Меню", component: MenuPage },
},
recordPages: [{ pages: drinkPages, component: DrinkPage }],
});

The template gets the whole content, the record itself and its place in the collection — content, record and index. With index, the markers point at the fields of exactly this record:

src/components/DrinkPage.astro
---
import { editable } from "@seamlet/astro/schema";
import type { Content, Drink } from "../schema";
interface Props {
content: Content;
record: Drink;
index: number;
}
const { record, index } = Astro.props;
const edit = editable<Content>();
---
<article {...edit("menu", index)}>
<h1 {...edit("menu", index, "name")}>{record.name}</h1>
</article>

Serve all pages of the site — its own ones and the pages of records — from one route src/pages/[...page].astro. site.staticPaths(content) lists them for a static build, site.pageOf(rest, content) finds one by its address when served on request. Both give the component and its props:

src/pages/[...page].astro
---
import site from "../seamlet";
export async function getStaticPaths() {
return site.staticPaths(await site.loadPublishedContent());
}
const { component: Page, props } = Astro.props;
---
<Page {...props} />

If the site’s pages are separate files (index.astro, menu.astro), replace them with this route: it serves them too. site.pageAt shows only the site’s own pages and is deprecated.

pathOf gives the link to a record’s page:

{content.menu.map((drink) => (
<a href={drinkPages.pathOf(drink)}>{drink.name}</a>
))}

The address is the record’s slug field: Latin letters, digits and hyphens, different for every record of the collection. Until it is set, the page opens at the record’s id, so a record has its page at once. The owner sets the address in the editor: whatever they type, the editor writes it in Latin letters at once, and «Из названия» (From the name) takes the address from the record’s name. The address of a new record follows its name by itself until the record is saved; after that it does not change, even if the record is renamed — the page on the site does not move.

  • slug is a field of a record of a collection, one per record: the schema does not accept it anywhere else.
  • An address another record already has (as its address or as its id) is the issue taken-slug, one with other characters is invalid-slug. The CMS does not save the content until the owner fixes them.
  • Pages of records are one segment deeper than a page of the site: /catalog/[record], not /[record]. A record has one page, and a page of the site’s own cannot stand among the pages of records (/menu/specials beside /menu/[record]).
  • Every record of the initial content must have an address of its own — a slug or an id: its page is built before the CMS saves anything.

The build checks all of this the same way the CMS does and stops, explaining what to fix.

shows leaves the pages of some records off the site — for example, of hidden ones:

export const productPages = recordPages(schema, {
collection: "products",
path: "/catalog/[record]",
shows: (product) => !product.isHidden,
});

site.staticPaths and site.pageOf do not serve such pages, and pathOf gives them no address. The preview shows them: the owner prepares a product in the editor before showing it on the site.

searchTexts names the group of a record with the fields title and description — what search engines show for its page. The editor puts these fields first on the record’s page and shows how it will look in search results.

productFields() — the ready fields of a product for a record of a collection:

Field What it is
name name
slug the address of the page
photos photos: records with a photo field, the first is the main one
summary a line for a card
description the description for the product’s page
price price, ₽
priceNote a note on the price: «от» (from), «за м²» (per m²), «под заказ» (made to order)
characteristics characteristics — pairs of name and value: «Площадь» — «36 м²» (Area), «Материал» — «брус» (Material — timber)
isHidden «Скрыть с сайта» (Hide from the site)
seo title, description and image for search engines and links shared in messengers

Characteristics are a list of pairs, so the owner adds a new property without a developer. Put your own fields beside them — for example, a category:

src/schema.ts
import {
collection,
defineSchema,
productFields,
recordPages,
reference,
text,
} from "@seamlet/astro/schema";
export const schema = defineSchema({
categories: collection({
label: "Категории",
itemTitle: "name",
item: { name: text({ label: "Название" }) },
}),
products: collection({
label: "Товары",
itemTitle: "name",
item: {
...productFields(),
category: reference({ label: "Категория", collection: "categories" }),
},
}),
});
export const productPages = recordPages(schema, {
collection: "products",
path: "/catalog/[record]",
searchTexts: "seo",
shows: (product) => !product.isHidden,
});

You make the product card and the template of its page in your design once. After that the owner adds products, changes prices and photos, hides and reorders them, and edits the whole catalog as a table — with export to Excel and import back.

Pages of products are optional. Without recordPages products stay cards of the catalog, and the editor does not show the slug of productFields() — neither in the form, nor in the table, nor in the CSV file: an address without a page leads nowhere. Describe the pages later, and the «Адрес страницы» (Page address) field appears by itself.