|
| 1 | +# Vite GitHub Pages deployment template |
| 2 | + |
| 3 | +A simple but robust deployment of a Vite app to GitHub Pages. This project uses React + TypeScript as an example, but [you can easily switch to a different stack by following the instructions below](#switching-away-from-react--typescript). |
| 4 | + |
| 5 | +[The Actions workflow](./.github/workflows/publish.yaml) is built from a combination of the [Vite documentation on static sites][vite-static], the documentation from the various GitHub Actions in use and my own wisdom. |
| 6 | + |
| 7 | +[vite-static]: https://vitejs.dev/guide/static-deploy.html |
| 8 | + |
| 9 | +## Create a new repo from this template |
| 10 | + |
| 11 | +Press the _Use this template_ button (or equivalent for your language) near where the _Code_ button usually is. |
| 12 | +[See GitHub's docs on creating a repository from a template][template-docs] for more details. |
| 13 | + |
| 14 | +Once created, the repo's Pages configuration needs to be switched to GitHub Actions mode. |
| 15 | +Go to the repo > _Settings_ > _Pages_ > and change _Source_ to **GitHub Actions**. |
| 16 | +[See _Publishing with a custom GitHub Actions workflow_ in GitHub's documentation][pages-actions-publishing]. |
| 17 | + |
| 18 | +[template-docs]: https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-repository-from-a-template#creating-a-repository-from-a-template |
| 19 | +[pages-actions-publishing]: https://docs.github.com/en/pages/getting-started-with-github-pages/configuring-a-publishing-source-for-your-github-pages-site#publishing-with-a-custom-github-actions-workflow |
| 20 | + |
| 21 | +## Switching away from React + TypeScript |
| 22 | + |
| 23 | +1. Create a new project using the desired create-vite template by [following the Vite getting started guide][vite-get-started]. |
| 24 | +2. Copy the contents of the `.github` directory from this repo to your new project -- it'll work with any project generated from Vite's templates. |
| 25 | + |
| 26 | +[vite-get-started]: https://vitejs.dev/guide/ |
| 27 | + |
| 28 | +## Fixing the broken Vite logo (fix now merged upstream!) |
| 29 | + |
| 30 | +When production building the app for GitHub Pages, you'll notice the that the Vite logo ends up broken. |
| 31 | + |
| 32 | +[I'm not the only one to have noticed](https://github.com/vitejs/vite/issues/10601). [A fix was suggested here](https://github.com/vitejs/vite/issues/7358), which I've implemented in this project. |
| 33 | + |
| 34 | +This occurs because the `img` `src` directly refers to the build-generated `/vite.svg` and React does not preprocess `src` attributes to add the configured `base`. |
| 35 | +The fix is to import `/vite.svg` just like any other resource in React: |
| 36 | + |
| 37 | +```ts |
| 38 | +import viteLogo from '/vite.svg' |
| 39 | +// ... |
| 40 | +<img src={viteLogo} className="logo" alt="Vite logo" /> |
| 41 | +``` |
| 42 | + |
| 43 | +To make things a bit more tricky, this issues does not show up when running `npm run dev` -- only when running `npm run build` (followed by `npm run preview` for testing locally): |
| 44 | + |
| 45 | +```shell |
| 46 | +# This will replicate the issue |
| 47 | +npm run build -- --base /test |
| 48 | +npm run preview -- --base /test |
| 49 | + |
| 50 | +# This will NOT replicate the issue |
| 51 | +npm run dev -- --base /test |
| 52 | +``` |
| 53 | + |
| 54 | +[I've raised an MR to fix this](https://github.com/vitejs/vite/pull/12374) for anyone generating projects in the future. **Update 2023-03-11: Now merged!** |
0 commit comments