# Routing

How file paths become URLs.

Markdown files in `content/` and `.vue` files in `src/pages/` are served at their file paths. A folder or file name in brackets, like `[framework]`, is a param, and the site has a page for each of its values.

## Content Pages [#content-pages]

A Markdown file in `content/` is served at its path without `.md`, and `index.md` at its folder's path:

```files
content
├── index.md
├── getting-started.md
├── _snippet.md
└── guide
    ├── index.md
    └── install.md
```

| File                 | URL                |
| -------------------- | ------------------ |
| `index.md`           | `/`                |
| `getting-started.md` | `/getting-started` |
| `guide/index.md`     | `/guide`           |
| `guide/install.md`   | `/guide/install`   |

A file or folder whose name starts with `_`, like an [included](/markdown#includes) file, isn't a page. To serve your pages under a prefix, like `/docs`, give the source a `baseDir`:

```ts title="vuesho.config.ts"
// `content/guide/install.md` is served at `/docs/guide/install`
createSite(docs.toSource({ baseDir: "docs" }));
```

## Custom Pages [#custom-pages]

A `.vue` file in `src/pages/` is a [custom page](/custom-pages), served at its path. A segment in brackets is a param:

```files
src/pages
├── index.vue
├── about.vue
├── blog
│   ├── index.vue
│   └── [slug].vue
├── releases
│   └── v[version].vue
├── api
│   └── [...path].vue
└── _components
    └── Hero.vue
```

| File                      | URLs                                 |
| ------------------------- | ------------------------------------ |
| `index.vue`               | `/`                                  |
| `about.vue`               | `/about`                             |
| `blog/[slug].vue`         | `/blog/hello`, `/blog/release-notes` |
| `releases/v[version].vue` | `/releases/v1`, `/releases/v2`       |
| `api/[...path].vue`       | `/api/users`, `/api/users/create`    |

A segment can hold text and several params, like `[pkg]-v[version].vue`, and a file or folder whose name starts with `_` isn't a page.

A custom page can't have the URL of a content page. For a landing page at `/`, serve the content pages under a prefix with `baseDir`, as above.

### Static Params [#static-params]

A page with params renders on request, on a server. To render some URLs when the site builds instead, list their params in `staticParams`, and read the params with `usePage()`:

```vue title="src/pages/showcase/[slug].vue"
<script setup lang="ts">
import { definePage, usePage } from "vuesho/client";

definePage({
  staticParams: [{ slug: "acme" }, { slug: "globex" }], // [!code highlight]
  data: ({ params }) => ({ title: `Showcase: ${params.slug}` }),
});

const page = usePage();
</script>

<template>
  <h1 v-if="page.kind === 'custom'">{{ page.params.slug }}</h1>
</template>
```

A URL whose params aren't in `staticParams` shows the not found page.

The value of `[...path]` is a list of segments:

```vue title="src/pages/api/[...path].vue"
<script setup lang="ts">
import { definePage } from "vuesho/client";

definePage({
  // `/api/users/create` and `/api`
  staticParams: [{ path: ["users", "create"] }, { path: [] }],
});
</script>
```

With [type checking](/markdown/type-checking), `definePage()` and `usePage()` type the params from the file's path, so an unknown param is a type error.

When `data` returns `undefined` for a URL, a page rendered on request shows the not found page, while the build fails on an entry of `staticParams`.

`render` sets when a page renders:

- `"dynamic"` renders it on each request, even for a page without params or with `staticParams`.
- `"static"` renders it when the site builds. A page with params then needs `staticParams`, or the site fails to start.

## Path Params [#path-params]

A folder in brackets in `content/` copies its pages for each param value. Each copy has its URLs and its folder in the sidebar, and its content can differ per value. Use it for docs per framework, per package or per version.

### Defining Params [#defining-params]

A folder or file in brackets, like `[framework]/` or `[pkg].md`, takes its values from `staticParams` in the folder's `meta.json`:

```json title="content/[framework]/meta.json"
{
  "title": "Framework",
  "staticParams": [{ "framework": "waku" }, { "framework": "astro" }]
}
```

```files
content/[framework]
├── meta.json
├── index.md
└── deploy.md
```

| File        | URLs                            |
| ----------- | ------------------------------- |
| `index.md`  | `/waku`, `/astro`               |
| `deploy.md` | `/waku/deploy`, `/astro/deploy` |

Each value is a separate folder in the sidebar. To give each one a title and an icon, see [Data per Value](#data-per-value).

Without `staticParams`, a folder in brackets is a plain folder with the brackets in its name. A path with several params, like `[framework]/[pkg].md`, needs a value for each.

### Rendering per Value [#rendering-per-value]

A page receives each param as a prop, in camel case, so `[my-param]` is `myParam`. Use `v-if` to show content for some values only:

```md title="content/[framework]/deploy.md"
## Deploy on {{ framework }}

<template v-if="framework === 'astro'">

Add an adapter to `astro.config.mjs`.

</template>
```

The table of contents, search and the page's [Markdown version](/ai) match what each value renders. A heading inside a false `v-if` is left out, and a heading's ID comes from its rendered text, like `deploy-on-astro`.

### Typing Params [#typing-params]

With [type checking](/markdown/type-checking), each param is a `string`. Narrow its type with `defineProps()`:

```md title="content/[framework]/deploy.md"
<script setup lang="ts">
const { framework } = defineProps<{ framework: "waku" | "astro" }>();
</script>
```

### Limiting a Page to Some Values [#limiting-a-page-to-some-values]

To create a page for some param values only, set `staticParams` in its frontmatter:

```md title="content/[framework]/adapters.md"
---
title: Adapters
staticParams:
  - framework: astro
---
```

### Overriding a Value's Page [#overriding-a-values-page]

A file in a folder named after a value, like `astro/deploy.md`, replaces the page for that value. The other values keep the page from the folder in brackets:

```files
content
├── [framework]
│   └── deploy.md
└── astro
    └── deploy.md
```

Here `/astro/deploy` is `astro/deploy.md`, and `/waku/deploy` is `[framework]/deploy.md`. With [languages](/i18n), a translation comes before a value's file, so `/cn/astro/deploy` renders the first of these that exists:

1. `astro/deploy.cn.md`
2. `[framework]/deploy.cn.md`
3. `astro/deploy.md`
4. `[framework]/deploy.md`

### Data per Value [#data-per-value]

To give each value its own title, icon and folder options, write a `meta.ts` or `meta.js` file instead of `meta.json`. It exports `staticParams`, and a default function that takes the params and returns the folder's data:

```ts title="content/[framework]/meta.ts"
const names = { waku: "Waku", astro: "Astro" };

export const staticParams = [{ framework: "waku" }, { framework: "astro" }];

export default ({ framework }: { framework: keyof typeof names }) => ({
  title: names[framework],
  icon: framework === "astro" ? "Rocket" : "Zap",
});
```

The function runs when the site builds, and its result must match the collection's [meta schema](/markdown/collections#frontmatter-schema). `icon` takes an icon name or a component:

```ts title="content/[framework]/meta.ts"
import { Rocket, Zap } from "@lucide/vue"; // [!code highlight]

export const staticParams = [{ framework: "waku" }, { framework: "astro" }];

export default ({ framework }: { framework: "waku" | "astro" }) => ({
  icon: framework === "astro" ? Rocket : Zap, // [!code highlight]
});
```

### Frontmatter per Value [#frontmatter-per-value]

A plain `<script>` in a page can export `frontmatter` as a function of the params. The fields it returns override the YAML, as with [computed frontmatter](/markdown#computed-frontmatter):

```md title="content/[framework]/index.md"
---
title: Overview
---

<script lang="ts">
const names = { waku: "Waku", astro: "Astro" };

export const frontmatter = ({ framework }: { framework: keyof typeof names }) => ({
  description: `Create a docs site on ${names[framework]}.`,
});
</script>
```

## Route Priority [#route-priority]

When several pages match a URL, the most specific one renders it:

1. A page at that exact path: a custom page without params, a page that `staticParams` lists, or a content page.
2. Then the pages with params, compared segment by segment. Text beats a param, and a param beats `[...path]`. A param with text, like `v[version]`, beats a bare one, like `[version]`.

A custom page at `src/pages/[...path].vue` renders every URL that no other page matches, instead of the not found page.

Two pages at the same path make the site fail to start, and the error names both files. A custom page at an exact path takes priority over pages that a [plugin](/plugin-api) adds, and over a [dynamic source](/dynamic-rendering)'s pages, which can appear at any path while the site runs.

## Not Found Page [#not-found-page]

`src/pages/404.vue` replaces the [theme's](/theme) not found page:

```vue title="src/pages/404.vue"
<script setup lang="ts">
import { definePage } from "vuesho/client";

definePage({ data: { title: "Not Found" } });
</script>

<template>
  <main>This page doesn't exist.</main>
</template>
```

Search engines don't index the page, and it isn't listed in the sitemap. The build writes it to `404.html`, and to `<lang>/404.html` for each language.
