部署你的 VitePress 网站 
以下指南基于一些共设前提:
VitePress 站点位于项目的
docs目录中。你使用的是默认的生成输出目录 (
.vitepress/dist)。VitePress 作为本地依赖项安装在项目中,并且你已在
package.json中设置以下脚本:json{ "scripts": { "docs:build": "vitepress build docs", "docs:preview": "vitepress preview docs" } }{ "scripts": { "docs:build": "vitepress build docs", "docs:preview": "vitepress preview docs" } }1
2
3
4
5
6
本地构建与测试 
你可以运行以下命令来构建文档:
sh$ npm run docs:build$ npm run docs:build1构建文档后,通过运行以下命令在本地预览它:
sh$ npm run docs:preview$ npm run docs:preview1
preview 命令将启动一个本地静态 Web 服务器http://localhost:4173,该服务器以 .vitepress/dist 作为源文件。这是检查生产版本在本地环境中是否正常的一种简单方法。
你可以通过传递
--port作为参数来配置服务器的端口。json{ "scripts": { "docs:preview": "vitepress preview docs --port 8080" } }{ "scripts": { "docs:preview": "vitepress preview docs --port 8080" } }1
2
3
4
5
现在 docs:preview 方法将在 http://localhost:8080 启动服务器。
设定 public 根目录 
默认情况下,我们假设站点将部署在域名 (/)的根路径上。如果你的网站将在子路径中提供服务,例如 https://mywebsite.com/blog/,则需要在 VitePress 配置中将 base选项设置为 '/blog/'。
例: 如果你使用的是 Github(或 GitLab)页面并部署到 user.github.io/repo/,请将你的 base 设置为 /repo/。
HTTP 缓存标头 
如果可以控制生产服务器上的 HTTP 标头,则可以配置 cache-control 标头以在重复访问时获得更好的性能。
生产版本对静态资源(JavaScript、CSS 和其他非 public 目录中的导入资源)使用哈希文件名。如果你使用浏览器开发工具的网络选项卡检查生产预览,你将看到类似 app.4f283b18.js 的文件。
此哈希 4f283b18 是从此文件的内容生成的。相同的哈希 URL 保证提供相同的文件内容 —— 如果内容更改,URL 也会更改。这意味着你可以安全地为这些文件使用最强的缓存标头。所有此类文件都将放置在输出目录的 assets/ 中,因此你可以为它们配置以下标头:
Cache-Control: max-age=31536000,immutableCache-Control: max-age=31536000,immutableNetlify 示例 _headers 文件
/assets/*
  cache-control: max-age=31536000
  cache-control: immutable/assets/*
  cache-control: max-age=31536000
  cache-control: immutable2
3
注意:该 _headers 文件应放置在public 目录中(在我们的例子中是 docs/public/_headers),以便将其逐字复制到输出目录。
Vercel 配置示例 vercel.json
{
	"headers": [
		{
			"source": "/assets/(.*)",
			"headers": [
				{
					"key": "Cache-Control",
					"value": "max-age=31536000, immutable"
				}
			]
		}
	]
}{
	"headers": [
		{
			"source": "/assets/(.*)",
			"headers": [
				{
					"key": "Cache-Control",
					"value": "max-age=31536000, immutable"
				}
			]
		}
	]
}2
3
4
5
6
7
8
9
10
11
12
13
注意:vercel.json 文件应放在存储库的根目录中。
各平台部署指南 
Netlify / Vercel / Cloudflare Pages / AWS Amplify / Render 
使用仪表板创建新项目并更改这些设置:
- 构建命令: 
npm run docs:build - 输出目录: 
docs/.vitepress/dist - node 版本: 
18(或更高版本)警告
不要为 HTML 代码启用 Auto Minify 等选项。它将从输出中删除对 Vue 有意义的注释。如果被删除,你可能会看到激活不匹配错误。
 
GitHub Pages 
在项目的
.github/workflows目录中创建一个名为deploy.yml的文件,其中包含这样的内容:yaml# 用于构建 VitePress 站点并将其部署到 GitHub Pages 的示例工作流程 # name: Deploy VitePress site to Pages on: # 针对 `main` 分支的推送上运行。 # 如果你使用 `master` 作为默认分支,请将其更改为 `master` push: branches: [main] # Allows you to run this workflow manually from the Actions tab workflow_dispatch: # Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages permissions: contents: read pages: write id-token: write # Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued. # However, do NOT cancel in-progress runs as we want to allow these production deployments to complete. concurrency: group: pages cancel-in-progress: false jobs: # Build job build: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v3 with: fetch-depth: 0 # Not needed if lastUpdated is not enabled # - uses: pnpm/action-setup@v2 # Uncomment this if you're using pnpm # - uses: oven-sh/setup-bun@v1 # Uncomment this if you're using Bun - name: Setup Node uses: actions/setup-node@v3 with: node-version: 18 cache: npm # or pnpm / yarn - name: Setup Pages uses: actions/configure-pages@v3 - name: Install dependencies run: npm ci # or pnpm install / yarn install / bun install - name: Build with VitePress run: | npm run docs:build # or pnpm docs:build / yarn docs:build / bun run docs:build touch docs/.vitepress/dist/.nojekyll - name: Upload artifact uses: actions/upload-pages-artifact@v2 with: path: docs/.vitepress/dist # Deployment job deploy: environment: name: github-pages url: ${{ steps.deployment.outputs.page_url }} needs: build runs-on: ubuntu-latest name: Deploy steps: - name: Deploy to GitHub Pages id: deployment uses: actions/deploy-pages@v2# 用于构建 VitePress 站点并将其部署到 GitHub Pages 的示例工作流程 # name: Deploy VitePress site to Pages on: # 针对 `main` 分支的推送上运行。 # 如果你使用 `master` 作为默认分支,请将其更改为 `master` push: branches: [main] # Allows you to run this workflow manually from the Actions tab workflow_dispatch: # Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages permissions: contents: read pages: write id-token: write # Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued. # However, do NOT cancel in-progress runs as we want to allow these production deployments to complete. concurrency: group: pages cancel-in-progress: false jobs: # Build job build: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v3 with: fetch-depth: 0 # Not needed if lastUpdated is not enabled # - uses: pnpm/action-setup@v2 # Uncomment this if you're using pnpm # - uses: oven-sh/setup-bun@v1 # Uncomment this if you're using Bun - name: Setup Node uses: actions/setup-node@v3 with: node-version: 18 cache: npm # or pnpm / yarn - name: Setup Pages uses: actions/configure-pages@v3 - name: Install dependencies run: npm ci # or pnpm install / yarn install / bun install - name: Build with VitePress run: | npm run docs:build # or pnpm docs:build / yarn docs:build / bun run docs:build touch docs/.vitepress/dist/.nojekyll - name: Upload artifact uses: actions/upload-pages-artifact@v2 with: path: docs/.vitepress/dist # Deployment job deploy: environment: name: github-pages url: ${{ steps.deployment.outputs.page_url }} needs: build runs-on: ubuntu-latest name: Deploy steps: - name: Deploy to GitHub Pages id: deployment uses: actions/deploy-pages@v21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66警告
确保 VitePress 中的
base选项配置正确。有关更多详细信息,请参阅设置 base 路径。在存储库设置中的 “Pages” 菜单项下,选择 “Build and deployment > Source > GitHub Actions”。
将更改推送到
main分支并等待 GitHub Actions 工作流完成。你应该看到你的站点部署到https://<username>.github.io/[repository]/或https://<custom-domain>/,这取决于你的设置。你的网站将在每次推送到main分支时自动部署。
GitLab Pages 
如果你想部署到
https://<username> .gitlab.io/<repository> /,将 VitePress 配置中的outDir设置为../public。将base选项配置为'/<repository>/'。在项目的根目录中创建一个名为
.gitlab-ci.yml的文件,其中包含以下内容。每当你更改内容时,这都会构建和部署你的网站:使用以下内容在项目的根目录中创建一个名为
.gitlab-ci.yml的文件。每当你更改内容时,会自动构建和部署你的站点:yamlimage: node:18 pages: cache: paths: - node_modules/ script: # - apk add git # Uncomment this if you're using small docker images like alpine and have lastUpdated enabled - npm install - npm run docs:build artifacts: paths: - public only: - mainimage: node:18 pages: cache: paths: - node_modules/ script: # - apk add git # Uncomment this if you're using small docker images like alpine and have lastUpdated enabled - npm install - npm run docs:build artifacts: paths: - public only: - main1
2
3
4
5
6
7
8
9
10
11
12
13
14
Azure Static Web Apps 
遵循官方文档。
在配置文件中设置这些值(并删除不需要的值,如
api_location):app_location:/output_location:docs/.vitepress/distapp_build_command:npm run docs:build
Firebase 
在项目的根目录下创建
firebase.json和.firebaserc:firebase.json:json{ "hosting": { "public": "docs/.vitepress/dist", "ignore": [] } }{ "hosting": { "public": "docs/.vitepress/dist", "ignore": [] } }1
2
3
4
5
6.firebaserc:json{ "projects": { "default": "<YOUR_FIREBASE_ID>" } }{ "projects": { "default": "<YOUR_FIREBASE_ID>" } }1
2
3
4
5运行
npm run docs:build后,运行此命令进行部署:shfirebase deployfirebase deploy1
Surge 
运行
npm run docs:build后,运行此命令进行部署:shnpx surge docs/.vitepress/distnpx surge docs/.vitepress/dist1
Heroku 
遵循
heroku-buildpack-static中给出的文档和指南。使用以下内容在项目的根目录中创建一个名为
static.json的文件:json{ "root": "docs/.vitepress/dist" }{ "root": "docs/.vitepress/dist" }1
2
3
Edgio 
请参阅创建并部署 VitePress 应用程序到 Edgio。