Deploying a Static Site
The following guides are based on some shared assumptions:
- You are using the default build output location (
dist
). This location can be changed usingbuild.outDir
, and you can extrapolate instructions from these guides in that case. - You are using npm. You can use equivalent commands to run the scripts if you are using Yarn or other package managers.
- Vite is installed as a local dev dependency in your project, and you have setup the following npm scripts:
{
"scripts": {
"build": "vite build",
"preview": "vite preview"
}
}
It is important to note that vite preview
is intended for previewing the build locally and not meant as a production server.
NOTE
These guides provide instructions for performing a static deployment of your Vite site. Vite also supports Server Side Rendering. SSR refers to front-end frameworks that support running the same application in Node.js, pre-rendering it to HTML, and finally hydrating it on the client. Check out the SSR Guide to learn about this feature. On the other hand, if you are looking for integration with traditional server-side frameworks, check out the Backend Integration guide instead.
Building the App
You may run npm run build
command to build the app.
$ npm run build
By default, the build output will be placed at dist
. You may deploy this dist
folder to any of your preferred platforms.
Testing the App Locally
Once you've built the app, you may test it locally by running npm run preview
command.
$ npm run preview
The vite preview
command will boot up a local static web server that serves the files from dist
at http://localhost:4173
. It's an easy way to check if the production build looks OK in your local environment.
You may configure the port of the server by passing the --port
flag as an argument.
{
"scripts": {
"preview": "vite preview --port 8080"
}
}
Now the preview
command will launch the server at http://localhost:8080
.
GitHub Pages
Set the correct
base
invite.config.js
.If you are deploying to
https://<USERNAME>.github.io/
, or to a custom domain through GitHub Pages (eg.www.example.com
), setbase
to'/'
. Alternatively, you can removebase
from the configuration, as it defaults to'/'
.If you are deploying to
https://<USERNAME>.github.io/<REPO>/
(eg. your repository is athttps://github.com/<USERNAME>/<REPO>
), then setbase
to'/<REPO>/'
.Go to your GitHub Pages configuration in the repository settings page and choose the source of deployment as "GitHub Actions", this will lead you to create a workflow that builds and deploys your project, a sample workflow that installs dependencies and builds using npm is provided:
yml# Simple workflow for deploying static content to GitHub Pages name: Deploy static content to Pages on: # Runs on pushes targeting the default branch push: branches: ['main'] # Allows you to run this workflow manually from the Actions tab workflow_dispatch: # Sets the GITHUB_TOKEN permissions to allow deployment to GitHub Pages permissions: contents: read pages: write id-token: write # Allow one concurrent deployment concurrency: group: 'pages' cancel-in-progress: true jobs: # Single deploy job since we're just deploying deploy: environment: name: github-pages url: ${{ steps.deployment.outputs.page_url }} runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 - name: Set up Node uses: actions/setup-node@v4 with: node-version: 20 cache: 'npm' - name: Install dependencies run: npm ci - name: Build run: npm run build - name: Setup Pages uses: actions/configure-pages@v4 - name: Upload artifact uses: actions/upload-pages-artifact@v3 with: # Upload dist folder path: './dist' - name: Deploy to GitHub Pages id: deployment uses: actions/deploy-pages@v4
GitLab Pages and GitLab CI
Set the correct
base
invite.config.js
.If you are deploying to
https://<USERNAME or GROUP>.gitlab.io/
, you can omitbase
as it defaults to'/'
.If you are deploying to
https://<USERNAME or GROUP>.gitlab.io/<REPO>/
, for example your repository is athttps://gitlab.com/<USERNAME>/<REPO>
, then setbase
to'/<REPO>/'
.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:yamlimage: node:16.5.0 pages: stage: deploy cache: key: files: - package-lock.json prefix: npm paths: - node_modules/ script: - npm install - npm run build - cp -a dist/. public/ artifacts: paths: - public rules: - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
Netlify
Netlify CLI
- Install the Netlify CLI.
- Create a new site using
ntl init
. - Deploy using
ntl deploy
.
# Install the Netlify CLI
$ npm install -g netlify-cli
# Create a new site in Netlify
$ ntl init
# Deploy to a unique preview URL
$ ntl deploy
The Netlify CLI will share with you a preview URL to inspect. When you are ready to go into production, use the prod
flag:
# Deploy the site into production
$ ntl deploy --prod
Netlify with Git
- Push your code to a git repository (GitHub, GitLab, BitBucket, Azure DevOps).
- Import the project to Netlify.
- Choose the branch, output directory, and set up environment variables if applicable.
- Click on Deploy.
- Your Vite app is deployed!
After your project has been imported and deployed, all subsequent pushes to branches other than the production branch along with pull requests will generate Preview Deployments, and all changes made to the Production Branch (commonly “main”) will result in a Production Deployment.
Vercel
Vercel CLI
- Install the Vercel CLI and run
vercel
to deploy. - Vercel will detect that you are using Vite and will enable the correct settings for your deployment.
- Your application is deployed! (e.g. vite-vue-template.vercel.app)
$ npm i -g vercel
$ vercel init vite
Vercel CLI
> Success! Initialized "vite" example in ~/your-folder.
- To deploy, `cd vite` and run `vercel`.
Vercel for Git
- Push your code to your git repository (GitHub, GitLab, Bitbucket).
- Import your Vite project into Vercel.
- Vercel will detect that you are using Vite and will enable the correct settings for your deployment.
- Your application is deployed! (e.g. vite-vue-template.vercel.app)
After your project has been imported and deployed, all subsequent pushes to branches will generate Preview Deployments, and all changes made to the Production Branch (commonly “main”) will result in a Production Deployment.
Learn more about Vercel’s Git Integration.
Cloudflare Pages
Cloudflare Pages via Wrangler
- Install Wrangler CLI.
- Authenticate Wrangler with your Cloudflare account using
wrangler login
. - Run your build command.
- Deploy using
npx wrangler pages deploy dist
.
# Install Wrangler CLI
$ npm install -g wrangler
# Login to Cloudflare account from CLI
$ wrangler login
# Run your build command
$ npm run build
# Create new deployment
$ npx wrangler pages deploy dist
After your assets are uploaded, Wrangler will give you a preview URL to inspect your site. When you log into the Cloudflare Pages dashboard, you will see your new project.
Cloudflare Pages with Git
- Push your code to your git repository (GitHub, GitLab).
- Log in to the Cloudflare dashboard and select your account in Account Home > Pages.
- Select Create a new Project and the Connect Git option.
- Select the git project you want to deploy and click Begin setup
- Select the corresponding framework preset in the build setting depending on the Vite framework you have selected.
- Then save and deploy!
- Your application is deployed! (e.g
https://<PROJECTNAME>.pages.dev/
)
After your project has been imported and deployed, all subsequent pushes to branches will generate Preview Deployments unless specified not to in your branch build controls. All changes to the Production Branch (commonly “main”) will result in a Production Deployment.
You can also add custom domains and handle custom build settings on Pages. Learn more about Cloudflare Pages Git Integration.
Google Firebase
Make sure you have firebase-tools installed.
Create
firebase.json
and.firebaserc
at the root of your project with the following content:firebase.json
:json{ "hosting": { "public": "dist", "ignore": [], "rewrites": [ { "source": "**", "destination": "/index.html" } ] } }
.firebaserc
:js{ "projects": { "default": "<YOUR_FIREBASE_ID>" } }
After running
npm run build
, deploy using the commandfirebase deploy
.
Surge
First install surge, if you haven’t already.
Run
npm run build
.Deploy to surge by typing
surge dist
.
You can also deploy to a custom domain by adding surge dist yourdomain.com
.
Azure Static Web Apps
You can quickly deploy your Vite app with Microsoft Azure Static Web Apps service. You need:
- An Azure account and a subscription key. You can create a free Azure account here.
- Your app code pushed to GitHub.
- The SWA Extension in Visual Studio Code.
Install the extension in VS Code and navigate to your app root. Open the Static Web Apps extension, sign in to Azure, and click the '+' sign to create a new Static Web App. You will be prompted to designate which subscription key to use.
Follow the wizard started by the extension to give your app a name, choose a framework preset, and designate the app root (usually /
) and built file location /dist
. The wizard will run and will create a GitHub action in your repo in a .github
folder.
The action will work to deploy your app (watch its progress in your repo's Actions tab) and, when successfully completed, you can view your app in the address provided in the extension's progress window by clicking the 'Browse Website' button that appears when the GitHub action has run.
Render
You can deploy your Vite app as a Static Site on Render.
Create a Render account.
In the Dashboard, click the New button and select Static Site.
Connect your GitHub/GitLab account or use a public repository.
Specify a project name and branch.
- Build Command:
npm run build
- Publish Directory:
dist
- Build Command:
Click Create Static Site.
Your app should be deployed at
https://<PROJECTNAME>.onrender.com/
.
By default, any new commit pushed to the specified branch will automatically trigger a new deployment. Auto-Deploy can be configured in the project settings.
You can also add a custom domain to your project.
Flightcontrol
Deploy your static site using Flightcontrol by following these instructions.
Kinsta Static Site Hosting
Deploy your static site using Kinsta by following these instructions.