Basker Docs

Build a site with Astro

Build a static Astro site from a Basker site with routes, route, and the FrontStage client

This guide builds an Astro site that renders every public URL of a Basker site. One catch-all route gets its paths from routes and its content from route. The examples use Astro 5 and https://www.example.org/graphql.

Create the project

npm create astro@latest basker-site -- --template minimal
cd basker-site

Add the FrontStage client. It is not on npm yet. Build it in a checkout of the Basker repository, then install it from its folder:

pnpm --filter @baskerhq/frontstage-client build
npm install /path/to/basker/packages/frontstage-client

To work without the client, send GET requests with fetch. See Send your first query.

Add the endpoint of your site to .env:

BASKER_GRAPHQL_URL=https://www.example.org/graphql

Create the client

src/lib/basker.ts
import { createFrontstageClient } from "@baskerhq/frontstage-client";

export const basker = createFrontstageClient({
  endpoint: import.meta.env.BASKER_GRAPHQL_URL,
});

export type RouteData = {
  route: {
    __typename: "RouteMatch" | "RouteRedirect" | "RouteNotFound";
    node?: RouteNode;
    to?: string;
    status?: 301 | 302;
    page?: { title: string } | null;
  };
};

export type RouteNode = {
  __typename: string;
  title: string;
  path: string;
  seo: { title: string | null; description: string | null };
  description?: string | null;
  startDate?: string | null;
  venue?: { title: string; path: string } | null;
  blocks: Array<{ __typename: string; type?: string; data?: Record<string, unknown> }>;
};

Write the queries

src/lib/queries.ts
export const SITE_QUERY = `{
  site { name defaultLocale locales { code } }
  navigation { title path }
}`;

export const ROUTES_QUERY = `query Routes($page: Int) {
  routes(page: $page, limit: 500) {
    hasNextPage
    docs { path }
  }
}`;

export const ROUTE_QUERY = `query Route($path: String!) {
  route(path: $path) {
    __typename
    ... on RouteRedirect { to status }
    ... on RouteNotFound { page { title } }
    ... on RouteMatch {
      node {
        __typename
        title
        path
        seo { title description }
        blocks {
          __typename
          ... on ThemeBlock { type data }
        }
        ... on Page { description }
        ... on Event {
          description
          startDate
          venue { title path }
        }
      }
    }
  }
}`;

Add the catch-all route

getStaticPaths lists the paths of every language. The page then resolves its own path with route.

src/pages/[...path].astro
---
import type { RouteData } from "../lib/basker";

import { basker } from "../lib/basker";
import { ROUTE_QUERY, ROUTES_QUERY, SITE_QUERY } from "../lib/queries";

type SiteData = { site: { locales: Array<{ code: string }> } };
type RoutesData = { routes: { hasNextPage: boolean; docs: Array<{ path: string }> } };

export async function getStaticPaths() {
  const { site } = await basker.query<SiteData>(SITE_QUERY);
  const paths = [];
  for (const { code } of site.locales) {
    for (let page = 1; ; page += 1) {
      const { routes } = await basker.query<RoutesData>(ROUTES_QUERY, { page }, { locale: code });
      for (const { path } of routes.docs) {
        const trimmed = path.replace(/^\/+|\/+$/g, "");
        paths.push({ params: { path: trimmed || undefined }, props: { locale: code } });
      }
      if (!routes.hasNextPage) {
        break;
      }
    }
  }
  return paths;
}

const { locale } = Astro.props;
const path = `/${Astro.params.path ?? ""}`;
const { route } = await basker.query<RouteData>(ROUTE_QUERY, { path }, { locale });
const node = route.node;
if (!node) {
  throw new Error(`No record for ${path}`);
}
---

<html lang={locale}>
  <head>
    <title>{node.seo.title ?? node.title}</title>
    {node.seo.description && <meta name="description" content={node.seo.description} />}
  </head>
  <body>
    <main>
      <h1>{node.title}</h1>
      {node.__typename === "Event" && node.startDate && (
        <p>
          {new Date(node.startDate).toLocaleString(locale)}
          {node.venue && <a href={node.venue.path}>{node.venue.title}</a>}
        </p>
      )}
      {node.description && <p>{node.description}</p>}
      {node.blocks.map(block => (
        <section data-block={block.type}>
          {typeof block.data?.title === "string" && <h2>{block.data.title}</h2>}
        </section>
      ))}
    </main>
  </body>
</html>

Replace the block markup with one component for each block type. See Theme blocks and Website builder blocks.

Add the 404 page

src/pages/404.astro
---
import { basker } from "../lib/basker";

const { page } = await basker.query<{ page: { title: string } | null }>(
  `{ page(slug: "404") { title } }`,
);
---

<h1>{page?.title ?? "Page not found"}</h1>

Build the site

npm run build

Astro writes one HTML file for each path that routes returns, in each language.

Render on request

To render each request on the server, set output: "server" and an adapter in astro.config.mjs. Remove getStaticPaths, and handle all three route results:

src/pages/[...path].astro
---
import type { RouteData } from "../lib/basker";

import { splitLocalePath } from "@baskerhq/frontstage-client";

import { basker } from "../lib/basker";
import { ROUTE_QUERY } from "../lib/queries";

const { site } = await basker.query<{ site: { defaultLocale: string; locales: Array<{ code: string }> } }>(
  `{ site { defaultLocale locales { code } } }`,
);
const path = `/${Astro.params.path ?? ""}`;
const { locale } = splitLocalePath(path, site.locales.map(l => l.code), site.defaultLocale);
const { route } = await basker.query<RouteData>(ROUTE_QUERY, { path }, { locale });

if (route.__typename === "RouteRedirect" && route.to) {
  return Astro.redirect(route.to, route.status);
}
if (route.__typename === "RouteNotFound") {
  Astro.response.status = 404;
}
const node = route.node;
---

{node ? <h1>{node.title}</h1> : <h1>{route.page?.title ?? "Page not found"}</h1>}

The edge cache serves repeated requests. See Caching.

Load data in the browser

For data that changes often, such as booking status, query the API from a client script. The API accepts requests from any origin:

<ul id="dates"></ul>
<script>
  import { createFrontstageClient } from "@baskerhq/frontstage-client";

  type Dates = {
    eventInstances: { docs: Array<{ startDate: string; ticketing: { status: string | null } | null }> };
  };

  const basker = createFrontstageClient({ endpoint: "https://www.example.org/graphql" });

  basker
    .query<Dates>(`{ eventInstances(limit: 10) { docs { startDate ticketing { status } } } }`)
    .then(({ eventInstances }) => {
      const list = document.querySelector("#dates");
      if (list) {
        list.innerHTML = eventInstances.docs
          .map(d => `<li>${new Date(d.startDate).toLocaleString()} ${d.ticketing?.status ?? ""}</li>`)
          .join("");
      }
    });
</script>

On this page