-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use @sveltejs/adapter-static
on GitHub Pages
#5845
Changes from all commits
5f85f56
1e998b8
dda286b
afb15ea
a5a3986
beefcb2
d0e91ca
2bb0550
ca7a09d
bae8090
94d6594
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@sveltejs/adapter-auto': patch | ||
'@sveltejs/adapter-static': patch | ||
--- | ||
|
||
Use adapter-static with GitHub Pages |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ You can deploy to the following platforms with the default adapter, `adapter-aut | |
- [Cloudflare Pages](https://developers.cloudflare.com/pages/) via [`adapter-cloudflare`](https://github.com/sveltejs/kit/tree/master/packages/adapter-cloudflare) | ||
- [Netlify](https://netlify.com) via [`adapter-netlify`](https://github.com/sveltejs/kit/tree/master/packages/adapter-netlify) | ||
- [Vercel](https://vercel.com) via [`adapter-vercel`](https://github.com/sveltejs/kit/tree/master/packages/adapter-vercel) | ||
- [GitHub Pages](https://pages.github.com) via [`adapter-static`](https://github.com/sveltejs/kit/tree/master/packages/adapter-static) | ||
|
||
#### Node.js | ||
|
||
|
@@ -56,6 +57,67 @@ You can also use `adapter-static` to generate single-page apps (SPAs) by specify | |
|
||
> You must ensure [`trailingSlash`](configuration#trailingslash) is set appropriately for your environment. If your host does not render `/a.html` upon receiving a request for `/a` then you will need to set `trailingSlash: 'always'` to create `/a/index.html` instead. | ||
|
||
#### GitHub Pages | ||
|
||
SvelteKit can be automatically configured for deployment on GitHub Pages by using the [`actions/configure-pages`](https://github.com/actions/configure-pages) GitHub Action before building it. This action will set `GITHUB_PAGES=true` and replace `kit.paths.base` in `svelte.config.js` by the correct path for your GitHub repository. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the automatic // svelte.config.js
export default {
kit: {
adapter: adapter(),
paths: { base: process.env.GITHUB_PAGES_PATH ?? '' }
}
}; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be clear, this is an optional feature of actions/configure-pages enabled with this setting. I think the reason they decided to go with config replacement is so that their starter-workflows would require zero config changes to work. I'm not entirely sure though... In conversation with them about environment variables, they were also against providing environment variables for all the necessary site info (pages path, site origin, etc.). That's not to say the info isn't available. In a workflow, you could manually forward the data from their workflow step as outputs into environment variables in the build step. In the We can definitely wait on the automatic replacement workflow until after we propose a starter workflow and see what they say. |
||
|
||
To use it, create a new [workflow](https://docs.github.com/en/actions/using-workflows/about-workflows) file at `.github/workflows/github-pages.yaml`. You can start with GitHub's [_Node.js CI_ template](https://github.com/actions/starter-workflows/blob/main/ci/node.js.yml), use the template provided here or make your own. | ||
|
||
If you want to make your own workflow, it needs to perform these steps: | ||
|
||
1. Checkout the repository with [`actions/checkout`](https://github.com/actions/checkout) | ||
2. Setup Node.JS with [`actions/setup-node`](https://github.com/actions/setup-node) | ||
3. Install dependencies with `npm i`, `pnpm i` or `yarn` | ||
4. Setup SvelteKit for GitHub Pages with [`actions/configure-pages`](https://github.com/actions/configure-pages) | ||
5. Build the website with `npm run build`, `pnpm run build` or `yarn run build` | ||
6. Upload the built website with [`actions/upload-pages-artifact`](https://github.com/actions/upload-pages-artifact) | ||
7. Deploy the uploaded website with [`actions/deploy-pages`](https://github.com/actions/deploy-pages) | ||
|
||
Here's a complete template using [`pnpm`](https://pnpm.io). | ||
|
||
```yaml | ||
name: GitHub Pages | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
strategy: | ||
matrix: | ||
node-version: [latest] | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- uses: pnpm/action-setup@v2 | ||
with: | ||
version: latest | ||
|
||
- name: Use Node.js ${{ matrix.node-version }} | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
cache: pnpm | ||
|
||
- name: Install dependencies | ||
run: pnpm install | ||
|
||
- uses: actions/configure-pages@v1 | ||
|
||
- name: Build | ||
run: pnpm run build | ||
|
||
- uses: actions/upload-pages-artifact@v1 | ||
with: | ||
path: build | ||
|
||
- uses: actions/deploy-pages@v1 | ||
``` | ||
|
||
#### Platform-specific context | ||
|
||
Some adapters may have access to additional information about the request. For example, Cloudflare Workers can access an `env` object containing KV namespaces etc. This can be passed to the `RequestEvent` used in [hooks](/docs/hooks) and [server routes](/docs/routing#server) as the `platform` property — consult each adapter's documentation to learn more. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,5 +78,13 @@ export const platforms = [ | |
}) | ||
); | ||
} | ||
}, | ||
{ | ||
name: 'GitHub Pages', | ||
test: () => !!process.env.GITHUB_PAGES, | ||
defaults: () => ({ | ||
fallback: '404.html' | ||
}), | ||
done: (builder) => fs.writeFileSync(`${builder.config.kit.outDir}/.nojekyll`, '') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From what I can tell, the new custom GitHub Actions Pages deployments don't need a |
||
} | ||
]; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this section should really go in the adapter-static README (there's already a GitHub Pages section). maybe it should be an
npm
example instead ofpnpm
? many more people use it. or we could have something like