Recipes

Practical recipes and patterns for building sites with Stati. Each recipe solves a common use case with code you can adapt. A recipe that starts with a site/partials/ file name, such as site/partials/menu.eta, is a template to include from a layout: <%~ stati.include('partials/menu') %>. The others are layouts: they say which pages they lay out, and wrap themselves in the page shell site/base.eta with stati.layout('base') (see Sharing a shell). Links to the site’s own pages go through stati.url(), so the templates also work on a site served from a subfolder.

Content management

Dynamic navigation menu

Automatically generate navigation from your content structure using Stati’s built-in navigation:

<!-- site/partials/menu.eta -->
<nav class="menu">
  <% function renderNavigation(items, level = 0) { %>
    <ul class="<%= stati.propValue('menu-level', `level-${level}`) %>">
      <% items.forEach(item => { %>
        <li class="menu-item">
          <a
            href="<%= stati.url(item.url) %>"
            class="<%= stati.propValue('menu-link', stati.page.url === item.url && 'active') %>"
          >
            <%= item.title %>
          </a>
          <% if (item.children && item.children.length > 0) { %>
            <% renderNavigation(item.children, level + 1) %>
          <% } %>
        </li>
      <% }) %>
    </ul>
  <% } %>
  <% renderNavigation(stati.nav.tree) %>
</nav>

Call a template function that writes markup with <% %>: it adds to the output as it runs and returns nothing, so <%~ renderNavigation(...) %> would print undefined. Every folder of pages is a top-level node in stati.nav.tree, so this menu has at most two levels, with nested folders listed at the top (see Tree shape).

Section-specific navigation

Use stati.page.navNode to show the current page’s child pages:

<!-- site/partials/section-nav.eta -->
<% if (stati.page.navNode?.children && stati.page.navNode.children.length > 0) { %>
  <nav class="section-navigation">
    <h3><%= stati.page.navNode.title %></h3>

    <!-- Show all child pages of the current page -->
    <ul>
      <% stati.page.navNode.children.forEach(page => { %>
        <li>
          <a
            href="<%= stati.url(page.url) %>"
            class="<%= stati.page.url === page.url ? 'active' : '' %>"
          >
            <%= page.title %>
          </a>
        </li>
      <% }) %>
    </ul>
  </nav>
<% } %>

navNode.children is only set on collection pages (a folder’s index.md), and the children are the pages in that folder. A subfolder appears there as a single page without its own children.

The navigation tree is flat: every folder with pages is a top-level node. A nested folder’s index.md therefore also appears, without children, among its parent folder’s pages, and stati.page.navNode can be that entry. On such a page, look its node up among the top-level nodes instead: stati.nav.tree.find((node) => node.url === stati.page.url)?.children ?? [].

Common navigation patterns:

  • Current page’s children - Use stati.page.navNode.children
  • Any section’s children - Use stati.nav.getChildren('/path')
  • Current page’s siblings - Use stati.nav.getSiblings()
<!-- site/partials/docs-sidebar.eta -->
<!-- Show children of the docs section, regardless of current page -->
<aside class="docs-sidebar">
  <h3>Documentation</h3>
  <ul>
    <% stati.nav.getChildren('/docs').forEach(page => { %>
      <li>
        <a
          href="<%= stati.url(page.url) %>"
          class="<%= stati.page.url === page.url ? 'active' : '' %>"
        >
          <%= page.title %>
        </a>
      </li>
    <% }) %>
  </ul>
</aside>
<!-- site/partials/sibling-nav.eta -->
<!-- Show siblings of the current page -->
<% const siblings = stati.nav.getSiblings() %>

<% if (siblings.length > 0) { %>
  <nav class="sibling-nav">
    <h3>Related Pages</h3>
    <ul>
      <% siblings.forEach(sibling => { %>
        <li>
          <a href="<%= stati.url(sibling.url) %>"><%= sibling.title %></a>
        </li>
      <% }) %>
    </ul>
  </nav>
<% } %>

Table of contents (in-page navigation)

Stati automatically extracts headings (h2-h6) and makes them available via stati.page.toc:

