genko.meDocs
Frameworks and languagesAstro

Tutorial

Adjust the @genko-me/astro components, run draft previews, and rebuild the public site with webhooks.

Add the features needed to operate the list, detail, and preview pages from Get started. Use the @genko-me/astro components as they are, and style them with your own CSS.

Adjust how fields render

Field renders according to the field type. Add a publish date (date and time) and a thumbnail (image) to see how. First add the fields to schema.ts.

src/lib/schema.ts
import { listApi } from "@genko-me/sdk";
export type Blog = {  title: string;  body: string;  date: string;  thumbnail: { url: string; alt: string };};
export const apis = {  blog: listApi<Blog>({    fields: [      { id: "title", type: "text" },      { id: "body", type: "richtext" },      { id: "date", type: "datetime" },      { id: "thumbnail", type: "image" },    ],  }),};
src/components/PostCard.astro
---import { Field } from "@genko-me/astro";import { client } from "../lib/client";
type Post = Awaited<ReturnType<typeof client.blog.getAll>>[number];interface Props {  post: Post;}const { post } = Astro.props;---<article>  <Field api={client.blog} entry={post} id="thumbnail" class="thumbnail">    <span slot="fallback" class="thumbnail">No image</span>  </Field>  <h2><a href={`/blog/entries/${post.id}`}><Field api={client.blog} entry={post} id="title" /></a></h2>  <Field api={client.blog} entry={post} id="date" format="MM/dd/yyyy" locale="en-US" timeZone="UTC" /></article>
  • Date and time values render as <time datetime>. format (using yyyy, MM, dd, HH, mm, and ss), locale, and timeZone change the output. timeZone defaults to UTC, so pass a zone such as Asia/Tokyo to show local time.
  • Images render as <img> with width, height, and alt carried over, and loading="lazy". Unsafe URLs such as javascript: are not rendered.
  • Empty values render nothing, or the content of the named fallback slot when you provide one.
  • Every Field accepts class and style. Passing lang for a multilingual entry sets the language of built-in labels such as Yes and No for boolean fields.

Rich text, which is output as HTML, can be rendered with either Field or RichText. Field follows the field type, while RichText inserts the string you pass as html.

Adjust pagination

Pagination takes the page from paginate() and renders previous and next links with the current page. Style it with your own CSS, and change the labels and language through props.

src/components/BlogPagination.astro
---import type { Page } from "astro";import { Pagination } from "@genko-me/astro";
interface Props {  page: Page<unknown>;}const { page } = Astro.props;---<Pagination page={page} previousLabel="Newer" nextLabel="Older" lang="en" class="blog-pagination" /><style>  .blog-pagination {    display: flex;    gap: 1rem;    justify-content: center;  }</style>

Attributes for <nav>, such as class, are passed through. When you omit previousLabel and nextLabel, the default labels follow lang (ja or en). On the first and last pages, the unavailable link renders as a <span> with aria-disabled. Change the number of entries per page with pageSize in paginate(posts, { pageSize: 10 }).

The page count changes when the total changes, so rebuild after adding or deleting entries (you can automate this with the webhook below).

Adjust the share buttons

ShareButtons takes services to choose which services appear and in what order.

src/pages/share-example.astro
---import { ShareButtons } from "@genko-me/astro";---<ShareButtons  url="https://your-site.example/blog/entries/post-1/"  title="Entry title"  services={["x", "line", "facebook", "hatena", "copy", "share"]}  lang="en"/>

services defaults to x, line, hatena, copy, and share. share opens the device share sheet and does not appear in browsers that lack one. Use class, buttonClass, and tooltipClass for CSS, and size for the icon size. Pass unstyled to skip the default design and style the buttons yourself. Button descriptions appear after a 400 ms hover and on keyboard focus, and the copy result appears above the pressed button for two seconds. The buttons also work with ClientRouter page transitions.

Run draft previews

