Skip to content

feat(service-worker): expose RegistrationOptions token to allow for runtime configuration#26002

Closed
deebloo wants to merge 4 commits intoangular:masterfrom
deebloo:expose-sw-registration-options
Closed

feat(service-worker): expose RegistrationOptions token to allow for runtime configuration#26002
deebloo wants to merge 4 commits intoangular:masterfrom
deebloo:expose-sw-registration-options

Conversation

@deebloo
Copy link
Copy Markdown
Contributor

@deebloo deebloo commented Sep 18, 2018

Expose the RegistrationOptions DI token to allow for runtime configuration of ServiceWorkerModule. At the moment there is no way to mark ServiceWorkerModule as enabled at runtime since the configuration values are set at build time.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] angular.io application / infrastructure changes
[ ] Other... Please describe:

What is the current behavior?

At the moment there is no way to mark the sw module as enabled at runtime. For example if you use global variables for environment variables.

@NgModule({
  imports: [
    // this does not work since the configuration happens at compile time.
    ServiceWorkerModule.register('/sw/script.js', { enabled: window['isProdMode'] })
  ]
])
class AppModule {}

Issue Number: #22796

What is the new behavior?

Exposing this token will allow folks to configure the sw with useFactory making runtime config possible.

export function swConfigFactory() {
  return { enabled: window['isProdMode'] }
}

@NgModule({
  imports: [ServiceWorkerModule.register('/sw/script.js')],
  providers: [
    { provide: RegistrationOptions, useFactory: swConfigFactory }
  ]
])
class AppModule {}

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

@gkalpak
Copy link
Copy Markdown
Member

gkalpak commented Sep 18, 2018

Related to #22796.

@gkalpak gkalpak added feature Label used to distinguish feature request from other issues action: review The PR is still awaiting reviews from at least one requested reviewer area: service-worker Issues related to the @angular/service-worker package PR target: TBD and removed feature Label used to distinguish feature request from other issues labels Sep 18, 2018
@deebloo
Copy link
Copy Markdown
Contributor Author

deebloo commented Sep 18, 2018

must not have a good job looking for an issue. I noticed that the test is failing because of public API restrictions. I was wondering the correct way going about updating those type def files. whether by hand or some tool?

@gkalpak
Copy link
Copy Markdown
Member

gkalpak commented Sep 19, 2018

There is bazel command you can run to update the "golden files". It is mentioned in the CI logs:

bazel run //tools/public_api_guard:service-worker_service-worker_api_bin.accept

(But for small changes it is usually easier to manually update the corresponding file, service-worker.d.ts in this case.)

Copy link
Copy Markdown
Member

@gkalpak gkalpak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the motivation for exporting this? Can you add it to the commit message (and PR description)?

If we are going to do it, then we at least need some docs 😉

@deebloo
Copy link
Copy Markdown
Contributor Author

deebloo commented Sep 19, 2018

@gkalpak i updated the PR and the public_api_guard for service-worker. Let me know if anything needs to be clarified

@gkalpak
Copy link
Copy Markdown
Member

gkalpak commented Sep 19, 2018

#26002 (review)

@deebloo deebloo changed the title feat(service-worker): expose RegistrationOptions token feat(service-worker): expose RegistrationOptions token to allow for runtime configuration Sep 19, 2018
@deebloo
Copy link
Copy Markdown
Contributor Author

deebloo commented Sep 20, 2018

@gkalpak I believe I have everything in order. If not then I am missing the point and need more specific direction to penetrate my thick skull 😆

@gkalpak
Copy link
Copy Markdown
Member

gkalpak commented Sep 21, 2018

@deebloo, can you give an example of a usecase that was not possible before and is possible with this change?

@deebloo
Copy link
Copy Markdown
Contributor Author

deebloo commented Sep 21, 2018

yes. ill update the PR to show what could NOT be done before

@deebloo
Copy link
Copy Markdown
Contributor Author

deebloo commented Sep 21, 2018

done. updated with current behavior and better example for new behavior

@mary-poppins
Copy link
Copy Markdown

You can preview 8632c5d at https://pr26002-8632c5d.ngbuilds.io/.
You can preview 15e19bf at https://pr26002-15e19bf.ngbuilds.io/.
You can preview e6e12d6 at https://pr26002-e6e12d6.ngbuilds.io/.

Copy link
Copy Markdown
Member