<!-- site/partials/toc.eta -->
<% if (stati.page.toc && stati.page.toc.length > 0) { %>
  <nav class="table-of-contents">
    <h2>On this page</h2>
    <ul>
      <% stati.page.toc.forEach(entry => { %>
        <li class="<%= stati.propValue(`toc-level-${entry.level}`) %>">
          <a href="<%= `#${entry.id}` %>"><%= entry.text %></a>
        </li>
      <% }) %>
    </ul>
  </nav>
<% } %>

Each TOC entry contains:

  • id - Anchor ID for linking (e.g., getting-started)
  • text - Plain text heading content
  • level - Heading level (2-6)

For hierarchical TOC with nesting:

<!-- site/partials/hierarchical-toc.eta -->
<%
  // Build a nested tree structure from flat TOC entries
  function buildTocTree(entries) {
    const root = { children: [] };
    const stack = [{ node: root, level: 1 }];

    entries.forEach(entry => {
      const newNode = { ...entry, children: [] };

      // Pop stack until we find the parent level
      while (stack.length > 1 && stack[stack.length - 1].level >= entry.level) {
        stack.pop();
      }

      // Add as child of current parent
      stack[stack.length - 1].node.children.push(newNode);
      stack.push({ node: newNode, level: entry.level });
    });

    return root.children;
  }

  // Recursive template function for rendering TOC tree
  function renderTocTree(items) { %>
    <ul>
      <% items.forEach(item => { %>
        <li>
          <a href="<%= `#${item.id}` %>"><%= item.text %></a>
          <% if (item.children && item.children.length > 0) { %>
            <% renderTocTree(item.children) %>
          <% } %>
        </li>
      <% }) %>
    </ul>
  <% }
%>

<% if (stati.page.toc && stati.page.toc.length > 0) { %>
  <nav class="toc-hierarchical">
    <h2>Contents</h2>
    <% const tocTree = buildTocTree(stati.page.toc) %>
    <% renderTocTree(tocTree) %>
  </nav>
<% } %>

Prev/Next navigation using siblings

<!-- site/partials/page-nav.eta -->
<%
  // Get siblings to create prev/next navigation
  const siblings = stati.nav.getSiblings(undefined, true);
  const currentIndex = siblings.findIndex(s => s.url === stati.page.url);
  const prevPage = currentIndex > 0 ? siblings[currentIndex - 1] : null;
  const nextPage = currentIndex < siblings.length - 1 ? siblings[currentIndex + 1] : null;
%>

<% if (prevPage || nextPage) { %>
  <nav class="page-navigation">
    <% if (prevPage) { %>
      <a href="<%= stati.url(prevPage.url) %>" class="nav-prev">
        <span class="label">← Previous</span>
        <span class="title"><%= prevPage.title %></span>
      </a>
    <% } %>

    <% if (nextPage) { %>
      <a href="<%= stati.url(nextPage.url) %>" class="nav-next">
        <span class="label">Next →</span>
        <span class="title"><%= nextPage.title %></span>
      </a>
    <% } %>
  </nav>
<% } %>

Collection index pages

Use collection data provided to index pages to list content and link to tag pages:

<!-- site/blog/index.eta: layout of site/blog/index.md -->
<% stati.layout('base') %>
<% // Collection data is automatically provided to index pages %>
<% const posts = stati.collection ? stati.collection.recentPages : [] %>
<% const tags = stati.collection ? Object.keys(stati.collection.pagesByTag) : [] %>

