Skip to Content
Vuesho

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. This page is served at /about, and uses HomeLayout for the site navbar:

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." } }); 
</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 classes.

The other examples on this page leave out HomeLayout.

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 }:

Prop

Type

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.

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:

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

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

definePage({
  data: { title: "Releases" },
  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, while a page rendered at build time fails the build.

Dynamic Rendering

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

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

definePage({
  render: "dynamic", 
  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

In a server module, read the pages of your content sources with getSiteContext() from vuesho/config, for example to list blog posts on an index page:

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 }] : []));
}
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) }), 
});

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.

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.

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:

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

definePage({
  data: { title: "News" },
  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

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, which wraps every page:

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>
src/theme.ts
import { defineTheme } from "vuesho/theme";
import defaultTheme from "vuesho/theme/default";
import Root from "./Root.vue"; 

export default defineTheme({
  extends: defaultTheme,
  Root, 
});

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

Markdown Version

A content page has a Markdown version 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():

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[] }>();

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:

# 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

On a site with languages, 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:

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

definePage({ lang: "en", data: { title: "Acme Docs" } }); 
</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