Skip to content

Commit a1633c1

Browse files
dario-piotrowiczthePunderWoman
authored andcommitted
docs(service-worker): improve service worker guides (angular#43508)
improve slightly the angular.io guides related to service workers, by means of fixes, styling, information and clarity changes PR Close angular#43508
1 parent a2eae02 commit a1633c1

File tree

11 files changed

+52
-42
lines changed

11 files changed

+52
-42
lines changed

aio/content/examples/service-worker-getting-started/src/app/check-for-update.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { first } from 'rxjs/operators';
77
export class CheckForUpdateService {
88

99
constructor(appRef: ApplicationRef, updates: SwUpdate) {
10-
// Allow the app to stabilize first, before starting polling for updates with `interval()`.
10+
// Allow the app to stabilize first, before starting
11+
// polling for updates with `interval()`.
1112
const appIsStable$ = appRef.isStable.pipe(first(isStable => isStable === true));
1213
const everySixHours$ = interval(6 * 60 * 60 * 1000);
1314
const everySixHoursOnceAppIsStable$ = concat(appIsStable$, everySixHours$);

aio/content/examples/service-worker-getting-started/src/app/handle-unrecoverable-state.service.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ export class HandleUnrecoverableStateService {
99
constructor(updates: SwUpdate) {
1010
updates.unrecoverable.subscribe(event => {
1111
notifyUser(
12-
`An error occurred that we cannot recover from:\n${event.reason}\n\n` +
13-
'Please reload the page.');
12+
'An error occurred that we cannot recover from:\n' +
13+
event.reason +
14+
'\n\nPlease reload the page.'
15+
);
1416
});
1517
}
1618
}

aio/content/guide/app-shell.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,4 @@ Or to use the production configuration.
8787
ng run my-app:app-shell:production
8888
</code-example>
8989

90-
To verify the build output, open `dist/my-app/browser/index.html`. Look for default text `app-shell works!` to show that the application shell route was rendered as part of the output.
90+
To verify the build output, open <code class="no-auto-link">dist/my-app/browser/index.html</code>. Look for default text `app-shell works!` to show that the application shell route was rendered as part of the output.

aio/content/guide/service-worker-communications.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ For example, imagine the following scenario:
7777
This newer version includes the files `index.html`, `main.<main-hash-2>.js` and `lazy-chunk.<lazy-hash-2>.js` (note that the hashes are different now, because the content of the files has changed).
7878
The old version is no longer available on the server.
7979
- In the meantime, the user's browser decides to evict `lazy-chunk.<lazy-hash-1>.js` from its cache.
80-
Browsers may decide to evict specific (or all) resources from a cache in order to reclaim disk space.
80+
This is possible since browsers may decide to evict specific (or all) resources from a cache in order to reclaim disk space.
8181
- The user opens the application again.
8282
The service worker serves the latest version known to it at this point, namely the old version (`index.html` and `main.<main-hash-1>.js`).
8383
- At some later point, the application requests the lazy bundle, `lazy-chunk.<lazy-hash-1>.js`.
@@ -87,7 +87,7 @@ For example, imagine the following scenario:
8787
In the above scenario, the service worker is not able to serve an asset that would normally be cached.
8888
That particular application version is broken and there is no way to fix the state of the client without reloading the page.
8989
In such cases, the service worker notifies the client by sending an `UnrecoverableStateEvent` event.
90-
You can subscribe to `SwUpdate#unrecoverable` to be notified and handle these errors.
90+
You can subscribe to `SwUpdate.unrecoverable` to be notified and handle these errors.
9191

9292
<code-example path="service-worker-getting-started/src/app/handle-unrecoverable-state.service.ts" header="handle-unrecoverable-state.service.ts" region="sw-unrecoverable-state"></code-example>
9393

aio/content/guide/service-worker-config.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@ To use this strategy set `strategy` to `freshness` and `timeout` to `0u` in `cac
240240
This will essentially do the following:
241241

242242
1. Try to fetch from the network first.
243-
2. If the network request does not complete after 0ms (that is, immediately), fall back to the cache (ignoring cache age).
244-
3. Once the network request completes, update the cache for future requests.
245-
4. If the resource does not exist in the cache, wait for the network request anyway.
243+
1. If the network request does not complete after 0ms (that is, immediately), fall back to the cache (ignoring cache age).
244+
1. Once the network request completes, update the cache for future requests.
245+
1. If the resource does not exist in the cache, wait for the network request anyway.
246246

247247
</div>
248248

aio/content/guide/service-worker-devops.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ This file integrity is especially important when lazy loading modules.
2323
A JS bundle may reference many lazy chunks, and the filenames of the
2424
lazy chunks are unique to the particular build of the application. If a running
2525
application at version `X` attempts to load a lazy chunk, but the server has
26-
updated to version `X + 1` already, the lazy loading operation will fail.
26+
already updated to version `X + 1`, the lazy loading operation will fail.
2727

2828
The version identifier of the application is determined by the contents of all
2929
resources, and it changes if any of them change. In practice, the version
@@ -96,7 +96,7 @@ configured lifetimes.
9696

9797
It can be problematic for an application if the version of resources
9898
it's receiving changes suddenly or without warning. See the
99-
[Versions](guide/service-worker-devops#versions) section above
99+
[App versions](guide/service-worker-devops#versions) section above
100100
for a description of such issues.
101101

102102
The Angular service worker provides a guarantee: a running application
@@ -303,7 +303,7 @@ Tools open to differ from behavior a user might experience.
303303
* If you look in the Cache Storage viewer, the cache is frequently
304304
out of date. Right click the Cache Storage title and refresh the caches.
305305

306-
Stopping and starting the service worker in the Service Worker
306+
* Stopping and starting the service worker in the Service Worker
307307
pane triggers a check for updates.
308308

309309
## Service Worker Safety

aio/content/guide/service-worker-getting-started.md

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ A basic understanding of the information in [Introduction to Angular service wor
1010

1111
## Adding a service worker to your project
1212

13-
To set up the Angular service worker in your project, use the CLI command `ng add @angular/pwa`. It takes care of configuring your application to use service workers by adding the `service-worker` package along
13+
To set up the Angular service worker in your project, use the CLI command `ng add @angular/pwa`. It takes care of configuring your application to use service workers by adding the `@angular/service-worker` package along
1414
with setting up the necessary support files.
1515

1616
```sh
@@ -24,7 +24,7 @@ The above command completes the following actions:
2424
3. Imports and registers the service worker in the application module.
2525
4. Updates the `index.html` file:
2626
* Includes a link to add the `manifest.webmanifest` file.
27-
* Adds meta tags for `theme-color`.
27+
* Adds a meta tag for `theme-color`.
2828
5. Installs icon files to support the installed Progressive Web App (PWA).
2929
6. Creates the service worker configuration file called [`ngsw-config.json`](/guide/service-worker-config), which specifies the caching behaviors and other settings.
3030

@@ -55,10 +55,14 @@ http-server -p 8080 -c-1 dist/<project-name>
5555

5656
### Initial load
5757

58-
With the server running, you can point your browser at http://localhost:8080/. Your application should load normally.
58+
With the server running, you can point your browser at [http://localhost:8080](http://localhost:8080). Your application should load normally.
59+
60+
<div class="alert is-helpful">
5961

6062
**Tip:** When testing Angular service workers, it's a good idea to use an incognito or private window in your browser to ensure the service worker doesn't end up reading from a previous leftover state, which can cause unexpected behavior.
6163

64+
</div>
65+
6266
<div class="alert is-helpful">
6367

6468
**Note:**
@@ -68,14 +72,16 @@ If you are not using HTTPS, the service worker will only be registered when acce
6872

6973
### Simulating a network issue
7074

71-
To simulate a network issue, disable network interaction for your application. In Chrome:
75+
To simulate a network issue, disable network interaction for your application.
76+
77+
In Chrome:
7278

7379
1. Select **Tools** > **Developer Tools** (from the Chrome menu located at the top right corner).
74-
2. Go to the **Network tab**.
75-
3. Check the **Offline box**.
80+
1. Go to the **Network tab**.
81+
1. Select **Offline** in the **Throttling** dropdown menu.
7682

7783
<div class="lightbox">
78-
<img src="generated/images/guide/service-worker/offline-checkbox.png" alt="The offline checkbox in the Network tab is checked">
84+
<img src="generated/images/guide/service-worker/offline-option.png" alt="The offline option in the Network tab is selected">
7985
</div>
8086

8187
Now the application has no access to network interaction.
@@ -90,7 +96,7 @@ If you look at the Network tab, you can verify that the service worker is active
9096
<img src="generated/images/guide/service-worker/sw-active.png" alt="Requests are marked as from ServiceWorker">
9197
</div>
9298

93-
Notice that under the "Size" column, the requests state is `(from ServiceWorker)`. This means that the resources are not being loaded from the network. Instead, they are being loaded from the service worker's cache.
99+
Notice that under the "Size" column, the requests state is `(ServiceWorker)`. This means that the resources are not being loaded from the network. Instead, they are being loaded from the service worker's cache.
94100

95101

96102
### What's being cached?
@@ -104,7 +110,7 @@ Notice that all of the files the browser needs to render this application are ca
104110
* Images and fonts directly under the configured `outputPath` (by default `./dist/<project-name>/`) or `resourcesOutputPath`. See [`ng build`](cli/build) for more information about these options.
105111

106112

107-
<div class="alert is-helpful">
113+
<div class="alert is-important">
108114
Pay attention to two key points:
109115

110116
1. The generated `ngsw-config.json` includes a limited list of cacheable fonts and images extensions. In some cases, you might want to modify the glob pattern to suit your needs.
@@ -119,15 +125,15 @@ next step is understanding how updates work. Let's make a change to the applicat
119125

120126
1. If you're testing in an incognito window, open a second blank tab. This will keep the incognito and the cache state alive during your test.
121127

122-
2. Close the application tab, but not the window. This should also close the Developer Tools.
128+
1. Close the application tab, but not the window. This should also close the Developer Tools.
123129

124-
3. Shut down `http-server`.
130+
1. Shut down `http-server`.
125131

126-
4. Open `src/app/app.component.html` for editing.
132+
1. Open `src/app/app.component.html` for editing.
127133

128-
5. Change the text `Welcome to {{title}}!` to `Bienvenue à {{title}}!`.
134+
1. Change the text `Welcome to {{title}}!` to `Bienvenue à {{title}}!`.
129135

130-
6. Build and run the server again:
136+
1. Build and run the server again:
131137

132138
```sh
133139
ng build
@@ -138,25 +144,26 @@ http-server -p 8080 -c-1 dist/<project-name>
138144

139145
Now look at how the browser and service worker handle the updated application.
140146

141-
1. Open http://localhost:8080 again in the same window. What happens?
147+
1. Open [http://localhost:8080](http://localhost:8080) again in the same window. What happens?
142148

143-
<div class="lightbox">
144-
<img src="generated/images/guide/service-worker/welcome-msg-en.png" alt="It still says Welcome to Service Workers!">
145-
</div>
149+
<div class="lightbox">
150+
<img src="generated/images/guide/service-worker/welcome-msg-en.png" alt="It still says Welcome to Service Workers!">
151+
</div>
146152

147-
What went wrong? Nothing, actually. The Angular service worker is doing its job and serving the version of the application that it has **installed**, even though there is an update available. In the interest of speed, the service worker doesn't wait to check for updates before it serves the application that it has cached.
153+
What went wrong? Nothing, actually. The Angular service worker is doing its job and serving the version of the application that it has **installed**, even though there is an update available. In the interest of speed, the service worker doesn't wait to check for updates before it serves the application that it has cached.
148154

149-
If you look at the `http-server` logs, you can see the service worker requesting `/ngsw.json`. This is how the service worker checks for updates.
155+
If you look at the `http-server` logs, you can see the service worker requesting `/ngsw.json`. This is how the service worker checks for updates.
150156

151-
2. Refresh the page.
157+
1. Refresh the page.
152158

153-
<div class="lightbox">
154-
<img src="generated/images/guide/service-worker/welcome-msg-fr.png" alt="The text has changed to say Bienvenue à app!">
155-
</div>
159+
<div class="lightbox">
160+
<img src="generated/images/guide/service-worker/welcome-msg-fr.png" alt="The text has changed to say Bienvenue à app!">
161+
</div>
156162

157-
The service worker installed the updated version of your application *in the background*, and the next time the page is loaded or reloaded, the service worker switches to the latest version.
163+
The service worker installed the updated version of your application *in the background*, and the next time the page is loaded or reloaded, the service worker switches to the latest version.
158164

159165
## More on Angular service workers
160166

161167
You may also be interested in the following:
168+
* [App Shell](guide/app-shell).
162169
* [Communicating with service workers](guide/service-worker-communications).

aio/content/guide/service-worker-intro.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@ Angular applications, as single-page applications, are in a prime position to be
1717

1818
Angular's service worker is designed to optimize the end user experience of using an application over a slow or unreliable network connection, while also minimizing the risks of serving outdated content.
1919

20-
The Angular service worker's behavior follows that design goal:
20+
To achieve this, the Angular service worker follows these guidelines:
2121

2222
* Caching an application is like installing a native application. The application is cached as one unit, and all files update together.
2323
* A running application continues to run with the same version of all files. It does not suddenly start receiving cached files from a newer version, which are likely incompatible.
2424
* When users refresh the application, they see the latest fully cached version. New tabs load the latest cached code.
2525
* Updates happen in the background, relatively quickly after changes are published. The previous version of the application is served until an update is installed and ready.
2626
* The service worker conserves bandwidth when possible. Resources are only downloaded if they've changed.
2727

28-
To support these behaviors, the Angular service worker loads a *manifest* file from the server. The manifest describes the resources to cache and includes hashes of every file's contents. When an update to the application is deployed, the contents of the manifest change, informing the service worker that a new version of the application should be downloaded and cached. This manifest is generated from a CLI-generated configuration file called `ngsw-config.json`.
28+
To support these behaviors, the Angular service worker loads a *manifest* file from the server. The file, called `ngsw.json` (not to be confused with the [web app manifest](https://developer.mozilla.org/en-US/docs/Web/Manifest)), describes the resources to cache and includes hashes of every file's contents. When an update to the application is deployed, the contents of the manifest change, informing the service worker that a new version of the application should be downloaded and cached. This manifest is generated from a CLI-generated configuration file called `ngsw-config.json`.
2929

3030
Installing the Angular service worker is as easy as including an `NgModule`. In addition to registering the Angular service worker with the browser, this also makes a few services available for injection which interact with the service worker and can be used to control it. For example, an application can ask to be notified when a new update becomes available, or an application can ask the service worker to check the server for available updates.
3131

3232
## Prerequisites
3333

34-
To make use of all the features of Angular service worker, use the latest versions of Angular and the Angular CLI.
34+
To make use of all the features of Angular service workers, use the latest versions of Angular and the Angular CLI.
3535

3636
In order for service workers to be registered, the application must be accessed over HTTPS, not HTTP.
3737
Browsers ignore service workers on pages that are served over an insecure connection.
38-
The reason is that service workers are quite powerful, so extra care needs to be taken to ensure the service worker script has not been tampered with.
38+
The reason is that service workers are quite powerful, so extra care is needed to ensure the service worker script has not been tampered with.
3939

4040
There is one exception to this rule: to make local development easier, browsers do _not_ require a secure connection when accessing an application on `localhost`.
4141

@@ -48,7 +48,7 @@ Browsers like IE and Opera Mini do not support service workers.
4848
If the user is accessing your application with a browser that does not support service workers, the service worker is not registered and related behavior such as offline cache management and push notifications does not happen.
4949
More specifically:
5050

51-
* The browser does not download the service worker script and `ngsw.json` manifest file.
51+
* The browser does not download the service worker script and the `ngsw.json` manifest file.
5252
* Active attempts to interact with the service worker, such as calling `SwUpdate.checkForUpdate()`, return rejected promises.
5353
* The observable events of related services, such as `SwUpdate.available`, are not triggered.
5454

Binary file not shown.
Loading
Loading

0 commit comments

Comments
 (0)