<div class="blog-index">
  <!-- Tag links -->
  <% if (tags.length > 0) { %>
    <div class="tag-filter">
      <% tags.forEach(tag => { %>
        <a
          href="<%= stati.url(`/blog/tags/${stati.slugify(tag)}`) %>"
          class="tag"
        >
          <%= tag %> (<%= stati.collection.pagesByTag[tag].length %>)
        </a>
      <% }) %>
    </div>
  <% } %>

  <!-- Posts -->
  <div class="posts">
    <% posts.forEach(post => { %>
      <article class="post-card">
        <h2>
          <a href="<%= stati.url(post.url) %>"><%= post.frontMatter.title %></a>
        </h2>
        <div class="post-meta">
          <% if (post.publishedAt) { %>
            <time datetime="<%= post.publishedAt.toISOString() %>">
              <%= post.publishedAt.toLocaleDateString('en-US') %>
            </time>
          <% } %>
          <% if (post.frontMatter.author) { %>
            <span class="author">by <%= post.frontMatter.author %></span>
          <% } %>
        </div>
        <% if (post.frontMatter.description) { %>
          <p><%= post.frontMatter.description %></p>
        <% } %>
        <% if (post.frontMatter.tags && post.frontMatter.tags.length > 0) { %>
          <div class="tags">
            <% post.frontMatter.tags.forEach(tag => { %>
              <span class="tag"><%= tag %></span>
            <% }) %>
          </div>
        <% } %>
      </article>
    <% }) %>
  </div>
</div>

The tag links lead to the pages from A page for every tag.

A post reads the collection of its folder, so the post layout can ask it for related posts with no hook. related() scores the other posts by shared tags, newest first on ties:

<!-- site/blog/layout.eta: layout of the posts -->
<% stati.layout('base') %>
<% const related = stati.collection?.related(stati.page.url, { limit: 3 }) ?? []; %>
<article class="post">
  <%~ stati.content %>
</article>

<% if (related.length > 0) { %>
  <aside class="related-posts">
    <h2>Related Posts</h2>
    <ul>
      <% related.forEach((post) => { %>
        <li>
          <a href="<%= stati.url(post.url) %>"><%= post.frontMatter.title %></a>
        </li>
      <% }) %>
    </ul>
  </aside>
<% } %>

Because the layout calls stati.collection.related(), ISG re-renders the posts when a post is added or removed or its title or tags change. See Related pages for the options.

Paginated listing

Split a long listing into pages with paginate, and link them with stati.pagination:

---
# site/blog/index.md
title: Blog
paginate: 10
---
<!-- site/blog/index.eta: layout of site/blog/index.md -->
<% stati.layout('base') %>
<% const { items, pageNumber, totalPages, prevUrl, nextUrl, urls } = stati.pagination; %>
<% items.forEach((post) => { %>
  <h2><a href="<%= stati.url(post.url) %>"><%= post.frontMatter.title %></a></h2>
<% }) %>

<% if (totalPages > 1) { %>
  <nav aria-label="Pages">
    <% if (prevUrl) { %><a href="<%= stati.url(prevUrl) %>" rel="prev">Newer</a><% } %>
    <% urls.forEach((url, i) => { %>
      <% if (i + 1 === pageNumber) { %>
        <span aria-current="page"><%= i + 1 %></span>
      <% } else { %>
        <a href="<%= stati.url(url) %>"><%= i + 1 %></a>
      <% } %>
    <% }) %>
    <% if (nextUrl) { %><a href="<%= stati.url(nextUrl) %>" rel="next">Older</a><% } %>
  </nav>
<% } %>

The first page stays at /blog; the next ones are /blog/page/2, /blog/page/3 and so on.

A page for every tag

A file named [tag].md becomes one page per tag of its collection:

---
# site/blog/tags/[tag].md
title: 'Posts tagged {tag}'
generate:
  from: tags
---
<!-- site/blog/tags/layout.eta: layout of the pages from site/blog/tags/[tag].md -->
<% stati.layout('base') %>
<h1><%= stati.page.title %></h1>
<ul>
  <% stati.generated.items.forEach((post) => { %>
    <li><a href="<%= stati.url(post.url) %>"><%= post.frontMatter.title %></a></li>
  <% }) %>
</ul>

Link to the pages with stati.slugify, which gives the same key Stati used for the URL:

<% (stati.page.tags ?? []).forEach((tag) => { %>
  <a href="<%= stati.url(`/blog/tags/${stati.slugify(tag)}`) %>"><%= tag %></a>
<% }) %>

See Collections and generated pages for keys from other fields, pagination of tag pages and pages from data.

Helpers in templates

Add reusable functions for templates under helpers in the config:

// stati.config.js
import { defineConfig } from '@stati/core';

