Skip to Content
Vuesho

Sanity

Publish Sanity documents as docs.

@vuesho/sanity publishes the documents in a Sanity Studio. The server reads them while the site runs, and the Studio can preview its drafts on the site.

Setup

Install the Packages

npm i @vuesho/sanity @sanity/client

Add the Schema to the Studio

In your Studio's project, install @vuesho/sanity and @sanity/code-input:

npm i @vuesho/sanity @sanity/code-input

Add schemaTypes to the Studio, with the pages' document type and their body's blocks:

sanity.config.ts
import { codeInput } from "@sanity/code-input";
import { defineConfig } from "sanity";
import { structureTool } from "sanity/structure";
import { schemaTypes } from "@vuesho/sanity/schema";

export default defineConfig({
  projectId: "...",
  dataset: "production",
  plugins: [structureTool(), codeInput()], 
  schema: { types: schemaTypes }, 
});

Store the Credentials

Put the project ID, from Sanity Manage, and the dataset name in .env:

.env
SANITY_PROJECT_ID=...
SANITY_DATASET=production

Connect the Source

Connect the documents in the config:

vuesho.config.ts
import { createClient } from "@sanity/client";
import { createSanity, sanityPlugin } from "@vuesho/sanity";
import { createSite, recommendedPreset } from "vuesho/config";

const sanity = createSanity({
  client: createClient({
    projectId: process.env.SANITY_PROJECT_ID,
    dataset: process.env.SANITY_DATASET,
    apiVersion: "2025-02-19",
    useCdn: true,
  }),
  docType: "docs",
});

export default createSite(sanity.source({ staleTime: 60_000 })).plugins(
  recommendedPreset(),
  sanityPlugin(), 
);

To use the Sanity pages next to the Markdown in content/, give their source a baseDir, as in Multiple Collections:

vuesho.config.ts
createSite({
  docs: docs.toSource(),
  sanity: sanity.source({ baseDir: "sanity", staleTime: 60_000 }), 
}).plugins(recommendedPreset(), sanityPlugin());

Pages

Each document of the docType type is a page of the site, with its last update date, in search and llms.txt like your other pages. Its slug sets its URL, where a / makes a folder.

@vuesho/sanity/schema has the docs document type, with a title, a slug, a description and a body, and blocks that render with Vuesho's components: callouts, cards, tabs, accordions, steps, file trees, code blocks highlighted with your code themes, and images from Sanity's CDN.

Page Data and URLs

page.data has a document's title and description, and data adds other fields, which the browser receives as JSON. generatePath replaces the default path, the slug with .md:

vuesho.config.ts
sanity.source({
  data: (document) => ({
    tags: Array.isArray(document.tags) ? document.tags : [],
  }),
  generatePath: (document) => `${document.slug?.current ?? document._id}.md`,
});

Custom Blocks

To add or replace block types, set the theme's sanity body to a component that passes Portable Text components to SanityBody:

src/theme.ts
import { defineTheme } from "vuesho/theme";
import defaultTheme from "vuesho/theme/default";
import type app from "../vuesho.config";

export default defineTheme<typeof app>({
  extends: defaultTheme,
  bodies: { sanity: () => import("./SanityBody.vue") }, 
});
src/SanityBody.vue
<script setup lang="ts">
import type { SanityBodyProps } from "@vuesho/sanity";
import { SanityBody } from "@vuesho/sanity/theme";
import Video from "./Video.vue";

const props = defineProps<SanityBodyProps>();
</script>

<template>
  <SanityBody v-bind="props" :components="{ types: { video: Video } }" />
</template>

To type a block's props, install @portabletext/vue and use its PortableTextComponentProps:

npm i @portabletext/vue
src/Video.vue
<script setup lang="ts">
import type { PortableTextComponentProps } from "@portabletext/vue";

defineProps<PortableTextComponentProps<{ url: string }>>();
</script>

<template>
  <video :src="value.url" controls />
</template>

Previews

The Studio's Presentation tool shows your drafts on the deployed site.

Create a Read Token

Create a token with the Viewer role in the project's API settings, and put it in .env. The build reads it too:

.env
SANITY_API_READ_TOKEN=sk...

Turn On Previews

Pass the token to createSanity()'s preview:

vuesho.config.ts
const token = process.env.SANITY_API_READ_TOKEN; 

const sanity = createSanity({
  client: createClient({/* ... */}),
  docType: "docs",
  preview: token ? { token } : undefined, 
});

Add the Presentation Tool

In the Studio, set presentationTool to open /api/sanity/preview, after the site's base path if it has one:

sanity.config.ts
import { codeInput } from "@sanity/code-input";
import { defineConfig } from "sanity";
import { presentationTool } from "sanity/presentation";
import { structureTool } from "sanity/structure";
import { schemaTypes } from "@vuesho/sanity/schema";

export default defineConfig({
  projectId: "...",
  dataset: "production",
  plugins: [
    structureTool(),
    codeInput(),
    presentationTool({
      previewUrl: {
        initial: "https://docs.example.com",
        previewMode: { enable: "/api/sanity/preview" },
      },
    }),
  ],
  schema: { types: schemaTypes },
});

/api/sanity/preview sets a cookie that shows the drafts to that browser. The drafts update as the Studio edits them. The preview ends when the browser closes, when the token changes, or at /api/sanity/preview/disable. Readers without the cookie see the published documents.

With previews, every page renders on request, for every reader, since a preview is an audience. With a Nitro cache route rule, list vuesho-sanity-preview in its allowCookies, as CDNs shows.

Refreshing Content

The server reads the documents again once staleTime passes, or when a webhook calls revalidateLoader(), as in Dynamic Rendering.

live: true refreshes the open pages when a document is published, through the Sanity Live Content API:

vuesho.config.ts
const sanity = createSanity({
  client: createClient({/* ... */}),
  docType: "docs",
  live: true, 
});
  • Add your site's origin to the project's CORS origins, without credentials.
  • On a serverless host with multiple instances, keep a short staleTime, since a live update refreshes only the instance that receives it, not the CDN.
  • A preview updates live with or without live.

A static build queries Sanity once and prerenders every page. It fails with preview or live, which need a server.

Options

createSanity()

Prop

Type

sanity.source()

Prop

Type