-
-
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
Conversation
🦋 Changeset detectedLatest commit: 94d6594 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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 is great, thanks — comment in line, I think we can probably make the setup more self-explanatory
documentation/docs/11-adapters.md
Outdated
SvelteKit can be automatically configured for deployment on GitHub Pages by using the `actions/configure-pages` GitHub Action before building it. | ||
|
||
```yaml | ||
- 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 | ||
``` |
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 could possibly use some more context — where do I put this code? Should we maybe have an example of a setting up a complete workflow, with instructions about where to write the .yml
file etc?
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.
Hi! Sorry it took this long, was busy at work. I added more information about GitHub Workflows.
GitHub Pages
SvelteKit can be automatically configured for deployment on GitHub Pages by using the
actions/configure-pages
GitHub Action before building it. This action will setGITHUB_PAGES=true
and replacekit.paths.base
insvelte.config.js
by the correct path for your GitHub repository.To use it, create a new workflow file at
.github/workflows/github-pages.yaml
. You can start with GitHub's Node.js CI template, use the template provided here or make your own.If you want to make your own workflow, it needs to perform these steps:
- Checkout the repository with
actions/checkout
- Setup Node.JS with
actions/setup-node
- Install dependencies with
npm i
,pnpm i
oryarn
- Setup SvelteKit for GitHub Pages with
actions/configure-pages
- Build the website with
npm run build
,pnpm run build
oryarn run build
- Upload the built website with
actions/upload-pages-artifact
- Deploy the uploaded website with
actions/deploy-pages
Here's a complete template using
pnpm
.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
✅ Deploy Preview for kit-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
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.
thank you! comments inline
@@ -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 |
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 of pnpm
? many more people use it. or we could have something like
packages/adapter-static/examples/github-pages-npm/build.yaml
packages/adapter-static/examples/github-pages-pnpm/build.yaml
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
the automatic kit.paths.base
replacement makes me super nervous. hacks like this are guaranteed to break for a percentage of users, and it's hard to test locally. it should really be an environment variable that you add to your config — does something like this exist?
// 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 comment
The 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 adapter-static
README, I think we could provide two workflows, with one using automatic replacement (warning users of potential issues) and the other using our own environment variables. The reason for this is that I don't think the automatic replacement version will go away. After this is merged, we hope to add a SvelteKit starter-workflow, which they may require to be using replacement (for zero config purposes, but I'm not sure yet).
We can definitely wait on the automatic replacement workflow until after we propose a starter workflow and see what they say.
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 comment
The 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 .nojekyll
file like publishing from a branch does. So, I think this can be removed since we're going with the new deployment method.
@@ -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 comment
The 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 adapter-static
README, I think we could provide two workflows, with one using automatic replacement (warning users of potential issues) and the other using our own environment variables. The reason for this is that I don't think the automatic replacement version will go away. After this is merged, we hope to add a SvelteKit starter-workflow, which they may require to be using replacement (for zero config purposes, but I'm not sure yet).
We can definitely wait on the automatic replacement workflow until after we propose a starter workflow and see what they say.
The more I wrap the head round this, the more I think we shouldn't do it. The whole point of I would rather we simply add some instructions to the |
Closing in favour of #6945 — thanks all |
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Publishing on GitHub Pages requires writing special checks on environment variables in order to select
adapter-static
.This could be automated by
adapter-auto
. To make this possible,actions/configure-pages
was modified to accommodate SvelteKit.This PR takes advantage of this recent modification to watch for
$GITHUB_PAGES
insideadapter-auto
. By doing this, it becomes possible to publish to GitHub Pages from a GitHub Workflow without having to manually configure SvelteKit.Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. All changesets should bepatch
until SvelteKit 1.0