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
A Markdown file in content/ is served at its path without .md, and index.md at its folder's path:
| 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 file, isn't a page. To serve your pages under a prefix, like /docs, give the source a baseDir:
// `content/guide/install.md` is served at `/docs/guide/install`
createSite(docs.toSource({ baseDir: "docs" }));Custom Pages
A .vue file in src/pages/ is a custom page, served at its path. A segment in brackets is a param:
| 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
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():
<script setup lang="ts">
import { definePage, usePage } from "vuesho/client";
definePage({
staticParams: [{ slug: "acme" }, { slug: "globex" }],
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:
<script setup lang="ts">
import { definePage } from "vuesho/client";
definePage({
// `/api/users/create` and `/api`
staticParams: [{ path: ["users", "create"] }, { path: [] }],
});
</script>With 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 withstaticParams."static"renders it when the site builds. A page with params then needsstaticParams, or the site fails to start.
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
A folder or file in brackets, like [framework]/ or [pkg].md, takes its values from staticParams in the folder's meta.json:
{
"title": "Framework",
"staticParams": [{ "framework": "waku" }, { "framework": "astro" }]
}| 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.
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
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:
## 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 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
With type checking, each param is a string. Narrow its type with defineProps():
<script setup lang="ts">
const { framework } = defineProps<{ framework: "waku" | "astro" }>();
</script>Limiting a Page to Some Values
To create a page for some param values only, set staticParams in its frontmatter:
---
title: Adapters
staticParams:
- framework: astro
---Overriding a Value's 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:
Here /astro/deploy is astro/deploy.md, and /waku/deploy is [framework]/deploy.md. With languages, a translation comes before a value's file, so /cn/astro/deploy renders the first of these that exists:
astro/deploy.cn.md[framework]/deploy.cn.mdastro/deploy.md[framework]/deploy.md
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:
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. icon takes an icon name or a component:
import { Rocket, Zap } from "@lucide/vue";
export const staticParams = [{ framework: "waku" }, { framework: "astro" }];
export default ({ framework }: { framework: "waku" | "astro" }) => ({
icon: framework === "astro" ? Rocket : Zap,
});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:
---
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
When several pages match a URL, the most specific one renders it:
- A page at that exact path: a custom page without params, a page that
staticParamslists, or a content page. - Then the pages with params, compared segment by segment. Text beats a param, and a param beats
[...path]. A param with text, likev[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 adds, and over a dynamic source's pages, which can appear at any path while the site runs.
Not Found Page
src/pages/404.vue replaces the theme's not found page:
<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.