8.3 KiB
outline |
---|
deep |
Deploy Your VitePress Site
The following guides are based on some shared assumptions:
-
The VitePress site is inside the
docs
directory of your project. -
You are using the default build output directory (
.vitepress/dist
). -
VitePress is installed as a local dependency in your project, and you have set up the following scripts in your
package.json
:{ "scripts": { "docs:build": "vitepress build docs", "docs:preview": "vitepress preview docs" } }
Build and Test Locally
-
Run this command to build the docs:
$ npm run docs:build
-
Once built, preview it locally by running:
$ npm run docs:preview
The
preview
command will boot up a local static web server that will serve the output directory.vitepress/dist
athttp://localhost:4173
. You can use this to make sure everything looks good before pushing to production. -
You can configure the port of the server by passing
--port
as an argument.{ "scripts": { "docs:preview": "vitepress preview docs --port 8080" } }
Now the docs:preview
method will launch the server at http://localhost:8080
.
Setting a Public Base Path
By default, we assume the site is going to be deployed at the root path of a domain (/
). If your site is going to be served at a sub-path, e.g. https://mywebsite.com/blog/
, then you need to set the base
option to '/blog/'
in the VitePress config.
Example: If you're using Github (or GitLab) Pages and deploying to user.github.io/repo/
, then set your base
to /repo/
.
HTTP Cache Headers
If you have control over the HTTP headers on your production server, you can configure cache-control
headers to achieve better performance on repeated visits.
The production build uses hashed file names for static assets (JavaScript, CSS and other imported assets not in public
). If you inspect the production preview using your browser devtools' network tab, you will see files like app.4f283b18.js
.
This 4f283b18
hash is generated from the content of this file. The same hashed URL is guaranteed to serve the same file content - if the contents change, the URLs change too. This means you can safely use the strongest cache headers for these files. All such files will be placed under assets/
in the output directory, so you can configure the following header for them:
Cache-Control: max-age=31536000,immutable
:::details Example Netlify _headers
file
/assets/*
cache-control: max-age=31536000
cache-control: immutable
Note: the _headers
file should be placed in the public directory - in our case, docs/public/_headers
- so that it is copied verbatim to the output directory.
Netlify custom headers documentation
:::
:::details Example Vercel config in vercel.json
{
"headers": [
{
"source": "/assets/(.*)",
"headers": [
{
"key": "Cache-Control",
"value": "max-age=31536000, immutable"
}
]
}
]
}
Note: the vercel.json
file should be placed at the root of your repository.
Vercel documentation on headers config
:::
Platform Guides
Netlify / Vercel / Cloudflare Pages / AWS Amplify / Render
Set up a new project and change these settings using your dashboard:
- Build Command:
npm run docs:build
- Output Directory:
docs/.vitepress/dist
- Node Version:
16
(or above, by default it usually will be 14 or 16, but on Cloudflare Pages the default is still 12, so you may need to change that)
::: warning Don't enable options like Auto Minify for HTML code. It will remove comments from output which have meaning to Vue. You may see hydration mismatch errors if they get removed. :::
GitHub Pages
-
In your theme config file,
docs/.vitepress/config.js
, set thebase
property to the name of your GitHub repository. If you plan to deploy your site tohttps://foo.github.io/bar/
, then you should set base to'/bar/'
. It should always start and end with a slash. -
Create a file named
deploy.yml
inside.github/workflows
directory of your project with the following content:name: Deploy on: workflow_dispatch: {} push: branches: - main jobs: deploy: runs-on: ubuntu-latest permissions: pages: write id-token: write environment: name: github-pages url: ${{ steps.deployment.outputs.page_url }} steps: - uses: actions/checkout@v3 with: fetch-depth: 0 - uses: actions/setup-node@v3 with: node-version: 16 cache: npm - run: npm install --frozen-lockfile - name: Build run: npm run docs:build - uses: actions/configure-pages@v2 - uses: actions/upload-pages-artifact@v1 with: path: docs/.vitepress/dist - name: Deploy id: deployment uses: actions/deploy-pages@v1
::: tip Please replace the corresponding branch name. For example, if the branch you want to build is
master
, then you should replacemain
withmaster
in the above file. ::: -
In your repository's Settings under Pages menu item, select
GitHub Actions
in Build and deployment's Source. -
Now commit your code and push it to the
main
branch. -
Wait for actions to complete.
-
In your repository's Settings under Pages menu item, click
Visit site
, then you can see your site. Your docs will automatically deploy each time you push.
GitLab Pages
-
Set
outDir
indocs/.vitepress/config.js
to../public
. -
Still in your config file,
docs/.vitepress/config.js
, set thebase
property to the name of your GitLab repository. If you plan to deploy your site tohttps://foo.gitlab.io/bar/
, then you should set base to'/bar/'
. It should always start and end with a slash. -
Create a file called
.gitlab-ci.yml
in the root of your project with the content below. This will build and deploy your site whenever you make changes to your content:image: node:16 pages: cache: paths: - node_modules/ script: - npm install - npm run docs:build artifacts: paths: - public only: - main
-
Alternatively, if you want to use an alpine version of node, you have to install
git
manually. In that case, the code above modifies to this:image: node:16-alpine pages: cache: paths: - node_modules/ before_script: - apk add git script: - npm install - npm run docs:build artifacts: paths: - public only: - main
Azure Static Web Apps
-
Follow the official documentation.
-
Set these values in your configuration file (and remove the ones you don't require, like
api_location
):app_location
:/
output_location
:docs/.vitepress/dist
app_build_command
:npm run docs:build
Firebase
-
Create
firebase.json
and.firebaserc
at the root of your project:firebase.json
:{ "hosting": { "public": "docs/.vitepress/dist", "ignore": [] } }
.firebaserc
:{ "projects": { "default": "<YOUR_FIREBASE_ID>" } }
-
After running
npm run docs:build
, run this command to deploy:firebase deploy
Surge
-
After running
npm run docs:build
, run this command to deploy:npx surge docs/.vitepress/dist
Heroku
-
Follow documentation and guide given in
heroku-buildpack-static
. -
Create a file called
static.json
in the root of your project with the below content:{ "root": "docs/.vitepress/dist" }