GitHub Pages
The following steps demonstrates how you can deploy your Vite app to the GitHub pages.
1. Create a repository
Create a repository to GitHub for your react app. In this example, I name my repository to reactapp.
2. Push your code to Github
Push your code to Github repository you just created by using the commands shown in your Github repository.
3. Project config
Open your app's vite.config.ts
file and add the base
property. The value is the name of your repository with leading and trailing slashes.
export default defineConfig({
base: '/{repo-name}/',
plugins: [react()],
test: {
globals: true,
environment: 'jsdom',
},
})
Open you app's package.json
file and add the homepage
property. Use your Github username and repository name.
"homepage": "https://{username}.github.io/{repo-name}/"
GitHub Pages does not support React Router browserRouter
. You can use hashRouter
from the same library instead.
Since the application is configured to be deployed to a subdirectory (the base
definition above), it is necessary to tell the router what the base path is. It can be defined by giving createBrowserRouter
/createHashRouter
a second parameter that defines the basename. Refer to React Router createBrowserRouter documentation for more information.
Vite sets the base path in environment variable BASE_URL
. You can define the second parameter using the environment variable as follows to make routing work both on localhost and in the publishing environment:
{
basename: import.meta.env.BASE_URL
}
4. Install gh-pages
Navigate to your app folder in your terminal and install gh-pages
npm package as a development dependency (https://github.com/tschaub/gh-pages).
npm install gh-pages --save-dev
The gh-pages
library is a Node.js package that provides a simple way to publish files to a gh-pages
branch on GitHub. This branch is often used to host static websites directly from a GitHub repository using GitHub Pages.
5. Deployment scripts
Add the deployment scripts to your package.json file.
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"predeploy": "npm run build",
"deploy": "gh-pages -d dist"
},
6. Deploy
Deploy your app to Github pages using the following command:
npm run deploy
After the succesfull deployment, your app URL is the following.
https://{username}.github.io/{repo_name}/
You can find the url from your repository's Settings | Pages