Skip to content

Conversation

sonukapoor
Copy link
Contributor

Previously, there was a chance that the SW will never register
when there is a long-running task or recurring timeout. This commit
fixes this, by first trying to wait for the app to stabilize, however,
if that does not happen, then the SW will register after 30 seconds.

Closes #34464

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • 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?

Issue Number: #34464

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@pullapprove pullapprove bot requested a review from alxhub March 5, 2020 10:48
@sonukapoor
Copy link
Contributor Author

@alxhub / @gkalpak My local lint and tests are all passing - not sure why CI is complaining. Can you help with this?

@sonukapoor sonukapoor force-pushed the sw-stable-with-delay branch from 21e583d to 0749d35 Compare March 5, 2020 12:19
@gkalpak gkalpak changed the title feat(docs-infra): change default SW strategy behavior feat(service-worker): change default SW strategy behavior Mar 5, 2020
@gkalpak gkalpak requested review from gkalpak and removed request for alxhub March 5, 2020 13:22
@gkalpak gkalpak added area: service-worker Issues related to the @angular/service-worker package action: review The PR is still awaiting reviews from at least one requested reviewer feature Issue that requests a new feature labels Mar 5, 2020
@ngbot ngbot bot modified the milestone: needsTriage Mar 5, 2020
@mary-poppins
Copy link

You can preview 21e583d at https://pr35870-21e583d.ngbuilds.io/.
You can preview 0749d35 at https://pr35870-0749d35.ngbuilds.io/.

@gkalpak gkalpak added the target: major This PR is targeted for the next major release label Mar 5, 2020
@gkalpak
Copy link
Member

gkalpak commented Mar 5, 2020

Thx, @sonukapoor, for taking this on. Can you please remove the formatting changes? They make the PR hard to review and the formatter is unhappy (you are probably running the wrong commands locally if lint passes).

Also, how are you running the tests locally? The failures on CI seem expected.

@sonukapoor
Copy link
Contributor Author

sonukapoor commented Mar 5, 2020

@gkalpak. I am running yarn lint and yarn test in the aio folder. When I try to run similar commands within root then I get all kind of strange errors from bazel

Copy link
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.

Here are some more comments from a preliminary high-level review.

@gkalpak
Copy link
Member

gkalpak commented Mar 5, 2020

@gkalpak. I am running yarn lint and yarn test in the aio folder. When I try to run similar commands within root then I get all kind of strange errors from bazel

These changes are outside the aio/ directory, so you need to run commands at the root of the repo 😁
Can you open a separate issue with the errors you are getting from bazel?

@sonukapoor
Copy link
Contributor Author

Can you open a separate issue with the errors you are getting from bazel?

@gkalpak, Please see here: #35891

@sonukapoor sonukapoor force-pushed the sw-stable-with-delay branch from 0749d35 to 5d6531f Compare March 6, 2020 03:18
@mary-poppins
Copy link

You can preview 5d6531f at https://pr35870-5d6531f.ngbuilds.io/.

@sonukapoor sonukapoor force-pushed the sw-stable-with-delay branch 2 times, most recently from 6be81b0 to 4b8b57a Compare March 6, 2020 03:42
@mary-poppins
Copy link

You can preview 4b8b57a at https://pr35870-4b8b57a.ngbuilds.io/.

@sonukapoor sonukapoor requested a review from gkalpak March 6, 2020 12:12
@gkalpak gkalpak added state: WIP and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Mar 6, 2020
@gkalpak gkalpak force-pushed the sw-stable-with-delay branch from afcdf51 to 50115ca Compare March 24, 2020 20:52
@googlebot

This comment has been minimized.

@gkalpak gkalpak added action: review The PR is still awaiting reviews from at least one requested reviewer and removed state: WIP labels Mar 24, 2020
@gkalpak

This comment has been minimized.

@googlebot

This comment has been minimized.

sonukapoor and others added 5 commits March 24, 2020 22:54
…stration strategy

Previously, when using the `registerWhenStable` ServiceWorker
registration strategy (which is also the default) Angular would wait
indefinitely for the [app to stabilize][1], before registering the
ServiceWorker script. This could lead to a situation where the
ServiceWorker would never be registered when there was a long-running
task (such as an interval or recurring timeout).

