SSR 兼容性
VitePress 在生产构建期间使用 Vue 的服务器端渲染 (SSR) 功能在 Node.js 中预渲染应用程序。这意味着主题组件中的所有自定义代码均须遵守 SSR 兼容性。
Vue 官方文档中的 SSR 部分 提供了有关什么是 SSR、SSR / SSG 之间的关系以及编写 SSR 的常见注意事项的更多上下文 -友好的代码。经验法则是仅在 Vue 组件的beforeMount或mounted挂钩中访问浏览器/DOM API。
<ClientOnly>
如果您正在使用或演示不适合 SSR 的组件(例如,包含自定义指令),您可以将它们包装在内置的 <ClientOnly> 组件中:
md
<ClientOnly>
<NonSSRFriendlyComponent />
</ClientOnly>导入时访问浏览器 API 的库
某些组件或库在导入时访问浏览器 API。要使用在导入时假定浏览器环境的代码,您需要动态导入它们。
导入已安装的钩子
vue
<script setup>
import { onMounted } from 'vue'
onMounted(() => {
import('./lib-that-access-window-on-import').then((module) => {
// use code
})
})
</script>条件导入
您还可以使用i标志有条件地导入依赖项(Vite env 变量 的一部分) ):
js
if (!import.meta.env.SSR) {
import('./lib-that-access-window-on-import').then((module) => {
// use code
})
}由于 Theme.enhanceApp 可以是异步的,因此您可以有条件地导入和注册在导入时访问浏览器 API 的 Vue 插件:
js
// .vitepress/theme/index.js
export default {
// ...
async enhanceApp({ app }) {
if (!import.meta.env.SSR) {
const plugin = await import('plugin-that-access-window-on-import')
app.use(plugin)
}
}
}defineClientComponent
VitePress 提供了一个方便的帮助器,用于导入 Vue 组件,在导入时访问浏览器 API。
vue
<script setup>
import { defineClientComponent } from 'vitepress'
const ClientComp = defineClientComponent(() => {
return import('component-that-access-window-on-import')
})
</script>
<template>
<ClientComp />
</template>您还可以将 props/children/slots 传递给目标组件:
vue
<script setup>
import { ref } from 'vue'
import { defineClientComponent } from 'vitepress'
const clientCompRef = ref(null)
const ClientComp = defineClientComponent(
() => import('component-that-access-window-on-import'),
// args are passed to h() - https://vuejs.org/api/render-function.html#h
[
{
ref: clientCompRef
},
{
default: () => 'default slot',
foo: () => h('div', 'foo'),
bar: () => [h('span', 'one'), h('span', 'two')]
}
],
// callback after the component is loaded, can be async
() => {
console.log(clientCompRef.value)
}
)
</script>
<template>
<ClientComp />
</template>目标组件只会在包装器组件的已安装钩子中导入。