跪拜 Guibai
← All articles
JavaScript · Vite

Build-Time Pre-Rendering Turns a Vue3 SPA into 21 Crawler-Friendly Pages

By hackhase ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

SPAs that return the same index.html for every route leave crawlers with a single homepage and a sea of duplicate URLs, making them invisible in search and to AI bots. Build-time pre-rendering fixes this without adding a Node service or running headless browsers in CI, giving Vue3+Vite sites a cheap path to real discoverability.

Summary

Client-side-only SPAs that rely on a single index.html fallback are nearly invisible to search engines and AI crawlers. Every route returns the homepage's head and body, soft 404s multiply, and the site ends up with a handful of old URLs in `site:` queries. This article documents a build-time pre-rendering solution that treats Vite's dist/index.html as a template and derives 21 static pages from it, each with its own TDK, JSON-LD structured data, and a static snapshot of the body content.

The implementation uses a Vite plugin hooked into writeBundle. It strips existing head tags in a specific order (comments first, then title and meta), writes dedicated per-page tags, injects a schema.org @graph containing Organization, Service, BreadcrumbList, and FAQPage, and drops a plain-HTML body snapshot into the #app mount point. The result is a folder structure like /bid/zhejiang/index.html served directly by nginx, with no server-side rendering, no headless browser, and zero runtime dependencies.

The author also generates sitemap.xml and a separate Chinese-only sitemap-zh.xml, verifies that GPTBot, ClaudeBot, PerplexityBot, and Bytespider all fetch the static pages successfully, and lists five pitfalls they hit, including double `<title>` tags, premature script closing from unescaped `<` in JSON-LD, canonical URL mismatches from missing trailing slashes, and soft 404s from the catch-all route. A final warning frames pre-rendering as legitimate only when the static snapshot matches the real rendered content exactly, otherwise it becomes cloaking and risks search engine penalties.

Takeaways
— Use the writeBundle hook, not generateBundle, because the index.html file only exists after the bundle is written to disk.
— Strip HTML comments before removing tags from the template head, because comments can contain tag-like text that regex matching will break.
— Escape '<' as "\u003c" in JSON-LD before injection to prevent premature script closing and HTML execution.
— Preserve trailing slashes in canonical URLs; nginx's directory redirect causes 301s and canonical mismatch errors when the slash is missing.
— To fix soft 404s, make the catch-all route render a real 404 page with noindex and have nginx return 404 for static file extensions via try_files $uri =404.
— The build generated 21 static pages (12 Chinese, 9 English) and sitemap.xml with 20 URLs.
— GPTBot, ClaudeBot, PerplexityBot, and Bytespider all returned 200 and read full body content from the static pages.
— A separate Chinese-only sitemap-zh.xml avoids diluting Baidu's crawl budget, which barely indexes English pages.
Conclusions

The choice to use regex for head tag stripping instead of a DOM parser is a pragmatic trade-off that works only if the index.html template stays stable and comments are kept clean of tag-like text; it shifts risk from build time to team discipline.

Converting SEO from a runtime problem to a build-time problem fits naturally with fully decoupled SPA + API architectures, making it a no-cost win compared to SSR or headless browser approaches.

The pre-rendered snapshot must match what Vue actually renders, otherwise the technique crosses into cloaking and invites search penalties, so the snapshot is generated from the same data rather than hand-written.

Concepts & terms
Pre-rendering
Generating static HTML files for each route at build time, so crawlers receive fully rendered content without executing JavaScript. It differs from SSG (static site generation) in that it's applied to an existing SPA and only affects output files.
Cloaking
Showing different content to search engine crawlers than to human users. It's against search engine guidelines and can result in penalties. Pre-rendering becomes cloaking when the static snapshot diverges from what the app actually renders.
Render budget
The limited time or resources a search engine allocates to execute JavaScript on a page. Pages beyond the budget may be queued for weeks or never rendered at all.
Soft 404
A page that returns HTTP 200 but shows content not relevant to the requested URL, such as always returning the homepage. Search engines interpret this as many duplicate pages, harming crawl efficiency.
JSON-LD @graph
A schema.org structured data format that groups multiple entity types (like Organization, Service, BreadcrumbList) into a single <script> block, making it easier for search engines and AI systems to extract meaning.
writeBundle hook
A Vite build hook that runs after all output files are written to disk. It's the correct point to read and modify generated HTML, unlike generateBundle which still holds files in memory.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