Skip to content

Commit e5c9383

Browse files
Initial commit
0 parents  commit e5c9383

File tree

18 files changed

+1291
-0
lines changed

18 files changed

+1291
-0
lines changed

.github/dependabot.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Enable automatic PRs for version updates:
2+
# https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
3+
version: 2
4+
updates:
5+
- package-ecosystem: "github-actions"
6+
directory: "/"
7+
schedule:
8+
interval: "weekly"

.github/workflows/publish.yaml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Based on: https://vitejs.dev/guide/static-deploy.html
2+
name: Publish to GitHub Pages
3+
on:
4+
push:
5+
branches:
6+
- main
7+
workflow_dispatch: # Allow manual invocations of this workflow
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout repo
14+
uses: actions/checkout@v4
15+
- name: Setup NodeJS
16+
uses: actions/setup-node@v4
17+
with:
18+
node-version: 18
19+
cache: npm
20+
- run: npm install
21+
- name: Build app with GitHub Pages base
22+
run: npm run build -- --base /${{ github.event.repository.name }}
23+
- name: Configure Pages (if this fails, set Pages source to GitHub Actions in repo settings)
24+
uses: actions/configure-pages@v5
25+
- name: Upload pages artifact
26+
uses: actions/upload-pages-artifact@v3
27+
with:
28+
path: ./dist
29+
deploy:
30+
needs: build
31+
32+
# Grant GITHUB_TOKEN the permissions required to make a Pages deployment
33+
permissions:
34+
pages: write # To deploy to Pages
35+
id-token: write # To verify the deployment originates from an appropriate source
36+
37+
# Deploy to the GitHub pages environment
38+
environment:
39+
name: github-pages
40+
url: ${{ steps.deployment.outputs.page_url }}
41+
42+
# Allow one concurrent deployment
43+
concurrency:
44+
group: ${{ github.workflow }}-deploy
45+
46+
runs-on: ubuntu-latest
47+
steps:
48+
- name: Deploy pages artifact
49+
uses: actions/deploy-pages@v4
50+
id: deployment

.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?

LICENCE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Alex Palmer
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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!**

index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite + React + TS</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.tsx"></script>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)