-
-
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
expose base
via $service-worker
, make paths relative
#9250
Conversation
@@ -35,21 +35,25 @@ export async function build_service_worker( | |||
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]>
paths
to$service-worker
module #4714This adds a
base
export to$service-worker
:Rather than being hardcoded from the
paths.base
insvelte.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
,files
andprerendered
exports to usebase
instead of having the hardcoded prefix.Note that there's no
assets
here, 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.js
can'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 test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.