Astro routing pattern

From Intense-Visions/harness-engineering · View interactive version

Astro routing pattern is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > File-based routing maps your `src/pages/` directory structure to URLs — static, dynamic, and rest-parameter routes all follow the same filesystem convention.

How to use this skill

Skill file

Astro Routing Pattern

File-based routing maps your src/pages/ directory structure to URLs — static, dynamic, and rest-parameter routes all follow the same filesystem convention.

When to Use

Instructions

  1. Create files in src/pages/ to define routes. The file path maps directly to the URL:
  1. Use bracket syntax for dynamic route segments. The bracket name becomes the param key:
  1. Implement getStaticPaths() in every dynamic route page used in SSG mode. It must return an array of { params } objects:
---
// src/pages/blog/[slug].astro
import { getCollection } from 'astro:content';

export async function getStaticPaths() {
  const posts = await getCollection('blog');
  return posts.map((post) => ({
    params: { slug: post.slug },
    props: { post },          // pass data to avoid re-fetching in frontmatter
  }));
}

const { post } = Astro.props;
const { Content } = await post.render();
---
<article>
  <h1>{post.data.title}</h1>
  <Content />
</article>
  1. Use rest parameters ([...slug]) for nested dynamic routes and catch-all paths:
  1. Implement pagination with paginate() inside getStaticPaths():
export async function getStaticPaths({ paginate }) {
  const posts = await getCollection('blog');
  const sorted = posts.sort((a, b) => b.data.pubDate - a.data.pubDate);
  return paginate(sorted, { pageSize: 10 });
}

const { page } = Astro.props;
// page.data: current page items
// page.currentPage: page number (1-indexed)
// page.url.next / page.url.prev: adjacent page URLs
  1. Create src/pages/404.astro for a custom 404 page. In SSR mode this file is served for unmatched routes; in SSG it depends on the host.
  1. Use Astro.redirect() for programmatic redirects in SSR mode. In SSG, configure redirects in astro.config.mjs:
// astro.config.mjs
export default defineConfig({
  redirects: {
    '/old-path': '/new-path',
    '/blog/[slug]': '/posts/[slug]', // dynamic redirect
  },
});
  1. Understand route priority when multiple routes could match the same URL:

Details

Astro's file-based router has no runtime overhead in SSG mode — all route resolution happens at build time, producing a flat directory of .html files. In SSR mode, the router resolves requests at the edge/server using the same file structure.

getStaticPaths() contract:

This function is called once per dynamic route file during the build. It must return a serializable array — the return value is cached and cannot be async per-entry (do all async work inside the function before mapping). Every params combination returned becomes a generated page. If a param is missing from the return value, that URL will 404.

Passing props through getStaticPaths():

The props key in each getStaticPaths() return object is merged into Astro.props for that specific page. This is the correct way to pass fetched data (collection entries, API responses) into the page frontmatter without a second network/file-system round-trip.

params vs. Astro.params:

Endpoint routes:

Files in src/pages/ with .ts or .js extensions (not .astro) become API endpoints, not HTML pages. See astro-server-endpoints for that pattern.

Named groups and optional segments (Astro 4+):

Hybrid SSR and routing:

In output: 'hybrid' mode, every page is static by default. Add export const prerender = false to opt specific pages into SSR. Dynamic route pages in SSR do not need getStaticPaths() — Astro.params is populated from the live request.

Source

https://docs.astro.build/en/guides/routing

Process

  1. Read the instructions and examples in this document.
  2. Apply the patterns to your implementation, adapting to your specific context.
  3. Verify your implementation against the details and edge cases listed above.

Harness Integration

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.