Load a script only on some pages in Stati

Stati's TypeScript bundles with include and exclude, and how their patterns match.

SNP 098 #42A Filed stati typescript

typescript
typescript: {
  enabled: true,
  bundles: [
    // No include: on every page
    { entryPoint: 'main.ts', bundleName: 'main' },
    // The docs index and every page under it
    { entryPoint: 'search.ts', bundleName: 'search', include: ['/docs', '/docs/**'] },
    // Posts, but not drafts
    {
      entryPoint: 'charts.ts',
      bundleName: 'charts',
      include: ['/blog/**'],
      exclude: ['/blog/drafts/**'],
    },
  ],
},

Each bundle is compiled from src/ and injected before </body> on the pages it matches, in this order.

  • Patterns match the page's URL, /docs/intro, not its file (site/docs/intro.md or dist/docs/intro.html).
  • A pattern has to match the whole URL, and /docs/** doesn't match /docs itself, so list the index page on its own.
  • * stays inside one folder and ** crosses them. There are no braces: write /{docs,api}/** as two patterns.
  • exclude wins over include, and every bundleName must be unique.

The paths a page gets are in stati.assets.bundlePaths, handy for preloading:

eta
<% for (const path of stati.assets?.bundlePaths ?? []) { %>
  <link rel="modulepreload" href="<%= path %>">
<% } %>

In the Stati docs: Multiple bundles and Pattern matching.