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

[feat] allow to disable service worker auto inject #2988

Merged
merged 7 commits into from
Dec 28, 2021

Conversation

SomaticIT
Copy link
Contributor

@SomaticIT SomaticIT commented Dec 6, 2021

This PR allows to disable the automatic injection of the service worker.
This allows developpers to code a custom logic to register service workers.

It is particularly useful when using workbox to manage the service worker.

Workaround

As a workaround, I actually use a script that I run after svelte-kit build to strip the injected service worker from index.html but it only work for SPA builds. In SSR environments, a custom hook may be able to strip it.

But it's not a viable solution since it can break anytime the injected code is changed on the svelte-kit side.

Usage

In your svelte.config.js, you just have to set the following option:

const config = {
  // ... your config
  serviceWorker: {
    inject: false
  }
};

Fixes

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 pnpx changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

Todo

  • Write documentation
  • Add a test case
  • Once validated, add changeset

@changeset-bot
Copy link

changeset-bot bot commented Dec 6, 2021

🦋 Changeset detected

Latest commit: c456bef

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

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

@SomaticIT
Copy link
Contributor Author

I added documentation and test.

The CI fails on windows due to a timing issue (not related with this PR).
Maybe a contributor can rerun the windows checks.

I think this is ready for review.

@benmccann
Copy link
Member

This generally seems reasonable to me and similar to #2281. My main question for the other maintainers would be about what we should call the option name

@SomaticIT
Copy link
Contributor Author

SomaticIT commented Dec 8, 2021

@benmccann, thank you for your reply.

1/ Regarding #2281, you're right, I found this PR after implementing the feature. However, this PR takes a simpler approach: if you want to customize the service worker behavior, just disable automatic injection and do what you want 😁. It means less impact on the kit code (only 3 lines impacted) and less maintenance.

2/ Regarding the option name, I think there are two possibilities:

  • inject: to explain the fact that the code is injected in your page. This was my first choice but I'm open to change
  • register: to explain the behavior of the code which actually register the service worker.

Let met know your preference, I will adapt the PR to your choice.

@SomaticIT
Copy link
Contributor Author

Hello,

I would like to know if it's possible to review this PR?
It is a very simple modification (3 lines of code impacted).

I'm using a custom script to workaround this issue but I'm creating new projects every week and it will become very hard to maintain in the future.

Thank you in advance

@Rich-Harris Rich-Harris merged commit 588aaaf into sveltejs:master Dec 28, 2021
@Rich-Harris
Copy link
Member

Thank you! I renamed the option to register, moved the test app (sw-inject -> options-2, since it serves the same function as options and would have been combined if the options under test weren't incompatible) and added a changeset

@SomaticIT
Copy link
Contributor Author

Thank you for your intervention!
I totally agree with all your changes.

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.

3 participants