Skip to content
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

Merged
merged 5 commits into from
Mar 8, 2023
Merged

Conversation

Rich-Harris
Copy link
Member

This adds a base export to $service-worker:

import { base } from '$service-worker';

Rather than being hardcoded from the paths.base in svelte.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 and prerendered exports to use base instead of having the hardcoded prefix.

Note that there's no assets here, because you can't use a service worker alongside assets (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:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

@@ -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('/');
Copy link
Member

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?

Copy link
Member Author

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

Copy link
Member

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!

Copy link
Member

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 <322311+benmccann@users.noreply.github.com>
@dummdidumm dummdidumm merged commit 78b4a1b into master Mar 8, 2023
@dummdidumm dummdidumm deleted the gh-4714 branch March 8, 2023 13:27
@github-actions github-actions bot mentioned this pull request Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose paths to $service-worker module
3 participants