VitePress サイトをデプロイする
以下のガイドは、次の前提に基づいています。
- VitePress のサイトはプロジェクトの
docsディレクトリ内にある。 - デフォルトのビルド出力ディレクトリ(
.vitepress/dist)を使用している。 - VitePress はプロジェクトのローカル依存としてインストールされており、
package.jsonに次のスクリプトが設定されている。
{
"scripts": {
"docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs"
}
}ローカルでビルドしてテストする
次のコマンドでドキュメントをビルドします。
sh$ npm run docs:buildビルド後、次のコマンドでローカルプレビューします。
sh$ npm run docs:previewpreviewコマンドはローカルの静的 Web サーバーを起動し、出力ディレクトリ.vitepress/distをhttp://localhost:4173で配信します。プロダクションへプッシュする前に見た目を確認できます。--port引数でサーバーのポートを設定できます。json{ "scripts": { "docs:preview": "vitepress preview docs --port 8080" } }これで
docs:previewはhttp://localhost:8080でサーバーを起動します。
公開ベースパスの設定
デフォルトでは、サイトはドメインのルートパス(/)にデプロイされることを想定しています。サイトをサブパス、例:https://mywebsite.com/blog/ で配信する場合は、VitePress の設定で base オプションを '/blog/' に設定してください。
例: GitHub(または GitLab)Pages に 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,immutableNetlify の _headers ファイル例
/assets/*
cache-control: max-age=31536000
cache-control: immutable注:_headers ファイルは public ディレクトリ に配置します(この例では docs/public/_headers)。そうすると、ビルド出力にそのままコピーされます。
vercel.json による Vercel 設定例
{
"headers": [
{
"source": "/assets/(.*)",
"headers": [
{
"key": "Cache-Control",
"value": "max-age=31536000, immutable"
}
]
}
]
}注:vercel.json は リポジトリのルート に配置してください。
プラットフォーム別ガイド
Netlify / Vercel / Cloudflare Pages / AWS Amplify / Render
新しいプロジェクトを作成し、ダッシュボードで次の設定に変更します。
- Build Command:
npm run docs:build - Output Directory:
docs/.vitepress/dist - Node Version:
20(以上)
WARNING
HTML の Auto Minify のようなオプションを有効にしないでください。Vue にとって意味のあるコメントが出力から削除され、削除されるとハイドレーションの不整合エラーが発生する可能性があります。
GitHub Pages
プロジェクトの
.github/workflowsディレクトリにdeploy.ymlを作成し、以下の内容を記述します。yaml# Sample workflow for building and deploying a VitePress site to GitHub Pages # name: Deploy VitePress site to Pages on: # Runs on pushes targeting the `main` branch. Change this to `master` if you're # using the `master` branch as the default branch. 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@v4 with: fetch-depth: 0 # Not needed if lastUpdated is not enabled # - uses: pnpm/action-setup@v3 # Uncomment this block if you're using pnpm # with: # version: 9 # Not needed if you've set "packageManager" in package.json # - uses: oven-sh/setup-bun@v1 # Uncomment this if you're using Bun - name: Setup Node uses: actions/setup-node@v4 with: node-version: 22 cache: npm # or pnpm / yarn - name: Setup Pages uses: actions/configure-pages@v4 - 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 - name: Upload artifact uses: actions/upload-pages-artifact@v3 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@v4WARNING
VitePress の
baseオプションが正しく設定されていることを確認してください。詳細は 公開ベースパスの設定 を参照してください。リポジトリ設定の「Pages」メニューで、「Build and deployment > Source」を「GitHub Actions」に設定します。
変更を
mainブランチにプッシュし、GitHub Actions の完了を待ちます。設定に応じて、サイトはhttps://<username>.github.io/[repository]/またはhttps://<custom-domain>/にデプロイされます。以後、mainへのプッシュごとに自動デプロイされます。
GitLab Pages
VitePress の設定で
outDirを../publicに設定します。https://<username>.gitlab.io/<repository>/にデプロイする場合はbaseを'/<repository>/'に設定します。カスタムドメイン、ユーザー/グループページ、または GitLab の「Use unique domain」を有効にしている場合はbaseは不要です。プロジェクトのルートに
.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: - main
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": [] } }.firebaserc:json{ "projects": { "default": "<YOUR_FIREBASE_ID>" } }npm run docs:buildの後、次のコマンドでデプロイします。shfirebase deploy
Surge
npm run docs:buildの後、次のコマンドでデプロイします。shnpx surge docs/.vitepress/dist
Heroku
heroku-buildpack-staticのドキュメントとガイドに従います。プロジェクトのルートに
static.jsonを作成し、以下を記述します。json{ "root": "docs/.vitepress/dist" }
Edgio
Creating and Deploying a VitePress App To Edgio を参照してください。
Kinsta Static Site Hosting
VitePress を こちらの手順 に従ってデプロイできます。
Stormkit
VitePress プロジェクトを Stormkit にデプロイ する手順を参照してください。
CloudRay
CloudRay でのデプロイ方法は こちらの手順 を参照してください。
Nginx
以下は Nginx サーバーブロックの設定例です。一般的なテキスト系アセットの gzip 圧縮、VitePress サイトの静的ファイル配信における適切なキャッシュヘッダー、そして cleanUrls: true のハンドリングを含みます。
server {
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
listen 80;
server_name _;
index index.html;
location / {
# content location
root /app;
# exact matches -> reverse clean urls -> folders -> not found
try_files $uri $uri.html $uri/ =404;
# non existent pages
error_page 404 /404.html;
# a folder without index.html raises 403 in this setup
error_page 403 /404.html;
# adjust caching headers
# files in the assets folder have hashes filenames
location ~* ^/assets/ {
expires 1y;
add_header Cache-Control "public, immutable";
}
}
}この設定は、ビルド済みの VitePress サイトがサーバー上の /app ディレクトリに配置されていることを想定しています。サイトのファイルが別の場所にある場合は、root ディレクティブを適宜変更してください。
index.html をデフォルトにしない
try_files の解決先を、他の Vue アプリのように index.html にフォールバックさせないでください。不正なページ状態になります。
詳細は 公式 nginx ドキュメント、Issue #2837、#3235、および Mehdi Merah 氏の ブログ記事 を参照してください。