# Custom Pages

Pages written as Vue components.

A custom page is a `.vue` file in `src/pages/`, for a page that isn't Markdown, like a landing page. The theme adds no layout around it, and its file path is its URL, as in [Routing](/routing#custom-pages). This page is served at `/about`, and uses `HomeLayout` for the site navbar:

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

definePage({ data: { title: "About", description: "Who writes these docs." } }); // [!code highlight]
</script>

<template>
  <HomeLayout>
    <main class="mx-auto w-full max-w-(--fd-layout-width) px-4 py-12">
      <h1 class="text-2xl font-semibold">About</h1>
      <p class="text-fd-muted-foreground">We write docs.</p>
    </main>
  </HomeLayout>
</template>
```

A page renders on the server before it runs in the browser, so use browser APIs like `window` only in `onMounted()` or an event handler. Style it with [Tailwind CSS](/styling#tailwind-css) classes.

The other examples on this page leave out `HomeLayout`.

## Page Options [#page-options]

`definePage()` from `vuesho/client` sets a page's data, props and paths. `data`, `props` and `contentOf` can be functions, which receive `{ params, lang, request }`:

### PageOptions

| Prop            | Type                                                                                                                                | Description                                                                                                                                                |
| --------------- | ----------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `lang?`         | `any`                                                                                                                               | The page's only language, or `false` for its path as written. Default: `every language`                                                                    |
| `props?`        | `Record<string, unknown> \| ((env: PageRouteEnv<any, string>) => Awaitable<object \| undefined>) \| undefined`                      | The props for the page's component, or a function of the page. `undefined` makes it a 404.                                                                 |
| `render?`       | `RenderMode \| undefined`                                                                                                           | Whether the build generates the route, or a server renders it on request. Default: ``"static" without params or with `staticParams`, "dynamic" otherwise`` |
| `staticParams?` | `StaticParams<string> \| undefined`                                                                                                 | The values of the params of the pages generated at build time, like `{ path: ["a", "b"] }` for `[...path]`.                                                |
| `data?`         | `Partial<PageData> \| ((env: PageRouteEnv<any, string>) => Awaitable<Partial<PageData> \| undefined>) \| undefined`                 | The page data for its pages, or a function of the page. `undefined` makes it a 404.                                                                        |
| `contentOf?`    | `((env: PageRouteEnv<any, string>) => Awaitable<Readonly<Record<string, Page<string \| undefined, SourcePageData>>>>) \| undefined` | The content pages whose content a page shows, under keys you choose, each a component in `page.contentOf`.                                                 |

`definePage()` runs on the server apart from the rest of the script. Call it once, at the top level of `<script setup>` or `<script>`, with one object literal that uses imports but no other variables of the script.

For a file with params, like `src/pages/blog/[slug].vue`, list the params to render at build time in [`staticParams`](/routing#static-params).

## Server Props [#server-props]

`props` returns the component's props, which `defineProps()` declares. It runs when the site builds, or on the server for a page rendered on request:

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

interface Release {
  name: string;
  html_url: string;
}

definePage({
  data: { title: "Releases" },
  // [!code highlight:5]
  props: async () => {
    const response = await fetch("https://api.github.com/repos/vuejs/core/releases");
    const releases = (await response.json()) as Release[];
    return { releases: releases.slice(0, 10) };
  },
});

defineProps<{ releases: Release[] }>();
</script>

<template>
  <ul>
    <li v-for="release in releases" :key="release.html_url">
      <a :href="release.html_url">{{ release.name }}</a>
    </li>
  </ul>
</template>
```

