Commit Graph

163 Commits

Author SHA1 Message Date
Carlos Ortiz García 9166baf709 refactor(core): Migrate TestBed.get to TestBed.inject (#32382)
This is cleanup/followup for PR #32200

PR Close #32382
2019-09-09 19:10:54 -04:00
George Kalpakas 094538c0ce feat(service-worker): recover from `EXISTING_CLIENTS_ONLY` mode when there is a valid update (#31865)
Previously, when the ServiceWorker entered a degraded mode
(`EXISTING_CLIENTS_ONLY` or `SAFE_MODE`) it would remain in that mode
for the rest of the lifetime of ServiceWorker instance. Note that
ServiceWorkers are stopped by the browser after a certain period of
inactivity and a new instance is created as soon as the ServiceWorker
needs to handle an event (such as a request from the page). Those new
instances would start from the `NORMAL` mode.

The reason for this behavior is to err on the side of caution: If we
can't be sure why the ServiceWorker entered the degraded mode, it is
risky to try recovering on the same instance and might lead to
unexpected behavior.

However, it turns out that the `EXISTING_CLIENTS_ONLY` mode can only be
a result of some error happening with the latest version (e.g. a hash
mismatch in the manifest). Therefore, it is safe to recover from that
mode once a new, valid update is successfully installed and to start
accepting new clients.

This commit ensures that the mode is set back to `NORMAL`, when (a) an
update is successfully installed and (b) the current mode is
`EXISTING_CLIENTS_ONLY`.

Besides making the behavior more predictable (instead of relying on the
browser to decide when to terminate the current ServiceWorker instance
and create a new one), this change can also improve the developer
experience:
When people notice the error during debugging and fix it by deploying a
new version (either to production or locally), it is confusing that the
ServiceWorker will fetch and install the update (as seen by the requests
in the Network panel in DevTools) but not serve it to clients. With this
change, the update will be served to new clients as soon as it is
installed.

Fixes #31109

PR Close #31865
2019-09-04 15:41:04 -07:00
George Kalpakas bda2b4ebb6 fix(service-worker): keep serving clients on older versions if latest is invalidated (#31865)
Previously, when the latest version was invalidated (e.g. due to a hash
mismatch), the SW entered a degraded `EXISTING_CLIENTS_ONLY` mode and
removed _all_ clients from its client-version map (essentially stopping
to serve any clients). Based on the code and surrounding comments, the
intention seems to have been to only remove clients that were on the
invalidated version, but keep other clients on older versions.

This commit fixes it by only unassigning clients what were on the latest
version and keep clients assigned to older versions.

PR Close #31865
2019-09-04 15:41:04 -07:00
George Kalpakas 20dc5e83ee test(service-worker): add helper function for making navigation requests (#31865)
Helper functions for making navigation requests were created in several
places inside the test suite, so this commit creates a top-level such
helper and uses that in all tests that need it.

PR Close #31865
2019-09-04 15:41:04 -07:00
George Kalpakas 24b8b3427c refactor(service-worker): remove redundant argument to `versionFailed()` (#31865)
The `latest` argument was only ever set to the value of comparing
`this.latestHash` with the `appVersion` hash, which is already computed
inside `versionFailed()`, so there is no reason to pass it as an
argument as well.

This doesn't have any impact on the current behavior of the SW.

PR Close #31865
2019-09-04 15:41:04 -07:00
George Kalpakas 65aaffcfe4 docs(service-worker): add example app for `SwPush` API docs (#32139)
Previously, the `SwPush` API docs were using hard-coded code snippets.

This commit switches to using code snippets from an actual example app,
which ensures that the code shown in the docs will at least continue to
compile successfully.

PR Close #32139
2019-08-27 16:18:53 -07:00
Judy Bogart ac11a0be15 docs: add push notification usage to api doc (#32139)
PR Close #32139
2019-08-27 16:18:53 -07:00
Miško Hevery 64770571b2 perf: don't create holey arrays (#32155)
Don't use `Array` constructor with the size value (ex. `new Array(5)`) - this will create a `HOLEY_ELEMENTS` array (even if this array is filled in later on!);

https://v8.dev/blog/elements-kinds
https://stackoverflow.com/questions/32054170/how-to-resize-an-array

PR Close #32155
2019-08-21 08:27:43 -07:00
Igor Minar 6ece7db37a build: TypeScript 3.5 upgrade (#31615)
https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#typescript-35

PR Close #31615
2019-07-25 17:05:23 -07:00
George Kalpakas 1bcd58cee8 refactor(docs-infra): remove `linenums=false` since it is now the default (#31674)
PR Close #31674
2019-07-24 14:38:54 -07:00
Paul Gschwendtner 647d7bdd88 refactor: fix typescript strict flag failures in all tests (#30993)
Fixes all TypeScript failures caused by enabling the `--strict`
flag for test source files. We also want to enable the strict
options for tests as the strictness enforcement improves the
overall codehealth, unveiled common issues and additionally it
allows us to enable `strict` in the `tsconfig.json` that is picked
up by IDE's.

PR Close #30993
2019-07-18 14:21:26 -07:00
Paul Gschwendtner 69a612d402 refactor(service-worker): ensure compatibility with typescript strict flag (#30993)
As part of FW-1265, the `@angular/service-worker` package is made compatible
with the TypeScript `--strict` flag. Read more about the strict flag [here](https://www.typescriptlang.org/docs/handbook/compiler-options.html)

PR Close #30993
2019-07-18 14:21:26 -07:00
Greg Magolan 361109d80f build: update to rules_nodejs 0.32.2 (#31325)
Brings in ts_library fixes required to get angular/angular building after 0.32.0:
typescript: exclude typescript lib declarations in node_module_library transitive_declarations
typescript: remove override of @bazel/tsetse (+1 squashed commit)

@npm//node_modules/foobar:foobar.js labels changed to @npm//:node_modules/foobar/foobar.js with fix for bazelbuild/rules_nodejs#802

also updates to rules_rass commit compatible with rules_nodejs 0.32.0

PR Close #31325
2019-07-01 14:16:42 -07:00
George Kalpakas 19e8570ac0 refactor(service-worker): remove redundant cache operation (#30977)
At this point, the response will have been cached (or scheduled to be
cached) in other code paths, so caching it again is redundant.

PR Close #30977
2019-06-27 09:52:29 -07:00
George Kalpakas 93abc35213 fix(service-worker): cache opaque responses when requests exceeds timeout threshold (#30977)
PR Close #30977
2019-06-27 09:52:29 -07:00
George Kalpakas d7be38f84b fix(service-worker): cache opaque responses in data groups with `freshness` strategy (#30977)
Previously, (presummably due to a typo) the `okToCacheOpaque` argument
of `DataGroup#cacheResponse()` was essentially never taken into account
(since opaque responses have a non-200 status code and thus `res.ok` is
always false).

This commit fixes the typo, which allows opaque responses to be cached
when `okToCacheOpaque` is true (i.e. in data groups using the
`freshness` strategy).

Fixes #30968

PR Close #30977
2019-06-27 09:52:29 -07:00
George Kalpakas 2d38623974 refactor(service-worker): make the caching behavior more explicit (#30977)
This commit doesn't change the behavior wrt caching, but it makes it
more explicit that only non-timed-out responses are cached. In case of a
timeout, `res` would be set to a programmatically created 504
`Response`, so `cacheResponse()` (which checks for `res.ok`) would not
have cached it anyway, but this makes change makes it more explicit (and
more similar to the equivalent part in [handleFetchWithFreshness()][1]).

[1]: https://github.com/angular/angular/blob/2b4d5c754/packages/service-worker/worker/src/data.ts#L379-L388

PR Close #30977
2019-06-27 09:52:28 -07:00
George Kalpakas 5306330d85 test(service-worker): better simulate opaque requests (#30977)
Previously, opaque responses where handled a little differently than
other responses from the mock server. More specifically, they were not
tracked (so no assertions could be made for them) and their
[`Body` mixin][1] methods (such as `arrayBuffer()`, `json()`, `text()`)
would throw an error due to `body` being `null`.

This commit ensures opaque responses are also captured on the mock
server and also changes `Body` mixin methods to better simulate the
[spec'd behavior][2].

(These improvements will be necessary to test caching of opaque
responses in a subsequent commit.)

[1]: https://developer.mozilla.org/en-US/docs/Web/API/Body
[2]: https://fetch.spec.whatwg.org/#concept-body-consume-body

PR Close #30977
2019-06-27 09:52:28 -07:00
George Kalpakas c150354464 test(service-worker): properly reset mock server state for each test (#30977)
PR Close #30977
2019-06-27 09:52:28 -07:00
George Kalpakas 7217525da4 test(service-worker): simplify test helpers (#30977)
PR Close #30977
2019-06-27 09:52:28 -07:00
George Kalpakas b6e8d19313 test(service-worker): remove obsolete async test helpers (#30977)
Jasmine natively supports returning promises from spec functions for
quite some time now. We don't need special async helpers.

PR Close #30977
2019-06-27 09:52:28 -07:00
Alex Rickabaugh 660800ca4e Revert "build: update to rules_nodejs 0.32.2 (#31019)" (#31267)
This reverts commit a38433f36b.

Reason: this causes failures in g3 with i18n extraction. See #31267.

PR Close #31267
2019-06-25 14:36:00 -07:00
Greg Magolan a38433f36b build: update to rules_nodejs 0.32.2 (#31019)
Brings in ts_library fixes required to get angular/angular building after 0.32.0:
typescript: exclude typescript lib declarations in node_module_library transitive_declarations
typescript: remove override of @bazel/tsetse (+1 squashed commit)

@npm//node_modules/foobar:foobar.js labels changed to @npm//:node_modules/foobar/foobar.js with fix for bazelbuild/rules_nodejs#802

also updates to rules_rass commit compatible with rules_nodejs 0.32.0

PR Close #31019
2019-06-25 10:21:07 -07:00
Hoel IRIS a5dd4edab9 fix(service-worker): registration failed on Safari (#31140)
Since Angular v8, and commit b3dda0e, `parseUrl()` can be called without
`relativeTo`, thus `new URL()` can be called with `relativeTo = undefined`.

Safari does not like it and the service worker registration fails:
```js
new URL('https://angular.io/') // OK
new URL('https://angular.io/', undefined) // TypeError
```

Closes #31061

PR Close #31140
2019-06-24 14:58:58 -07:00
Alex Eagle ef0b2cc74d build: convert entry_point to label (#30627)
PR Close #30627
2019-06-11 00:03:11 +00:00
Hoel IRIS 81c2a94310 fix(service-worker): avoid uncaught rejection warning when registration fails (#30876)
Service worker API `navigator.serviceWorker.register` can fail in multiple ways.
For example, in Chrome, with an unstable network connection you can have the
following error: `An unknown error occurred when fetching the script.`

In the current state, it creates an `Uncaught (in promise) TypeError:` style of
error, which cannot be caught by the user on his own.

I think it's better to log the error over raising an error that cannot be
caught.

PR Close #30876
2019-06-07 08:46:25 -07:00
Alex Eagle 337b6fe003 build: remove unreferenced tsconfig-build.json files (#30858)
These are no longer needed since Bazel generates a tsconfig for each compilation

PR Close #30858
2019-06-05 09:03:36 -07:00
Peter Johan Salomonsen 6200732e23 feat(service-worker): support bypassing SW with specific header/query param (#30010)
Add support for bypassing the ServiceWorker for a request by using the
ngsw-bypass header or query parameter.

Fixes #21191

PR Close #30010
2019-04-25 13:09:44 -07:00
George Kalpakas 957f594d7c test(service-worker): add tests for `RegistrationOptions#registrationStrategy` (#21842)
PR Close #21842
2019-04-25 12:29:59 -07:00
George Kalpakas 8f120aff33 refactor(service-worker): DRY up SW registration logic (#21842)
PR Close #21842
2019-04-25 12:29:58 -07:00
JiaLi.Passion 4cfba58072 feat(service-worker): allow configuring when the SW is registered (#21842)
Fixes #20970

PR Close #21842
2019-04-25 12:29:58 -07:00
George Kalpakas aa53d6cc6d docs(service-worker): improve `SwRegistrationOptions` docs and add example (#21842)
PR Close #21842
2019-04-25 12:29:58 -07:00
George Kalpakas be28a6ad8e test(service-worker): expand `SwRegistrationOptions` tests and move to separate file (#21842)
PR Close #21842
2019-04-25 12:29:58 -07:00
deebloo 39c0152b76 feat(service-worker): expose `SwRegistrationOptions` token to allow runtime 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
2019-04-25 12:29:58 -07:00
George Kalpakas d7887ab4ab refactor(service-worker): rename `RegistrationOptions` to `SwRegistrationOptions` (#21842)
This is in preparation of making `RegistrationOptions` part of the
public API (in a subsequent commit).

PR Close #21842
2019-04-25 12:29:58 -07:00
George Kalpakas 6cd3743b44 refactor(service-worker): use `Adapter#parseUrl()` for all URL parsing (#27080)
This commit also ensures that the correct implementation is used on
environments that do not support `URL` (e.g. Node.js).

PR Close #27080
2019-03-21 12:07:56 -04:00
George Kalpakas a24f4b51b3 test(service-worker): test support for multiple apps on different subpaths of a domain (#27080)
PR Close #27080
2019-03-21 12:07:56 -04:00
George Kalpakas 84baa0bb08 test(service-worker): make it easy to use a different SW scope in tests (#27080)
PR Close #27080
2019-03-21 12:07:56 -04:00
Sheik Althaf e721c08c7f feat(service-worker): support multiple apps on different subpaths of a domain (#27080)
Previously, it was not possible to have multiple apps (using
`@angular/service-worker`) on different subpaths of the same domain,
because each SW would overwrite the caches of the others (even though
their scope was different).

This commit fixes it by ensuring that the cache names created by the SW
are different for each scope.

Fixes #21388

PR Close #27080
2019-03-21 12:07:56 -04:00
George Kalpakas 37a154e4e6 test(service-worker): do not create testing artifacts on environments that do not support SW (#27080)
The tests will not be run anyway, so the artifacts are never used and
there might be errors if creating the testing artifacts relies on APIs
that are not available in that environment (e.g. `URL`).

PR Close #27080
2019-03-21 12:07:56 -04:00
George Kalpakas 415de9a291 test(service-worker): ensure `SwTestHarness#parseUrl()` behaves the same on browser and Node.js (#27080)
PR Close #27080
2019-03-21 12:07:56 -04:00
George Kalpakas b3dda0ebc1 refactor(service-worker): make second parameter to `Adapter#parseUrl()` optional (#27080)
PR Close #27080
2019-03-21 12:07:56 -04:00
Matias Niemelä fc8048ddaf build: update to nodejs rules 0.27 (#29210)
PR Close #29210
2019-03-14 19:35:00 -07:00
Matias Niemelä b2aadffbbc revert: build: update to nodejs rules 0.27 (#29210)
This reverts commit 08231f0bfa.
2019-03-14 17:38:35 -07:00
Greg Magolan 08231f0bfa build: update to nodejs rules 0.27 (#29210)
PR Close #29210
2019-03-14 11:38:12 -04:00
Alan 9a1959269f build: remove now redundant `bundle_dts = True` attribute (#29128)
This is now turned on by default in the ng_module macro

PR Close #29128
2019-03-07 10:47:20 -08:00
Cyrille Tuzi 3bee0f684d feat(service-worker): add JSON schema for service worker config (#27859)
Fixes #19847

PR Close #27859
2019-03-05 16:48:26 -08:00
George Kalpakas 586234bb01 fix(service-worker): detect new version even if files are identical to an old one (#26006)
Previously, if an app version contained the same files as an older
version (e.g. making a change, then rolling it back), the SW would not
detect it as the latest version (and update clients).

This commit fixes it by adding a `timestamp` field in `ngsw.json`, which
makes each build unique (with sufficiently high probability).

Fixes #24338

PR Close #26006
2019-03-05 09:41:44 -08:00
George Kalpakas 48214e2a05 fix(service-worker): ignore passive mixed content requests (#25994)
Although [passive mixed content][1] requests (like images) only produce
a warning without a ServiceWorker, fetching it via a ServiceWorker
results in an error. See
https://github.com/angular/angular/issues/23012#issuecomment-376430187
for more details.

This commit makes the ServiceWorker ignore such requests and let them be
handled by the browser directly to avoid breaking apps that would work
without the ServiceWorker.

[1]: https://developers.google.com/web/fundamentals/security/prevent-mixed-content/what-is-mixed-content#passive_mixed_content

Fixes #23012

PR Close #25994
2019-03-04 11:51:05 -08:00
Greg Magolan ea09430039 build: rules_nodejs 0.26.0 & use @npm instead of @ngdeps now that downstream angular build uses angular bundles (#28871)
PR Close #28871
2019-02-28 12:06:36 -08:00