-
Notifications
You must be signed in to change notification settings - Fork 26.2k
feat(service-worker): add option to config when to register sw #21842
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
Looks like the build failed with: |
Would going down a path similar to how preloading lazy loaded routes works be better? I.e. define a swRegistrationStrategy parameter that then has a couple of Angular provided defaults (like |
@Splaktar , thank you for review, I will update the PR and let you review again. |
@Splaktar , I have updated the PR, change the option to
I also update the sample at DevIntent/angular-example#1, please review. |
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.
I like this direction that this is heading 😄
navigator.serviceWorker.register(script, {scope: options.scope}); | ||
} else if (registrationStrategy.indexOf('registerDelay') !== -1) { | ||
const split = registrationStrategy.split(':'); | ||
const delay = split.length > 1 ? split[1] : 0; |
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.
Should the default delay be non-zero? Perhaps 5 or 10s? It would be better to give an intelligent default rather than falling back to just be the same as registerImmediately
.
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.
the zero
fallback is a little different with registerImmediately
.
registerImmediately
means no delay at all.zero
meanssetTimeout(register, 0)
, it will still wait allmicroTasks
to finish.
So I think maybe zero
is acceptable. What do you think?
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.
Interesting, can you please specify those details about this default delay in the docs?
@@ -66,6 +66,31 @@ Add `ServiceWorkerModule` to the `@NgModule` `imports` array. Use the `register( | |||
|
|||
<code-example path="service-worker-getting-started/src/app/app.module.ts" linenums="false" title="src/app/app.module.ts" region="sw-module"> </code-example> | |||
|
|||
You can pass some options to `register()` method. |
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.
You can pass some options to the register()
method.
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.
Got it, I will remove the ` mark.
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.
Sorry, the ` is fine, I just added a "the".
return someService.initData(); // return an Observable, when data is available, we can register service worker. | ||
)}; | ||
</code-pane> | ||
</code-tabs> |
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 formatting doesn't seem to be rendering properly in GitHub Markdown.
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.
The <code-tabs>
and <code-pane>
will be parsed by dgeni
, I have tested locally, this part can be rendered correctly.
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.
I know that dgeni will code parse comments for addition to docs, but it also parses markdown?
You can pass some options to `register()` method. | ||
- enabled: optional parameter, by default is true, if enabled is false, the module will behave like the browser not support service worker, and service worker will not be registered. | ||
- scope: optional parameter, to specify the subset of your content that you want the service worker to control. | ||
- registrationStrategy: optional parameter, specify the strategy that when to register service worker, the available options are: |
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.
specify a strategy that determines when to register the service worker
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.
got it.
- enabled: optional parameter, by default is true, if enabled is false, the module will behave like the browser not support service worker, and service worker will not be registered. | ||
- scope: optional parameter, to specify the subset of your content that you want the service worker to control. | ||
- registrationStrategy: optional parameter, specify the strategy that when to register service worker, the available options are: | ||
- registerWhenStable: this is the default behavior, service worker will register when Application is stable (no microTasks or macroTasks remain). |
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.
the service worker will register when the application is stable (no microTasks or macroTasks remain).
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.
got it, change Application
to application
.
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.
Added two "the"s in there too
- scope: optional parameter, to specify the subset of your content that you want the service worker to control. | ||
- registrationStrategy: optional parameter, specify the strategy that when to register service worker, the available options are: | ||
- registerWhenStable: this is the default behavior, service worker will register when Application is stable (no microTasks or macroTasks remain). | ||
- registerImmediately: register immediately without waiting application to become stable. |
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.
without waiting for the application to become stable
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.
got it, add the
before application.
- registrationStrategy: optional parameter, specify the strategy that when to register service worker, the available options are: | ||
- registerWhenStable: this is the default behavior, service worker will register when Application is stable (no microTasks or macroTasks remain). | ||
- registerImmediately: register immediately without waiting application to become stable. | ||
- registerDelay:timeout : register after timeout, timeout is a number to specify the delay timeout, for example `registerDelay:5000` means register after 5 seconds. |
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.
registerDelay:timeout
: register after the timeout period. timeout
is the number of milliseconds to delay registration. For example: registerDelay:5000
would register the service worker after 5 seconds.
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.
got it.
- registerWhenStable: this is the default behavior, service worker will register when Application is stable (no microTasks or macroTasks remain). | ||
- registerImmediately: register immediately without waiting application to become stable. | ||
- registerDelay:timeout : register after timeout, timeout is a number to specify the delay timeout, for example `registerDelay:5000` means register after 5 seconds. | ||
- a factory to get Observable : you can also specify a factory which return an Observable, service worker will register when the Observable emit for the first time. |
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.
A factory to get an Observable: You can also specify a factory which returns an Observable. The service worker will be registered the first time that a value is emitted by the Observable.
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.
got it.
@alxhub we would love some feedback on this approach before more effort is spent on this. |
@Splaktar , thank you for the detailed review, I have updated the PR, and about the |
Hi @JiaLiPassion! This PR has merge conflicts due to recent upstream merges. |
1 similar comment
Hi @JiaLiPassion! This PR has merge conflicts due to recent upstream merges. |
@JiaLiPassion can you please rebase this? |
@Splaktar, sure, will do. |
It looks like the last commit broke the build due to TypeScript 3.1.x changes. |
Got it, I will fix it today. |
@Splaktar, I have updated the source, please review , thanks. |
LGTM! CI Tests now pass. Thank you. |
So there's good news and bad news. 👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there. 😕 The bad news is that it appears that one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request. Note to project maintainer: This is a terminal state, meaning the |
|
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. |
…untime config (#21842) 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. PR Close #21842
…tionOptions` (angular#21842) This is in preparation of making `RegistrationOptions` part of the public API (in a subsequent commit). PR Close angular#21842
…untime config (angular#21842) 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. PR Close angular#21842
…o separate file (angular#21842) PR Close angular#21842
Relates to #1. Relates to angular/angular#21842.
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: #20970
Some times, angular will start up some long
macroTask
such as a long delaysetTimeout
(in #20970,firebase auth
will start a200s
timeout to do token refresh). songZone
will not become stable and will not triggerservice-worker
to do registration.What is the new behavior?
I think there are so many cases that when startup, there will be
macroTask
not complete or cost a lot of time, so in this PR, my idea is add acallback
to let user to be able to choose when toregister
.I added a property in
RegistrationOptions
, the sample is here.DevIntent/angular-example#1
and
getServiceWorkerInitFactory
will return a promise, user can resolve it by their choice.the sample code is here,
Does this PR introduce a breaking change?
Other information
if the idea is ok, I will update the document.
This PR incorporates #26002.