VuePress 2运行时动态添加页面及更新搜索索引相关问题咨询
Core Answer
VuePress 2 does not support runtime (post-deployment) page injection into $site.pages or updating the built-in search index. Unlike VuePress 1, VuePress 2 is built as a pure static site generator—all page data and the built-in search index are generated during the build process, making them static and unmodifiable without rebuilding the entire site.
Why This Change?
VuePress 2 refactored its architecture to prioritize static pre-rendering for better performance and stability. The $site.pages object is compiled entirely at build time, so there’s no mechanism to inject new pages or modify this object after deployment. The built-in search plugin generates a static index file during the build, which can’t be updated dynamically.
Recommended Solutions for Dynamic Content & Search
1. Custom Client-Side Search
- Fetch dynamic content (e.g., video data from an API) directly on the client side after the site loads.
- Implement a client-side search component using lightweight libraries like Fuse.js or Lunr.js to index the fetched content in memory.
- Example workflow:
- Create a Vue component that fetches data on mount.
- Initialize a search index with the fetched content.
- Add a search input that filters results in real time using the client-side index.
2. Third-Party Search Services
- Use external search services (e.g., Algolia, Meilisearch, Elasticsearch) to handle dynamic content indexing.
- Push new/updated content to the service via its API whenever your data changes—no need to rebuild your VuePress site.
- Integrate the service’s search widget directly into your VuePress site to enable real-time search of dynamic content.
3. Hybrid Static + Dynamic Approach
- Split your content: Keep static, rarely updated content in VuePress (taking advantage of its static generation benefits).
- For frequently updated dynamic content, embed a separate Vue application or section within your VuePress site that fetches data at runtime and includes its own search functionality.
Official Documentation Context
The VuePress 2 official docs explicitly state that it’s a static site generator, with all page rendering and asset generation occurring during the build phase. The built-in search plugin’s documentation clarifies that its index is generated statically at build time, with no support for runtime updates.
内容的提问来源于stack exchange,提问作者Sharktank

