expose base via $service-worker, make paths relative - #9250
Merged
Conversation
5 tasks
dummdidumm
reviewed
Mar 6, 2023
| fs.writeFileSync( | ||
| service_worker, | ||
| ` | ||
| export const base = /*@__PURE__*/ location.pathname.split('/').slice(0, -1).join('/'); |
Member
There was a problem hiding this comment.
Having a hard time understanding how/why this works. If you you visit example.com/foo/bar wouldn't the base be /foo, even if I didn't set a base path?
Member
Author
There was a problem hiding this comment.
In a service worker, location is the location of the service worker, not the URL you're currently visiting. Because SvelteKit guarantees that the service worker is at the root of the app (because a service worker can only handle requests at the same level or below), location.pathname.split('/').slice(0, -1).join('/') is guaranteed to be the base path
Member
There was a problem hiding this comment.
this might be worth a comment?
benmccann
reviewed
Mar 6, 2023
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
5 tasks
dummdidumm
approved these changes
Mar 8, 2023
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
pathsto$service-workermodule #4714This adds a
baseexport to$service-worker:Rather than being hardcoded from the
paths.baseinsvelte.config.js, this is computed when the service worker boots up. This means that if the app is deployed somewhere where we don't know the base path (IPFS being the classic example), it will continue to work.It also modifies the existing
build,filesandprerenderedexports to usebaseinstead of having the hardcoded prefix.Note that there's no
assetshere, because you can't use a service worker alongsideassets(since a service worker can only control requests for the same domain/path prefix, i.e.https://cdn.example.com/my-app/service-worker.jscan't handle requests for your app).Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.