Such tasks can often be started by a 3rd-party dependency (beyond the
developer's control or even without them realizing). In addition, this
situation is particularly hard to detect, because the ServiceWorker is
typically not used during development and on production builds a
previous ServiceWorker instance might be already active.

This commit enhances the `registerWhenStable` registration strategy by
adding support for an optional `<timeout>` argument, which guarantees
that the ServiceWorker will be registered when the timeout expires, even
if the app has not stabilized yet.

For example, with `registerWhenStable:5000` the ServiceWorker will be
registered as soon as the app stabilizes or after 5 seconds if the app
has not stabilized by then.

Related to angular#34464.

[1]: https://angular.io/api/core/ApplicationRef#is-stable-examples
…p never stabilizes

Previously, when using the default ServiceWorker registration strategy
Angular would wait indefinitely for the [app to stabilize][1], before
registering the ServiceWorker script. This could lead to a situation
where the ServiceWorker would never be registered when there was a
long-running task (such as an interval or recurring timeout).

Such tasks can often be started by a 3rd-party dependency (beyond the
developer's control or even without them realizing). In addition, this
situation is particularly hard to detect, because the ServiceWorker is
typically not used during development and on production builds a
previous ServiceWorker instance might be already active.

This commit fixes this by changing the default registration strategy
from `registerWhenStable` to `registerWhenStable:30000`, which will
ensure that the ServiceWorker will be registered after 30s at the
latest, even if the app has not stabilized by then.

Fixes angular#34464
…g app stabilization

Previously, some of the built-in ServiceWorker registration strategies,
namely `registerWithDelay:<timeout>` and `registerWhenStable:<timeout>`,
would register potentially long-running timeout, thus preventing the app
from stabilizing before the timeouts expired. This was especially
problematic for the `registerWhenStable:<timeout>` strategy, which waits
for the app to stabilize, because the strategy itself would prevent the
app from stabilizing and thus the ServiceWorker would always be
registered after the timeout.

This commit fixes this by subscribing to the registration strategy
observable outside the Angular zone, thus not affecting the app's
stabilization.
@gkalpak gkalpak force-pushed the sw-stable-with-delay branch from 50115ca to 5bf2fe2 Compare March 24, 2020 20:55
@gkalpak
Copy link
Member

gkalpak commented Mar 24, 2020

Rebased on master, updated commit messages and fixed up commits. Let's see what CI thinks 😃
@sonukapoor, please take a look and make sure I didn't miss anything 🙏

@sonukapoor
Copy link
Contributor Author

Rebased on master, updated commit messages and fixed up commits. Let's see what CI thinks 😃
@sonukapoor, please take a look and make sure I didn't miss anything 🙏

Looks good to me. Thanks for the updates - very much appreciated.

@mary-poppins
Copy link

You can preview 5bf2fe2 at https://pr35870-5bf2fe2.ngbuilds.io/.

@gkalpak gkalpak force-pushed the sw-stable-with-delay branch from 5bf2fe2 to 22bf4cf Compare March 24, 2020 21:25
Copy link
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, @sonukapoor

@gkalpak gkalpak 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 Mar 24, 2020
@mary-poppins
Copy link

You can preview 22bf4cf at https://pr35870-22bf4cf.ngbuilds.io/.

@alxhub alxhub closed this in 00efacf Mar 27, 2020
alxhub pushed a commit that referenced this pull request Mar 27, 2020
…p never stabilizes (#35870)

Previously, when using the default ServiceWorker registration strategy
Angular would wait indefinitely for the [app to stabilize][1], before
registering the ServiceWorker script. This could lead to a situation
where the ServiceWorker would never be registered when there was a
long-running task (such as an interval or recurring timeout).

Such tasks can often be started by a 3rd-party dependency (beyond the
developer's control or even without them realizing). In addition, this
situation is particularly hard to detect, because the ServiceWorker is
typically not used during development and on production builds a
previous ServiceWorker instance might be already active.

This commit fixes this by changing the default registration strategy
from `registerWhenStable` to `registerWhenStable:30000`, which will
ensure that the ServiceWorker will be registered after 30s at the
latest, even if the app has not stabilized by then.

Fixes #34464

PR Close #35870
alxhub pushed a commit that referenced this pull request Mar 27, 2020
…g app stabilization (#35870)

Previously, some of the built-in ServiceWorker registration strategies,
namely `registerWithDelay:<timeout>` and `registerWhenStable:<timeout>`,
would register potentially long-running timeout, thus preventing the app
from stabilizing before the timeouts expired. This was especially
problematic for the `registerWhenStable:<timeout>` strategy, which waits
for the app to stabilize, because the strategy itself would prevent the
app from stabilizing and thus the ServiceWorker would always be
registered after the timeout.

This commit fixes this by subscribing to the registration strategy
observable outside the Angular zone, thus not affecting the app's
stabilization.

PR Close #35870
@angular-automatic-lock-bot
Copy link

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 Apr 27, 2020
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 feature Issue that requests a new feature target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat(service-worker): improve default SwRegistrationStrategy
4 participants