-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
expose base via $service-worker, make paths relative
#9250
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
Conversation
| fs.writeFileSync( | ||
| service_worker, | ||
| ` | ||
| export const base = /*@__PURE__*/ location.pathname.split('/').slice(0, -1).join('/'); |
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.
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?
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.
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
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.
Thanks for clearing it up!
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 might be worth a comment?
Co-authored-by: Ben McCann <[email protected]>
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:.