export default defineConfig({
  helpers: {
    // Date formatting
    formatDate: (date) => {
      return new Date(date).toLocaleDateString('en-US', {
        year: 'numeric',
        month: 'long',
        day: 'numeric'
      });
    },

    // Time ago formatting
    timeAgo: (date) => {
      const now = new Date();
      const then = new Date(date);
      const diffMs = now - then;
      const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));

      if (diffDays === 0) return 'Today';
      if (diffDays === 1) return 'Yesterday';
      if (diffDays < 7) return `${diffDays} days ago`;
      if (diffDays < 30) return `${Math.floor(diffDays / 7)} weeks ago`;
      if (diffDays < 365) return `${Math.floor(diffDays / 30)} months ago`;
      return `${Math.floor(diffDays / 365)} years ago`;
    }
  }
});

Call them in templates as stati.helpers.name(). For slugs, Stati’s own stati.slugify() is built in, and so is the reading time: stati.page.readingTime holds whole minutes at readingTime.wordsPerMinute (default 230).

<!-- Post template -->
<article class="post">
  <header>
    <h1><%= stati.page.title %></h1>
    <% const published = stati.page.publishedAt ?? stati.page.date; %>
    <div class="meta">
      <% if (published) { %>
        <time><%= stati.helpers.formatDate(published) %></time>
        <span><%= stati.helpers.timeAgo(published) %></span>
      <% } %>
      <span><%= stati.page.readingTime %> min read</span>
    </div>
  </header>
  <%~ stati.content %>
</article>

stati.nav.getBreadcrumbs() returns the trail from the page’s top-level navigation node, which for nested pages is the nearest folder rather than the site root, so add the Home link yourself (see getBreadcrumbs):

<!-- site/partials/breadcrumbs.eta -->
<nav class="breadcrumbs" aria-label="Breadcrumb">
  <ol class="breadcrumb-list">
    <li class="breadcrumb-item">
      <a href="<%= stati.url('/') %>">Home</a>
    </li>
    <% stati.nav.getBreadcrumbs().forEach((crumb, index, array) => { %>
      <li class="breadcrumb-item">
        <% if (index === array.length - 1) { %>
          <span aria-current="page"><%= crumb.title %></span>
        <% } else { %>
          <a href="<%= stati.url(crumb.url) %>"><%= crumb.title %></a>
        <% } %>
      </li>
    <% }); %>
  </ol>
</nav>

SEO & metadata

Leveraging built-in SEO

Stati provides automatic SEO tag injection. Configure it in your config:

// stati.config.js
import { defineConfig } from '@stati/core';

export default defineConfig({
  site: {
    title: 'My Awesome Site',
    baseUrl: 'https://example.com',
    defaultLocale: 'en-US'
  },

  seo: {
    autoInject: true,
    defaultAuthor: {
      name: 'Your Name',
      url: 'https://yoursite.com',
      twitter: '@yourname'
    },
    twitter: { site: '@mysite' },
    articles: { include: ['/blog/*'] }
  },

  sitemap: {
    enabled: true,
    defaultPriority: 0.5,
    defaultChangeFreq: 'monthly',
    priorityRules: [
      { pattern: '/', priority: 1.0 },
      { pattern: '/blog/**', priority: 0.8 }
    ]
  },

  robots: {
    enabled: true,
    disallow: ['/admin/', '/draft/'],
    sitemap: true
  }
});

Every page now gets og:locale en_US, twitter:site @mysite and twitter:creator @yourname. Posts under /blog/ also get og:type article, article:published_time from their publish date, article:modified_time from updated, one article:tag per tag and article:author.

Add SEO metadata in front matter. Each value set here wins over the config:

---
title: My Blog Post
description: A comprehensive guide to...
publishedAt: 2024-01-15
tags: [tech, web-dev]
seo:
  title: Custom SEO Title | My Site
  description: Custom meta description
  keywords: [stati, ssg, jamstack]
  openGraph:
    image: /images/og-image.jpg
    article:
      section: Guides
  twitter:
    card: summary_large_image
---