@gkalpak gkalpak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx for the example. What we need:

  • Squash the commits into one (that follows our guidelines).
  • Add some API docs for RegistrationOptions (see #23138 for example).

It would be rad (but not necessary for this PR) if you wanted to add an example that shows how to use this feature (and has tests to ensure it doesn't break in the future). Let me know if you are interested 😉

@gkalpak gkalpak added the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Sep 24, 2018
@deebloo deebloo force-pushed the expose-sw-registration-options branch from e6e12d6 to 3308537 Compare September 24, 2018 13:18
@mary-poppins
Copy link
Copy Markdown

You can preview 3308537 at https://pr26002-3308537.ngbuilds.io/.

@deebloo deebloo force-pushed the expose-sw-registration-options branch from 3308537 to a03f9e4 Compare September 25, 2018 02:38
@deebloo
Copy link
Copy Markdown
Contributor Author

deebloo commented Sep 25, 2018

@gkalpak id be happy to add

@mary-poppins
Copy link
Copy Markdown

You can preview a03f9e4 at https://pr26002-a03f9e4.ngbuilds.io/.

@deebloo deebloo force-pushed the expose-sw-registration-options branch from a03f9e4 to 40ab248 Compare September 25, 2018 16:16
@deebloo
Copy link
Copy Markdown
Contributor Author

deebloo commented Sep 25, 2018

@gkalpak I updated the unit tests but not really sure the best way to add an example. Let me know if you think this is good enough

@mary-poppins
Copy link
Copy Markdown

You can preview 40ab248 at https://pr26002-40ab248.ngbuilds.io/.

@deebloo deebloo force-pushed the expose-sw-registration-options branch from 40ab248 to 89c5338 Compare September 25, 2018 18:31
deebloo and others added 3 commits March 27, 2019 15:06
…untime config

Previously, the ServiceWorker registration options should be defined as
an object literal (in order for them to be compatible with Ahead-of-Time
compilation), thus making it impossible to base the ServiceWorker
behavior on runtime conditions.
This commit allows specifying the registration options using a regular
provider, which means that it can take advantage of the `useFactory`
option to determine the config at runtime, while still remaining
compatible with AoT compilation.
@gkalpak gkalpak force-pushed the expose-sw-registration-options branch from e217161 to 1f8c38d Compare March 27, 2019 14:08
@deebloo deebloo requested a review from a team March 27, 2019 14:08
@deebloo
Copy link
Copy Markdown
Contributor Author

deebloo commented Mar 27, 2019

oh cool thanks @gkalpak ill keep and eye on the PR so I can see what is different

@gkalpak
Copy link
Copy Markdown
Member

gkalpak commented Mar 27, 2019

@deebloo, I think it's ready. Please, take a look and let me know if you spot anything dubious 😃

More or less here is what I have done:

@mary-poppins
Copy link
Copy Markdown

You can preview 1f8c38d at https://pr26002-1f8c38d.ngbuilds.io/.

@gkalpak gkalpak requested a review from IgorMinar March 27, 2019 14:22
@deebloo
Copy link
Copy Markdown
Contributor Author

deebloo commented Mar 27, 2019

@gkalpak everything looks like it makes sense. I appreciate all the help getting this done.

@gkalpak
Copy link
Copy Markdown
Member

gkalpak commented Mar 27, 2019

@IgorMinar, PTAL.

@glebmachine
Copy link
Copy Markdown

Please, publish

It's very important due to #26500 bug!
Service worker in Safari is totally broken

Copy link
Copy Markdown
Contributor

@IgorMinar IgorMinar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm! thanks for getting this into a mergeable state and iterating on the PR

@gkalpak thanks for helping out! ❤️

@googlebot
Copy link
Copy Markdown

A Googler has manually verified that the CLAs look good.

(Googler, please make sure the reason for overriding the CLA status is clearly documented in these comments.)

ℹ️ Googlers: Go here for more info.

@IgorMinar IgorMinar added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Apr 25, 2019
@IgorMinar IgorMinar modified the milestones: needsTriage, version 8 Apr 25, 2019
@gkalpak
Copy link
Copy Markdown
Member

gkalpak commented Apr 25, 2019

Thx for working in this, @deebloo ❤️
I am going to close this and include the commit in #21842 (per #21842 (review)).

@gkalpak gkalpak closed this Apr 25, 2019
@deebloo
Copy link
Copy Markdown
Contributor Author

deebloo commented Apr 26, 2019

Of course! I do really appreciate all the assistance.

@znikola
Copy link
Copy Markdown

znikola commented May 14, 2019

Hi, is this going to be in Angular 7 as well?

@Splaktar
Copy link
Copy Markdown
Contributor

@znikola I don't believe so.

@angular-automatic-lock-bot
Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: service-worker Issues related to the @angular/service-worker package cla: yes risk: low target: major This PR is targeted for the next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants