Vite 预渲染产物已生成,线上 /pricing 仍返回 SPA 空壳:三处断点排查
构建完成后 dist/ 里躺着 index-pricing.html 和 index-articles-my-article.html,打开能看到完整正文和 meta。部署后 curl https://yourdomain.com/pricing,返回的却还是 index.html 的空壳:div#app 加一段 JS。构建期预渲染没有失败,问题出在服务器到浏览器这一段。
按顺序查三处:
- 服务器有没有给每个预渲染路由做精确映射,还是被
try_files兜底送回了index.html - URL 末尾斜杠有没有命中 Caddy 的精确 matcher
renderAfterDocumentEvent是不是在异步内容进入 DOM 之前就触发了
Vue + Vite 站点默认是 SPA,服务器发出的 HTML 基本是空的,内容在页面加载后由客户端渲染。落地页、文章、定价页、文档这类需要 SEO 的路由,得让 Google 抓取时直接收到静态 HTML,不只是可见文字,还包括 title、description、Open Graph、Twitter Card、canonical 和路由正文。
@prerenderer/rollup-plugin 解决的是这件事:保留原有客户端架构,只为选定路由在构建期产出静态 HTML。不需要改应用架构,运行时零成本,生产服务器只发静态文件,没有 Node.js 运行时,也不做每请求 SSR。
安装
pnpm add -D @prerenderer/rollup-plugin @prerenderer/renderer-puppeteer
插件用 Puppeteer(无头 Chrome)渲染页面。另有 renderer-jsdom 选项,速度更快,但复杂页面可靠性差一些。如果搜到的是 vite-plugin-prerender,先看维护状态;@prerenderer/rollup-plugin 直接接 Vite 的 Rollup 构建管线。
配置 Vite 预渲染路由
import prerender from "@prerenderer/rollup-plugin"
export default defineConfig({
plugins: [
vue(),
prerender({
routes: ["/", "/pricing", "/docs", "/articles/my-article"],
renderer: "@prerenderer/renderer-puppeteer",
rendererOptions: {
renderAfterDocumentEvent: "custom-render-trigger",
maxConcurrentRoutes: 2,
skipThirdPartyRequests: true,
},
postProcess(renderedRoute) {
renderedRoute.html = renderedRoute.html
.replace(/http:/gi, "https:")
.replace(/(https:\/\/)?(localhost|127\.0\.0\.1):\d*/gi, "https://yourdomain.com")
let name: string
if (renderedRoute.originalRoute === "/") {
name = "root"
} else {
name = renderedRoute.originalRoute.slice(1).replace(/\//g, "-")
}
renderedRoute.outputPath = `index-${name}.html`
},
}),
],
})
postProcess 做两件事:把预渲染 HTML 里的 localhost URL 替换成生产域名;生成 index-pricing.html、index-articles-my-article.html 这种平铺文件名,而不是嵌套目录。
触发时机:renderAfterDocumentEvent
renderAfterDocumentEvent 指定预渲染器等哪个自定义 DOM 事件,事件触发后才抓取 HTML。每个需要预渲染的页面组件里:
import { onMounted } from "vue"
onMounted(() => {
document.dispatchEvent(new Event("custom-render-trigger"))
})
这里有个容易踩的点:如果页面异步取数据,要在数据加载完成后再 dispatch,不能在 onMounted 里立即触发。以 /articles/my-article 为例,文章正文是 fetch 回来的,onMounted 立刻 dispatch 时 DOM 里只有标题和骨架,快照里就没有正文。预渲染产物看起来“生成成功”,实际内容缺了一块。
按路由写 meta
预渲染抓的是触发时刻 DOM 里的内容,包括 meta。在 Vue Router 的 beforeEach 守卫里写;每个路由在 meta 里定义 title、description,可选 ogImage:
router.beforeEach((to) => {
if (to.meta?.title) {
document.title = to.meta.title as string
}
const description = (to.meta?.description as string) || defaultDescription
document.querySelector('meta[name="description"]')?.setAttribute("content", description)
document.querySelector('meta[property="og:description"]')?.setAttribute("content", description)
document.querySelector('meta[name="twitter:description"]')?.setAttribute("content", description)
})
输出长什么样
pnpm run build 之后,dist/ 里会有:
index.html—— 默认 SPA 入口,非预渲染路由的兜底index-root.htmlindex-pricing.htmlindex-articles-my-article.html
命名规则是 index-{路由中的斜杠替换成短横线}.html。打开任意一个文件,应该看到实际 HTML 内容,而不是空的 div#app。
服务器配置:精确映射不能漏
Caddy:
:80 {
root * /app/dist
@root path /
rewrite @root /index-root.html
@pricing path /pricing
rewrite @pricing /index-pricing.html
@myarticle path /articles/my-article
rewrite @myarticle /index-articles-my-article.html
try_files {path} /index.html
file_server
}
nginx:
location = / {
try_files /index-root.html =404;
}
location = /pricing {
try_files /index-pricing.html =404;
}
location / {
try_files $uri /index.html;
}
具体路由匹配发预渲染文件,SPA fallback 处理其余请求。这里最容易漏:路由在 Vite 预渲染列表里,但服务器没有对应 rewrite,try_files 会把 /pricing 送回 /index.html。Caddy 的 @pricing path /pricing 是精确匹配,/pricing/ 不带斜杠的写法命中不了,需要在服务器上做 redirect,或者同时匹配带斜杠和不带斜杠两个路径。nginx 的 location = /pricing 同理;/articles/my-article 也要照此补一条精确 location,否则会落到 location / 的 try_files $uri /index.html。
自检:curl + grep 看原始响应
预渲染只有在服务器真的返回生成文件时才对抓取有帮助。每次新增或改名 SEO 路由后,部署完都要检查原始响应。预渲染 HTML 往往是一整行,grep -o 只打印匹配到的标签:
curl -s https://yourdomain.com/articles/my-article \
| grep -oE '[^[^[^<]*|rel="canonical" href="[^"]*"'
构建期预渲染下,两次响应应该一致。不一致说明服务器和爬虫之间有东西在给 bot 单独路径——CDN 规则、bot-detection 代理,或者遗留的 pre-render 服务。
预渲染路由仍然过不了检查,常见原因:
- 路由在 Vite 预渲染列表里,但没有服务器 rewrite,
try_files把它送到了/index.html。 - URL 带末尾斜杠。Caddy 的
path /articles/my-articlematcher 是精确匹配,/articles/my-article/不命中,除非做 redirect 或同时匹配两者。 - HTML 里的 canonical URL 指向的路径和 rewrite 实际服务的路径不一致。
看 Google 自己的视角,用 Search Console 的 URL Inspection,Test live URL,View tested page,HTML tab。
常见坑
- 输出里有 localhost URL。 没有
postProcess的 URL 替换,预渲染 HTML 会带本地 dev server 地址。 - 内容缺失。 预渲染页面缺内容时,先查 trigger 事件是不是在异步操作完成后才 dispatch。对构建产物用 View Source 看实际 HTML。
- 第三方脚本。
skipThirdPartyRequests: true阻止预渲染期间加载外部脚本(如 analytics),加快构建,也避免脚本因期待真实浏览器环境而报错。 - 内存。 路由多时 Puppeteer 吃内存。
maxConcurrentRoutes设成 2;内存充足可调高,构建失败就调低。 - 预渲染在构建期生成静态 HTML,生产服务器只发文件。代价是内容到下次构建前固定。