This tutorial aims to guide you through the process of deploying a Vite project to the web while ensuring all your project assets are correctly included and load as expected once the site goes live.
By the end of this tutorial, you should be able to:
Prerequisites
Deploying a Vite project involves building the project, preparing the assets, and then deploying the project to a server.
Building the Vite Project
Before deploying, we need to build our Vite project. Building transforms our source code into production-ready code. Run the following command in your project directory:
npm run build
This will create a dist
folder which contains all our project's build assets, ready for deployment.
Preparing the assets
Vite already handles the inclusion of assets in the build process. However, to ensure all assets load correctly, confirm that Vite's base
config option in vite.config.js
matches the public path of your deployed project:
export default {
base: '/path/to/your/project/'
}
Deploying the Vite Project
After preparing the assets, you can now deploy your project to your preferred hosting platform. This step varies depending on the platform. Most of them require you to upload the dist
directory.
Here is an example of a vite.config.js
file:
// vite.config.js
export default {
base: '/my-vite-project/',
// other configurations...
}
The base
config option should match the public path of your deployed project. If your site will be deployed at https://www.example.com/my-vite-project/
, then the base
option should be /my-vite-project/
.
In this tutorial, we've:
The next steps could be learning about optimizing your Vite project, using plugins, and exploring advanced Vite configurations.
For additional resources, refer to the official Vite documentation.
base
configuration in vite.config.js
and observe the effect on asset loading in your deployed project.Solutions:
dist
directory and configure the deploy settings.base
config option will affect where the browser tries to load your assets from. If it doesn't match your project's public path, your assets will not load correctly. Tips for further practice
Practice deploying more complex projects and explore more features of Vite. Try using Vite with different frameworks like React, Vue, or Svelte.