The preview route (src/pages/blog/preview/[id].astro in Get started) differs from the public pages in these ways:

  • It is a prerender = false route and needs an SSR adapter. The draftKey is unknown at build time, so each request asks the delivery API. The public list and detail pages can stay static.
  • You set the response headers yourself. Use Cache-Control: private, no-store, X-Robots-Tag: noindex, nofollow, and Referrer-Policy: no-referrer. fetchGenkoEntry fetches from the API without caching but does not change the page's response headers.
  • Branch on status. Return 200 for ok, 404 for empty (no draftKey, an invalid one, or no such entry), and 503 for error. An invalid draftKey is never switched to the published entry, and logs for a failed fetch never include response bodies, URLs, or keys.

fetchGenkoEntry also accepts lang, langFallback, include, and depth. For an object-type API, omit id. When the result is ok, a PreviewNotice shows readers that they are looking at a draft.

src/pages/blog/preview/object.astro
---export const prerender = false;
import { Field, PreviewNotice } from "@genko-me/astro";import { fetchGenkoEntry } from "@genko-me/astro/server";import { client } from "../../../lib/client";
Astro.response.headers.set("Cache-Control", "private, no-store");Astro.response.headers.set("X-Robots-Tag", "noindex, nofollow");Astro.response.headers.set("Referrer-Policy", "no-referrer");
const result = await fetchGenkoEntry({  api: client.blog,  id: "post-1",  lang: "en",  searchParams: Astro.url.searchParams,});if (result.status !== "ok") Astro.response.status = result.status === "empty" ? 404 : 503;---<html lang="en">  <head>    <meta charset="utf-8" />    <meta name="robots" content="noindex,nofollow" />    <title>Preview</title>  </head>  <body>    {result.status === "ok" && (      <main>        <PreviewNotice lang="en">          <a href="/blog/entries/post-1">View the published version</a>        </PreviewNotice>        <Field api={client.blog} entry={result.data.entry} id="title" />      </main>    )}  </body></html>

PreviewNotice accepts children, so you can add a link to the published version. Always set Cache-Control for each preview route.

The API key is read with getSecret from astro:env/server. getSecret("GENKO_API_KEY") in client.ts reads the environment variable on the server at run time. The value is not embedded in the build output, so changing only the host's environment variable after deployment takes effect. import.meta.env embeds values at build time, which makes it a poor fit for rotating keys. For how your adapter passes environment variables, follow that adapter's documentation.

Rebuild with webhooks

Most of the public site is static. After you publish or update an entry, the old HTML remains live until a rebuild. Set the dashboard webhook destination to your host's deploy hook to rebuild automatically when an entry is saved. No receiver code is needed.

Subscribe to publish, update, delete, scheduled publish, and unpublish events. Without delete and unpublish, an entry you took down stays in the static HTML.

The draft preview route (the prerender = false route) asks the delivery API on every request, so it reads the latest draft without a rebuild. It needs no revalidation.

Only if a CDN or reverse proxy caches delivery API responses do you need a small API route that purges that cache. verifyGenkoSignature from @genko-me/sdk verifies the signature.

src/pages/api/webhook.ts
export const prerender = false;import type { APIRoute } from "astro";import { getSecret } from "astro:env/server";import { verifyGenkoSignature } from "@genko-me/sdk";
export const POST: APIRoute = async ({ request }) => {  const rawBody = await request.text();  const valid = await verifyGenkoSignature({    secret: getSecret("GENKO_WEBHOOK_SECRET")!,    rawBody,    signatureHeader: request.headers.get("x-genko-signature"),    timestampHeader: request.headers.get("x-genko-timestamp"),  });  if (!valid) return new Response(null, { status: 401 });
  // Call your CDN or reverse proxy's cache purge API here.  return new Response(null, { status: 204 });};

This route also needs prerender = false, so it requires an SSR adapter. Event types, payloads, and delivery limits are in Webhooks.

Next steps

Last updated: October 27, 2026

On this page