The props are sent to the browser as JSON, and `usePage()` returns them in `page.props`. When `props` returns `undefined`, a page rendered on request shows the [not found page](/routing#not-found-page), while a page rendered at build time fails the build.

### Dynamic Rendering [#dynamic-rendering]

`render: "dynamic"` renders the page on each request, with fresh props, so the site needs a server. `props` then receives the `request`:

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

definePage({
  render: "dynamic", // [!code highlight]
  data: { title: "Status" },
  props: async ({ request }) => {
    const response = await fetch("https://status.example.com/api/summary", {
      headers: { "Accept-Language": request?.headers.get("Accept-Language") ?? "en" },
    });
    return { status: (await response.json()) as { description: string } };
  },
});

defineProps<{ status: { description: string } }>();
</script>

<template>
  <h1>{{ status.description }}</h1>
</template>
```

### Reading Content [#reading-content]

In a server module, read the pages of your [content sources](/content-sources) with [`getSiteContext()`](/plugin-api#outside-hooks) from `vuesho/config`, for example to list blog posts on an index page:

```ts title="src/server/posts.ts"
import { getSiteContext, type SiteLang } from "vuesho/config";
import type app from "../../vuesho.config";

export interface Post {
  title: string;
  url: string;
}

export function listPosts(lang: SiteLang<typeof app>): Post[] {
  return getSiteContext<typeof app>()
    .getPages(lang)
    .flatMap((page) => (page.type === "blog" ? [{ title: page.data.title, url: page.url }] : []));
}
```

```vue title="src/pages/blog/index.vue"
<script setup lang="ts">
import { definePage, Link } from "vuesho/client";
import { listPosts, type Post } from "../../server/posts";
import type app from "../../../vuesho.config";

definePage<typeof app>({
  data: { title: "Blog" },
  props: ({ lang }) => ({ posts: listPosts(lang) }), // [!code highlight]
});

defineProps<{ posts: Post[] }>();
</script>

<template>
  <Link v-for="post in posts" :key="post.url" :href="post.url">{{ post.title }}</Link>
</template>
```

`definePage<typeof app>()` types `lang` with your site's languages.

<Callout title="Server Code">
An import that only `definePage()` uses, like `../../server/posts` above, stays out of the browser bundle. An import that the template or the rest of `<script setup>` also uses is bundled for the browser.
</Callout>

### Embedded Content [#embedded-content]

To render content pages inside a custom page, like the posts on a news page, return them from `contentOf` as an object with keys of your choice. `page.contentOf` then holds each page's body as a component, under the same key:

```vue title="src/pages/news.vue"
<script setup lang="ts">
import { definePage, usePage } from "vuesho/client";
import { getSiteContext } from "vuesho/config";

definePage({
  data: { title: "News" },
  // [!code highlight:6]
  contentOf: ({ lang }) =>
    Object.fromEntries(
      getSiteContext()
        .getPages(lang)
        .flatMap((page) => (page.type === "news" ? [[page.url, page]] : [])),
    ),
});

const page = usePage();
</script>

<template>
  <main v-if="page.kind === 'custom'" class="mx-auto w-full max-w-(--fd-layout-width) px-4 py-12">
    <article v-for="(Content, url) in page.contentOf" :key="url" class="prose mb-12">
      <component :is="Content" />
    </article>
  </main>
</template>
```

A page from a source with only data, without a body, renders nothing there.

## Shared Layouts [#shared-layouts]

When each page renders `HomeLayout`, the layout mounts again on each navigation. To keep one layout mounted across custom pages, render it in the theme's [`Root`](/theme#root-component), which wraps every page:

```vue title="src/Root.vue"
<script setup lang="ts">
import { usePage } from "vuesho/client";
import HomeLayout from "vuesho/layouts/home";
import { Root } from "vuesho/theme";

const page = usePage();
</script>

<template>
  <Root>
    <HomeLayout v-if="page.kind === 'custom'"><slot /></HomeLayout>
    <slot v-else />
  </Root>
</template>
```

```ts title="src/theme.ts"
import { defineTheme } from "vuesho/theme";
import defaultTheme from "vuesho/theme/default";
import Root from "./Root.vue"; // [!code ++]

export default defineTheme({
  extends: defaultTheme,
  Root, // [!code ++]
});
```

Then remove `HomeLayout` from the pages. To put the layout around some custom pages only, check `page.url`.

## Markdown Version [#markdown-version]

A content page has a [Markdown version](/ai) at its URL plus `.md`, which the Copy Markdown button and `llms-full.txt` use. A custom page has one when the component sets `markdown` in `defineOptions()`:

```vue title="src/pages/showcase/index.vue"
<script setup lang="ts">
import { definePage, md, type MarkdownContext } from "vuesho/client";

interface Site {
  name: string;
  url: string;
}

definePage({
  data: { title: "Showcase", description: "Sites built with Acme." },
  props: {
    sites: [
      { name: "Globex", url: "https://globex.dev" },
      { name: "Initech", url: "https://initech.dev" },
    ],
  },
});

defineProps<{ sites: Site[] }>();

// [!code highlight:4]
defineOptions({
  markdown: ({ props }: MarkdownContext<{ sites: Site[] }>) =>
    md`${props.sites.map((site) => `- [${md.escape(site.name)}](${site.url})\n`)}`,
});
</script>

<template>
  <ul>
    <li v-for="site in sites" :key="site.url">
      <a :href="site.url">{{ site.name }}</a>
    </li>
  </ul>
</template>
```

`/showcase.md` starts with the page's title and description:

```md
# Showcase

Sites built with Acme.

- [Globex](https://globex.dev)
- [Initech](https://initech.dev)
```

`markdown` runs only on the server, and stays out of the browser bundle. To include a child component's Markdown, call `component()` on the context that `markdown` receives. Custom pages aren't listed in `llms.txt`.

## Internationalization [#internationalization]

On a site with [languages](/i18n), a custom page exists in every language. Set `lang` to serve the page in one language only, or `lang: false` to serve it at one path without a language prefix:

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

definePage({ lang: "en", data: { title: "Acme Docs" } }); // [!code highlight]
</script>

<template>
  <h1>Acme Docs</h1>
</template>
```

With `languages: ["en", "cn"]` and `hideLocale: "never"`, `src/pages/about.vue` has these URLs:

| `definePage()` | URLs                     |
| -------------- | ------------------------ |
| No `lang`      | `/en/about`, `/cn/about` |
| `lang: "en"`   | `/en/about`              |
| `lang: false`  | `/about`                 |
