Skip to Content
Vuesho

Context

Reference of `this` in the hooks of a plugin.

this is the context of the site in a hook, in the functions of a view, and in the head and ui.layoutProps options of your config. Its type is Context of vuesho/config:

createSite(docs.toFumadocsSource()).plugins({
  name: "site:example",
  configure() {
    console.log(`${this.getPages().length} pages in ${this.languages.join(", ")}`);
  },
});

Use a regular function or a method for a hook, not an arrow function, so that this is the context.

The context has the types of the site: getPages() returns its pages, typed by their source, and localizePath() takes its languages. In a function of another file, type this with SiteContext<typeof app>, from a type-only import of vuesho.config.ts:

src/server/links.ts
import type { SiteContext, SiteLang } from "vuesho/config";
import type app from "../../vuesho.config";

export function blogLink(this: SiteContext<typeof app>, lang: SiteLang<typeof app>) {
  return { text: "Blog", url: this.localizePath(lang, "/blog") };
}

Site

  • site: the site option of your config.
  • base: the base path of the site, with a trailing slash, like /docs/.
  • plugins: every plugin, in the order they run: yours, the preset, then the built-in ones.
  • signal: an AbortSignal, aborted when another site replaces this one, like after an edit of vuesho.config.ts, of a custom page or of the theme in development. Pass it to what a plugin starts, like fs.watch(dir, { signal: this.signal }), so the replaced site stops it.

Content

  • getPages(lang?): the content pages, of every language or of one.
  • getPage(path): a content page by its URL, without the base path.
  • getLoader(): the content loader of Fumadocs, from the configure hook on. Its URLs are encoded, so read pages with getPages() and getPage(), and use the loader for its other lookups, like getPageByHref().
  • getPageTree(lang?): the page tree of a language as the browser receives it, the default language when you omit it.
  • getPageBody(page), getPageToc(page), getPageText(page), getPageStructuredData(page), getPageLastModified(page), getPageCreatedAt(page) and getPageFile(page): the first result of that content hook among plugins.
  • getPageData(page): the data of a page that the browser receives, after the transformPage hook of plugins. getPageData(route, path) gives the data of the page at path of a page route.
  • getContent(path, request?): what the browser receives for a URL, with its kind: a content page, a view, a custom page, or the not found page at /404. It's undefined for a URL without a page. request reaches the data, view and props functions of a route that renders on demand.
  • getPageRender(path): "static" when the build generates the page at path, "dynamic" when the server renders it on demand, and undefined for a path without a page. It reads the source or the route of the page, then the route rules, see What Renders on Demand. A route made from each page, like a Markdown copy at <page>.md, lists a page in staticPaths only when it's "static", and a route with render: "dynamic" at the same path answers the others.
  • getContentRender(source?): "dynamic" when a source is dynamic or one of its pages renders on demand, "static" otherwise. Pass the key of a source, or a list of keys, to check those sources only.
  • getCacheControl(page): the Cache-Control header of a page of a dynamic source with a staleTime, like public, s-maxage=60, for a route of your own that renders the page. It's undefined for other pages.
  • revalidateLoader(source?): reads the dynamic sources again now, or the source of one key. The requests that start afterwards have the new pages.
  • invalidateLoader(source?): marks the dynamic sources stale, or the source of one key, so the next request reads them again.

A request reads the content as it was when it started: getPages(), getPage(), getPageTree() and getLoader() agree with each other for the whole request, while a dynamic source changes. Code outside a request, like the configure hook, reads the content as the loader last read it.

Languages

  • i18n: the i18n option of the content, undefined on a site without languages.
  • languages: the languages of the site. Without i18n, it's site.lang alone, or "en".
  • defaultLanguage: the default language.
  • translations: the ui.translations of your config, with the keys that plugins add. It is undefined without ui.translations.
  • getSite(lang): the site in a language: its name and description, its language tag lang, its label in the language switch, its link and the direction of its text dir, from site and site.languages.
  • getLanguage(path): the language of a URL.
  • localizePath(lang, path): a path in a language, with the language prefix unless hideLocale hides it.
  • getPageAlternates(page): the page in every language it exists in, itself included, as a list of { lang, url }. It's empty when the page has no translation.
  • getFallbackOrigin(page): for a page that shows the fallback language because its own language has no file for it, the page of the fallback language it shows, which is its canonical URL. It's undefined for a page with its own file.

Routes and Pages

  • getPageRoutes(): the page routes, the most specific first: the views of plugins, the custom pages and the not found page of each language, one route per language. Each has its path with the language prefix, lang, render, the pages it lists with their params and translations, and data. It throws in the routes hook, see Derived Routes.
  • getLayoutProps({ lang }): the props every layout shares in a language, from ui.layoutProps of your config and the layoutProps hook of plugins.
  • getHead(env): the tags of the <head> of a page, from your config, the head hook of plugins and head of the theme.

URLs

  • withBase(path): adds the base path to a path of the site and encodes it, like withBase() of vuesho/client. A full URL, a hash or a relative path is returned as it is.
  • absoluteUrl(path): the full URL of a path, with the base path and site.baseUrl. Without site.baseUrl, it's the path with the base path.
  • getFileUrl(file): the URL of a file on the host of site.git, with file from the project root, like content/guide.md. It's undefined without site.git, and for a file outside the repository.

Outside of Hooks

getSiteContext() returns the context of the running site, for server code that has no this, like a module that your API handlers call:

src/server/pages.ts
import { getSiteContext } from "vuesho/config";
import type app from "../../vuesho.config";

export function pageUrls(): string[] {
  return getSiteContext<typeof app>()
    .getPages()
    .map((page) => page.url);
}

During a request, it returns the site that serves the request, so two sites in one process each read their own. Outside of a request, it returns the site that started serving last. It throws before the site has started, so call it in a handler, not when the module loads. It's for the server only: your Vue components run in the browser too.