Quote dates inside seo ('2024-01-15'): an unquoted YAML date becomes a Date object, which the tag prints as a local date string such as Mon Jan 15 2024 ... instead of ISO 8601. Dates Stati fills in itself, such as the publish date, are always printed in ISO 8601 form.

Custom structured data

Add structured data in a beforeAll hook. Changes made there are part of each page’s input, so cached pages stay cached until the data changes (changes made in beforeRender make the page re-render on every build):

// stati.config.js
import { defineConfig } from '@stati/core';

export default defineConfig({
  hooks: {
    beforeAll: async (ctx) => {
      // Add structured data for blog posts
      for (const page of ctx.pages) {
        if (page.frontMatter.type !== 'post') continue;
        page.frontMatter.seo = page.frontMatter.seo || {};
        page.frontMatter.seo.structuredData = {
          '@context': 'https://schema.org',
          '@type': 'BlogPosting',
          headline: page.frontMatter.title,
          description: page.frontMatter.description,
          datePublished: page.publishedAt?.toISOString(),
          dateModified: page.frontMatter.modifiedAt || page.publishedAt?.toISOString(),
          author: {
            '@type': 'Person',
            name: page.frontMatter.author || ctx.config.seo?.defaultAuthor?.name
          },
          publisher: {
            '@type': 'Organization',
            name: ctx.config.site.title,
            url: ctx.config.site.baseUrl
          }
        };
      }
    }
  }
});

page.publishedAt is a Date taken from the first valid front matter value among publishedAt, published, date and createdAt, quoted or unquoted. It is undefined for undated pages.

Advanced patterns

Build-time data fetching

Fetch external data during build using hooks:

// stati.config.js
import { defineConfig } from '@stati/core';

export default defineConfig({
  hooks: {
    beforeAll: async (ctx) => {
      // Fetch data from external API
      const response = await fetch('https://api.example.com/data');
      const data = await response.json();

      // Store in a way that templates can access
      ctx.pages.forEach(page => {
        if (page.frontMatter.useExternalData) {
          page.frontMatter.externalData = data;
        }
      });
    }
  }
});

Dynamic class names with propValue

Use stati.propValue() for clean dynamic class generation:

<!-- Component with dynamic classes -->
<button
  class="<%= stati.propValue(
    'btn',
    `btn-${stati.page.variant || 'primary'}`,
    stati.page.large && 'btn-lg',
    stati.page.disabled && 'btn-disabled'
  ) %>"
>
  <%= stati.page.buttonText %>
</button>

<!-- Dynamic attributes -->
<div
  class="<%= stati.propValue('card', stati.page.featured && 'featured') %>"
  data-category="<%= stati.page.category %>"
  data-analytics="<%= stati.propValue('card-click', `category-${stati.page.category}`) %>"
>
  <!-- Card content -->
</div>

ISG cache invalidation strategies

Configure intelligent caching with aging rules:

// stati.config.js
import { defineConfig } from '@stati/core';

export default defineConfig({
  isg: {
    enabled: true,
    ttlSeconds: 21600, // 6 hours default

    // Age-based TTL adjustment
    aging: [
      { untilDays: 7, ttlSeconds: 21600 },    // up to 7 days old: 6 hours
      { untilDays: 30, ttlSeconds: 86400 },   // up to 30 days old: 1 day
      { untilDays: 90, ttlSeconds: 259200 },  // up to 90 days old (and older): 3 days
    ]
  }
});

Cache Tags:

Stati automatically extracts tags from each page’s front matter tags array field. These tags can be used for targeted cache invalidation.

Example front matter:

---
title: My Blog Post
tags: [blog, tutorial, javascript]
---

This page will be tagged with blog, tutorial, and javascript in the cache, allowing you to invalidate all related content:

# Invalidate by tag from front matter
stati invalidate "tag:blog"
stati invalidate "tag:tutorial"

# Invalidate by path prefix
stati invalidate "path:/blog"

# Invalidate by glob pattern
stati invalidate "glob:/blog/**"

# Invalidate by age
stati invalidate "age:3months"

Performance monitoring

Each hook receives a new context object, so keep timing state in module-level variables:

// stati.config.js
import { defineConfig } from '@stati/core';

let buildStart = 0;
const renderStarts = new Map();

export default defineConfig({
  hooks: {
    beforeAll: async (ctx) => {
      buildStart = Date.now();
      console.log(`Building ${ctx.pages.length} pages...`);
    },

    beforeRender: async (ctx) => {
      renderStarts.set(ctx.page.url, Date.now());
    },

    afterRender: async (ctx) => {
      const renderTime = Date.now() - renderStarts.get(ctx.page.url);
      if (renderTime > 100) {
        console.warn(`Slow render: ${ctx.page.url} took ${renderTime}ms`);
      }
    },

    afterAll: async (ctx) => {
      const totalTime = Date.now() - buildStart;
      console.log(`Build completed in ${totalTime}ms`);
      console.log(`Average: ${Math.round(totalTime / ctx.pages.length)}ms per page`);
    }
  }
});

The render hooks only run for pages that are re-rendered, not for pages served from the ISG cache. For detailed timings, use build metrics instead.

Environment-specific configuration

Use environment variables for different build targets:

// stati.config.js
import { defineConfig, getEnv } from '@stati/core';

const isDev = getEnv() === 'development';
const isProd = getEnv() === 'production';

export default defineConfig({
  site: {
    title: 'My Site',
    baseUrl: isProd
      ? 'https://example.com'
      : 'http://localhost:3000'
  },

  isg: {
    enabled: isProd // Only enable ISG in production
  },

  seo: {
    autoInject: isProd, // Only inject SEO tags in production
    debug: isDev // Enable SEO debugging in development
  }
});

Markdown extensions

Add custom markdown processing with plugins and the configure function. Install the plugins first: npm install -D markdown-it-container.

// stati.config.js
import { defineConfig } from '@stati/core';

export default defineConfig({
  markdown: {
    // Load markdown-it plugins (Stati auto-prepends 'markdown-it-')
    // Note: TOC and heading anchors are built-in, no plugins needed
    plugins: [
      ['container', 'tip'],
      ['container', 'warning']
    ],

    // Custom markdown-it configuration
    configure: (md) => {
      // Customize renderer rules. Wrap the default image rule instead of
      // replacing it: Stati's rule adds width, height and lazy loading, and
      // with images.responsive it writes a <picture> with srcset.
      const defaultImageRender = md.renderer.rules.image;

      md.renderer.rules.image = (tokens, idx, options, env, self) => {
        // Attributes set on the token, such as a class, are kept on the <img>
        tokens[idx].attrJoin('class', 'responsive-image');
        return defaultImageRender(tokens, idx, options, env, self);
      };

      // Customize fence (code block) rendering
      md.renderer.rules.fence = (tokens, idx, options, env, self) => {
        const token = tokens[idx];
        const info = token.info ? md.utils.unescapeAll(token.info).trim() : '';
        const langName = info ? info.split(/\s+/g)[0] : '';
        const filename = info.split(/\s+/g).slice(1).join(' ');

        return `
          <div class="code-block">
            ${filename ? `<div class="code-filename">${md.utils.escapeHtml(filename)}</div>` : ''}
            <pre><code class="language-${langName}">${md.utils.escapeHtml(token.content)}</code></pre>
            <button class="copy-button" onclick="copyCode(this)">Copy</button>
          </div>
        `;
      };
    }
  }
});

An image rule that doesn’t call defaultImageRender replaces Stati’s own, and Markdown images lose their sizes, lazy loading and responsive versions.

Development workflow

Custom build scripts

{
  "scripts": {
    "dev": "stati dev",
    "build": "stati build",
    "build:clean": "stati build --clean",
    "preview": "stati preview --port 8080",
    "invalidate:recent": "stati invalidate tag:recent",
    "invalidate:all": "stati invalidate"
  }
}

Git hooks for cache management

#!/bin/sh
# .husky/post-merge
# Clear the whole cache after pulling changes
npx stati invalidate

stati invalidate without a query clears every cache entry. A path: query is a plain prefix (path:/blog), not a glob; use glob: for wildcards.

For complete starter sites, see Examples.