Learn how to use Schema.org in VitePress.
yarn add -D @vueuse/schema-org-vite @vueuse/head @vueuse/schema-org
Note: This package depends on @vueuse/head. The plugin will be automatically setup for you if you haven't already done so.
Note 2: dependency edge case with resolving the main package so including both are required.
Modify your .vitepress/theme/index.ts
file to add the plugin.
import DefaultTheme from 'vitepress/theme'import { installSchemaOrg } from '@vueuse/schema-org-vite/vitepress'import type { Theme } from 'vitepress/dist/client'const theme: Theme = { ...DefaultTheme, enhanceApp(ctx) { installSchemaOrg(ctx, { /* config */ }) },}export default theme
Next we need to load the plugin.
import DefaultTheme from 'vitepress/theme'import { installSchemaOrg } from '@vueuse/schema-org-vite/vitepress'import type { Theme } from 'vitepress/dist/client'const theme: Theme = { ...DefaultTheme, enhanceApp(ctx) { installSchemaOrg(ctx, { canonicalHost: 'https://example.com', }) },}export default theme
You should set the canonical host of your site.
See the User Config page for all options you can pass.
If you're using unplugin-vue-components or unplugin-auto-import, you can provide extra configuration for automatic imports.
Modify your vite.config.ts
to get the auto-imports.
import { SchemaOrgResolver, schemaOrgAutoImports } from '@vueuse/schema-org-vite'export default defineConfig({ plugins: [ // ... Components({ // ... resolvers: [ // auto-import schema-org components SchemaOrgResolver(), ], }), AutoImport({ // ... imports: [ // auto-import schema-org composables schemaOrgAutoImports, ], }), ]})
If you don't intend to use auto-imports you can import the components and define functions using the aliases.
<script lang="ts" setup>import { useSchemaOrg } from '#vueuse/schema-org/runtime'import { defineWebPage } from '#vueuse/schema-org/provider'useSchemaOrg([ defineWebPage({ name: 'Test', }),])</script>
To get all your pages up and running with Schema, you can make use schema inheritance and define Schema in your Layout file.
This allows all pages to inherit these Schemas, without them having to explicitly define them.
To add global Schema in VitePress, you need to override the default layout.
<script lang="ts" setup>useSchemaOrg([ // @todo Select Identity: https://vue-schema-org.netlify.app/guide/guides/identity defineWebSite({ name: 'My Awesome Website', }), defineWebPage(),])</script>
Your VitePress app is now serving basic Schema.org, congrats! 🎉
The next steps are: