Skip to content

VitePress 是什么?

当前文档字数: 0 字 | 阅读时长约: 0 分钟

更新时间:2025/03/08

简介

原文

VitePress is a Static Site Generator (SSG) designed for building fast, content-centric websites. In a nutshell, VitePress takes your source content written in Markdown, applies a theme to it, and generates static HTML pages that can be easily deployed anywhere.

VitePress 是一个静态站点生成器 (SSG),专为构建快速、以内容为中心的站点而设计。简而言之,VitePress 获取用 Markdown 编写的内容,对其应用主题,并生成可以轻松部署到任何地方的静态 HTML 页面。

使用场景

  • 文档
原文

VitePress ships with a default theme designed for technical documentation.

The official Vue.js documentation is also based on VitePress, but uses a custom theme shared between multiple translations.

VitePress 附带一个专为技术文档设计的默认主题。

Vue.js 官方文档也是基于 VitePress 的。但是为了可以在不同的翻译文档之间切换,它自定义了自己的主题。

  • 博客、档案和营销网站
原文

VitePress supports fully customized themes, with the developer experience of a standard Vite + Vue application. Being built on Vite also means you can directly leverage Vite plugins from its rich ecosystem. In addition, VitePress provides flexible APIs to load data (local or remote) and dynamically generate routes. You can use it to build almost anything as long as the data can be determined at build time.

The official Vue.js blog is a simple blog that generates its index page based on local content.

VitePress 支持完全的自定义主题,具有标准 Vite + Vue 应用程序的开发体验。基于 Vite 构建还意味着可以直接利用其生态系统中丰富的 Vite 插件。此外,VitePress 提供了灵活的 API 来加载数据 (本地或远程),也可以动态生成路由。只要可以在构建时确定数据,就可以使用它来构建几乎任何东西。

Vue.js 官方博客是一个简单的博客页面,它根据本地内容生成其索引页面。

开发体验

原文

VitePress aims to provide a great Developer Experience (DX) when working with Markdown content.

Vite-Powered: instant server start, with edits always instantly reflected (<100ms) without page reload.

Built-in Markdown Extensions: Frontmatter, tables, syntax highlighting... you name it. Specifically, VitePress provides many advanced features for working with code blocks, making it ideal for highly technical documentation.

Vue-Enhanced Markdown: each Markdown page is also a Vue Single-File Component, thanks to Vue template's 100% syntax compatibility with HTML. You can embed interactivity in your static content using Vue templating features or imported Vue components.

VitePress 旨在使用 Markdown 生成内容时提供出色的开发体验。

  • Vite 驱动:即时服务器启动,始终立即反映 (<100ms) 编辑变化,无需重新加载页面。

  • 内置 Markdown 扩展:frontmatter、表格、语法高亮……应有尽有。具体来说,VitePress 提供了许多用于处理代码块的高级功能,使其真正成为技术文档的理想选择。

  • Vue 增强的 Markdown:每个 Markdown 页面都是 Vue 单文件组件,这要归功于 Vue 模板与 HTML 的 100% 语法兼容性。可以使用 Vue 模板语法或导入的 Vue 组件在静态内容中嵌入交互性。

性能

原文

Unlike many traditional SSGs where each navigation results in a full page reload, a website generated by VitePress serves static HTML on the initial visit, but becomes a Single Page Application (SPA) for subsequent navigation within the site. This model, in our opinion, provides an optimal balance for performance:

与许多传统的 SSG 不同,每次导航都会导致页面完全重新加载,VitePress 生成的网站在初次访问时提供静态 HTML,但它变成了单页应用程序(SPA)进行站点内的后续导航。我们认为,这种模式为性能提供了最佳平衡:

  • 快速的初始加载
原文

The initial visit to any page will be served the static, pre-rendered HTML for fast loading speed and optimal SEO. The page then loads a JavaScript bundle that turns the page into a Vue SPA ("hydration"). Contrary to common assumptions of SPA hydration being slow, this process is actually extremely fast thanks to Vue 3's raw performance and compiler optimizations. On PageSpeed Insights, typical VitePress sites achieve near-perfect performance scores even on low-end mobile devices with a slow network.

对任何页面的初次访问都将会是静态的、预呈现的 HTML,以实现极快的加载速度和最佳的 SEO。然后页面加载一个 JavaScript bundle,将页面变成 Vue SPA (这被称为“激活”)。与 SPA 激活缓慢的常见假设不同,由于 Vue 3 良好的原始性能和编译优化,这个过程实际上非常快。在 PageSpeed Insights 上,典型的 VitePress 站点即使在网络速度较慢的低端移动设备上也能获得近乎完美的性能分数。

  • 加载完成后可以快速切换
原文

More importantly, the SPA model leads to better user experience after the initial load. Subsequent navigation within the site will no longer cause a full page reload. Instead, the incoming page's content will be fetched and dynamically updated. VitePress also automatically pre-fetches page chunks for links that are within viewport. In most cases, post-load navigation will feel instant.

更重要的是,SPA 模型在首次加载后能够提升用户体验。用户在站点内导航时,不会再触发整个页面的刷新。而是通过获取并动态更新页面的内容来实现切换。VitePress 还会自动预加载视口范围内链接对应的页面片段。这样一来,大部分情况下,用户在加载完成后就能立即浏览新页面。

  • 高效的交互
原文

To be able to hydrate the dynamic Vue parts embedded inside static Markdown, each Markdown page is processed as a Vue component and compiled into JavaScript. This may sound inefficient, but the Vue compiler is smart enough to separate the static and dynamic parts, minimizing both the hydration cost and payload size. For the initial page load, the static parts are automatically eliminated from the JavaScript payload and skipped during hydration.

为了能够嵌入静态 Markdown 中的动态 Vue 部分,每个 Markdown 页面都被处理为 Vue 组件并编译成 JavaScript。这听起来可能效率低下,但 Vue 编译器足够聪明,可以将静态和动态部分分开,从而最大限度地减少激活成本和有效负载大小。对于初始的页面加载,静态部分会自动从 JavaScript 有效负载中删除,并在激活期间跳过。

VuePress 又是什么?

原文

VitePress is the spiritual successor of VuePress. The original VuePress was based on Vue 2 and webpack. With Vue 3 and Vite under the hood, VitePress provides significantly better DX, better production performance, a more polished default theme, and a more flexible customization API.

The API difference between VitePress and VuePress mostly lies in theming and customization. If you are using VuePress 1 with the default theme, it should be relatively straightforward to migrate to VitePress.

There has also been effort invested into VuePress 2, which also supports Vue 3 and Vite with more compatibility with VuePress 1. However, maintaining two SSGs in parallel isn't sustainable, so the Vue team has decided to focus on VitePress as the main recommended SSG in the long run.

VitePress 灵感来源于 VuePress。最初的 VuePress 基于 Vue 2 和 webpack。借助 Vue 3 和 Vite,VitePress 提供了更好的开发体验、更好的生产性能、更精美的默认主题和更灵活的自定义 API。

VitePress 和 VuePress 之间的 API 区别主要在于主题和自定义。如果使用的是带有默认主题的 VuePress 1,迁移到 VitePress 应该相对简单。

VuePress 2 我们也投入了精力,它也支持 Vue 3 和 Vite,与 VuePress 1 的兼容性更好。但是,并行维护两个 SSG 是难以持续的,因此 Vue 团队决定将重点放在 VitePress,作为长期的主要 SSG 选择推荐。