diff --git a/aio/content/examples/service-worker-getstart/src/app/app.component.css b/aio/content/examples/service-worker-getstart/src/app/app.component.css
deleted file mode 100755
index e69de29bb2..0000000000
diff --git a/aio/content/examples/service-worker-getstart/src/app/app.component.ts b/aio/content/examples/service-worker-getstart/src/app/app.component.ts
deleted file mode 100755
index 6b80f360d2..0000000000
--- a/aio/content/examples/service-worker-getstart/src/app/app.component.ts
+++ /dev/null
@@ -1,10 +0,0 @@
-import { Component } from '@angular/core';
-
-@Component({
- selector: 'app-root',
- templateUrl: './app.component.html',
- styleUrls: ['./app.component.css']
-})
-export class AppComponent {
- title = 'Service Workers';
-}
diff --git a/aio/content/examples/service-worker-getstart/e2e/app.e2e-spec.ts b/aio/content/examples/service-worker-getting-started/e2e/app.e2e-spec.ts
similarity index 66%
rename from aio/content/examples/service-worker-getstart/e2e/app.e2e-spec.ts
rename to aio/content/examples/service-worker-getting-started/e2e/app.e2e-spec.ts
index 02b2f1dcff..6d77937689 100755
--- a/aio/content/examples/service-worker-getstart/e2e/app.e2e-spec.ts
+++ b/aio/content/examples/service-worker-getting-started/e2e/app.e2e-spec.ts
@@ -1,9 +1,9 @@
import { AppPage } from './app.po';
import { browser, element, by } from 'protractor';
+
describe('sw-example App', () => {
let page: AppPage;
- let logo = element(by.css('img'));
beforeEach(() => {
page = new AppPage();
@@ -15,17 +15,18 @@ describe('sw-example App', () => {
});
it('should display the Angular logo', () => {
+ let logo = element(by.css('img'));
page.navigateTo();
expect(logo.isPresent()).toBe(true);
});
- it('should show a header for the list of links', function () {
+ it('should show a header for the list of links', () => {
const listHeader = element(by.css('app-root > h2'));
expect(listHeader.getText()).toEqual('Here are some links to help you start:');
});
it('should show a list of links', function () {
- element.all(by.css('ul > li > h2 > a')).then(function(items) {
+ element.all(by.css('ul > li > h2 > a')).then((items) => {
expect(items.length).toBe(4);
expect(items[0].getText()).toBe('Angular Service Worker Intro');
expect(items[1].getText()).toBe('Tour of Heroes');
@@ -33,5 +34,11 @@ describe('sw-example App', () => {
expect(items[3].getText()).toBe('Angular blog');
});
});
-
+ // Check for a rejected promise as the service worker is not enabled
+ it('SwUpdate.checkForUpdate() should return a rejected promise', () => {
+ const button = element(by.css('button'));
+ const rejectMessage = element(by.css('p'));
+ button.click();
+ expect(rejectMessage.getText()).toContain('rejected: ');
+ });
});
diff --git a/aio/content/examples/service-worker-getstart/example-config.json b/aio/content/examples/service-worker-getting-started/example-config.json
similarity index 100%
rename from aio/content/examples/service-worker-getstart/example-config.json
rename to aio/content/examples/service-worker-getting-started/example-config.json
diff --git a/aio/content/examples/service-worker-getting-started/package.json b/aio/content/examples/service-worker-getting-started/package.json
new file mode 100644
index 0000000000..6886be13b7
--- /dev/null
+++ b/aio/content/examples/service-worker-getting-started/package.json
@@ -0,0 +1,50 @@
+{
+ "name": "angular.io-example",
+ "version": "0.0.0",
+ "license": "MIT",
+ "scripts": {
+ "ng": "ng",
+ "start": "ng serve",
+ "build": "ng build",
+ "test": "ng test",
+ "lint": "ng lint",
+ "e2e": "ng e2e"
+ },
+ "private": true,
+ "dependencies": {
+ "@angular/animations": "^5.0.0",
+ "@angular/common": "^5.0.0",
+ "@angular/compiler": "^5.0.0",
+ "@angular/core": "^5.0.0",
+ "@angular/forms": "^5.0.0",
+ "@angular/http": "^5.0.0",
+ "@angular/service-worker": "^5.0.0",
+ "@angular/platform-browser": "^5.0.0",
+ "@angular/platform-browser-dynamic": "^5.0.0",
+ "@angular/router": "^5.0.0",
+ "core-js": "^2.4.1",
+ "rxjs": "^5.5.2",
+ "zone.js": "^0.8.14"
+ },
+ "devDependencies": {
+ "@angular/cli": "1.5.4",
+ "@angular/compiler-cli": "^5.0.0",
+ "@angular/language-service": "^5.0.0",
+ "@types/jasmine": "~2.5.53",
+ "@types/jasminewd2": "~2.0.2",
+ "@types/node": "~6.0.60",
+ "codelyzer": "^4.0.1",
+ "jasmine-core": "~2.6.2",
+ "jasmine-spec-reporter": "~4.1.0",
+ "karma": "~1.7.0",
+ "karma-chrome-launcher": "~2.1.1",
+ "karma-cli": "~1.0.1",
+ "karma-coverage-istanbul-reporter": "^1.2.1",
+ "karma-jasmine": "~1.1.0",
+ "karma-jasmine-html-reporter": "^0.2.2",
+ "protractor": "~5.1.2",
+ "ts-node": "~3.2.0",
+ "tslint": "~5.7.0",
+ "typescript": "~2.4.2"
+ }
+}
diff --git a/aio/content/examples/service-worker-getting-started/plnkr.json b/aio/content/examples/service-worker-getting-started/plnkr.json
new file mode 100644
index 0000000000..661824d258
--- /dev/null
+++ b/aio/content/examples/service-worker-getting-started/plnkr.json
@@ -0,0 +1,5 @@
+{
+ "description": "Service Worker",
+ "basePath": "src/",
+ "tags": ["service worker"]
+ }
\ No newline at end of file
diff --git a/aio/content/examples/service-worker-getstart/src/app/app.component.html b/aio/content/examples/service-worker-getting-started/src/app/app.component.html
similarity index 91%
rename from aio/content/examples/service-worker-getstart/src/app/app.component.html
rename to aio/content/examples/service-worker-getting-started/src/app/app.component.html
index 6d21987a20..59cd86bcb5 100755
--- a/aio/content/examples/service-worker-getstart/src/app/app.component.html
+++ b/aio/content/examples/service-worker-getting-started/src/app/app.component.html
@@ -5,6 +5,10 @@
+
+
+
{{updateCheckText}}
+
Here are some links to help you start:
diff --git a/aio/content/examples/service-worker-getstart/src/app/app.component.spec.ts b/aio/content/examples/service-worker-getting-started/src/app/app.component.spec.ts
similarity index 91%
rename from aio/content/examples/service-worker-getstart/src/app/app.component.spec.ts
rename to aio/content/examples/service-worker-getting-started/src/app/app.component.spec.ts
index bcbdf36b3e..a136c59bfb 100755
--- a/aio/content/examples/service-worker-getstart/src/app/app.component.spec.ts
+++ b/aio/content/examples/service-worker-getting-started/src/app/app.component.spec.ts
@@ -16,12 +16,12 @@ describe('AppComponent', () => {
it(`should have as title 'app'`, async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
- expect(app.title).toEqual('app');
+ expect(app.title).toEqual('Service Workers');
}));
it('should render title in a h1 tag', async(() => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.debugElement.nativeElement;
- expect(compiled.querySelector('h1').textContent).toContain('Welcome to app!');
+ expect(compiled.querySelector('h1').textContent).toContain('Welcome to Service Workers!');
}));
});
diff --git a/aio/content/examples/service-worker-getting-started/src/app/app.component.ts b/aio/content/examples/service-worker-getting-started/src/app/app.component.ts
new file mode 100755
index 0000000000..67cc894396
--- /dev/null
+++ b/aio/content/examples/service-worker-getting-started/src/app/app.component.ts
@@ -0,0 +1,20 @@
+import { Component } from '@angular/core';
+import { SwUpdate } from '@angular/service-worker';
+
+@Component({
+ selector: 'app-root',
+ templateUrl: './app.component.html'
+})
+export class AppComponent {
+ title = 'Service Workers';
+ updateCheckText = '';
+
+ constructor(private update: SwUpdate) {}
+
+ updateCheck(): void {
+ this.update
+ .checkForUpdate()
+ .then(() => this.updateCheckText = 'resolved')
+ .catch(err => this.updateCheckText = `rejected: ${err.message}`);
+ }
+}
diff --git a/aio/content/examples/service-worker-getstart/src/app/app.module.ts b/aio/content/examples/service-worker-getting-started/src/app/app.module.ts
similarity index 100%
rename from aio/content/examples/service-worker-getstart/src/app/app.module.ts
rename to aio/content/examples/service-worker-getting-started/src/app/app.module.ts
diff --git a/aio/content/examples/service-worker-getstart/src/app/check-for-update.service.ts b/aio/content/examples/service-worker-getting-started/src/app/check-for-update.service.ts
similarity index 53%
rename from aio/content/examples/service-worker-getstart/src/app/check-for-update.service.ts
rename to aio/content/examples/service-worker-getting-started/src/app/check-for-update.service.ts
index 81ca74fff9..ed717fe384 100755
--- a/aio/content/examples/service-worker-getstart/src/app/check-for-update.service.ts
+++ b/aio/content/examples/service-worker-getting-started/src/app/check-for-update.service.ts
@@ -1,19 +1,15 @@
import { Injectable } from '@angular/core';
import { SwUpdate } from '@angular/service-worker';
-import { Observable } from 'rxjs/Observable';
-import 'rxjs/add/observable/interval';
-
-function promptUser(event): boolean {
- return true;
-}
// #docregion sw-check-update
+import { interval } from 'rxjs/observable/interval';
+
@Injectable()
export class CheckForUpdateService {
constructor(updates: SwUpdate) {
- Observable.interval(6 * 60 * 60).subscribe(() => updates.checkForUpdate());
+ interval(6 * 60 * 60).subscribe(() => updates.checkForUpdate());
}
}
// #enddocregion sw-check-update
diff --git a/aio/content/examples/service-worker-getstart/src/app/log-update.service.ts b/aio/content/examples/service-worker-getting-started/src/app/log-update.service.ts
similarity index 100%
rename from aio/content/examples/service-worker-getstart/src/app/log-update.service.ts
rename to aio/content/examples/service-worker-getting-started/src/app/log-update.service.ts
diff --git a/aio/content/examples/service-worker-getstart/src/app/prompt-update.service.ts b/aio/content/examples/service-worker-getting-started/src/app/prompt-update.service.ts
similarity index 100%
rename from aio/content/examples/service-worker-getstart/src/app/prompt-update.service.ts
rename to aio/content/examples/service-worker-getting-started/src/app/prompt-update.service.ts
diff --git a/aio/content/examples/service-worker-getstart/src/index.html b/aio/content/examples/service-worker-getting-started/src/index.html
similarity index 56%
rename from aio/content/examples/service-worker-getstart/src/index.html
rename to aio/content/examples/service-worker-getting-started/src/index.html
index 2f9232b839..c3066538b7 100755
--- a/aio/content/examples/service-worker-getstart/src/index.html
+++ b/aio/content/examples/service-worker-getting-started/src/index.html
@@ -5,8 +5,6 @@
SwExample
-
-
diff --git a/aio/content/examples/service-worker-getstart/src/main.ts b/aio/content/examples/service-worker-getting-started/src/main.ts
similarity index 100%
rename from aio/content/examples/service-worker-getstart/src/main.ts
rename to aio/content/examples/service-worker-getting-started/src/main.ts
diff --git a/aio/content/examples/service-worker-getstart/src/ngsw-config.json b/aio/content/examples/service-worker-getting-started/src/ngsw-config.json
similarity index 100%
rename from aio/content/examples/service-worker-getstart/src/ngsw-config.json
rename to aio/content/examples/service-worker-getting-started/src/ngsw-config.json
diff --git a/aio/content/guide/service-worker-comm.md b/aio/content/guide/service-worker-communications.md
similarity index 72%
rename from aio/content/guide/service-worker-comm.md
rename to aio/content/guide/service-worker-communications.md
index 8c65444e83..62f327b1f2 100644
--- a/aio/content/guide/service-worker-comm.md
+++ b/aio/content/guide/service-worker-communications.md
@@ -1,7 +1,15 @@
-# Communicating with service workers
+# Service Worker Communication
Importing `ServiceWorkerModule` into your `AppModule` doesn't just register the service worker, it also provides a few services you can use to interact with the service worker and control the caching of your app.
+#### Prerequisites
+
+A basic understanding of the following:
+* [Getting Started with Service Workers](guide/service-worker-getting-started).
+
+
+
+
## `SwUpdate` service
The `SwUpdate` service gives you access to events that indicate when the service worker has discovered an available update for your app or when it has activated such an update—meaning it is now serving content from that update to your app.
@@ -16,7 +24,7 @@ The `SwUpdate` service supports four separate operations:
The two update events, `available` and `activated`, are `Observable` properties of `SwUpdate`:
-
+
You can use these events to notify the user of a pending update or to refresh their pages when the code they are running is out of date.
@@ -27,7 +35,7 @@ It's possible to ask the service worker to check if any updates have been deploy
Do this with the `checkForUpdate()` method:
-
+
This method returns a `Promise` which indicates that the update check has completed successfully, though it does not indicate whether an update was discovered as a result of the check. Even if one is found, the service worker must still successfully download the changed files, which can fail. If successful, the `available` event will indicate availability of a new version of the app.
@@ -36,6 +44,11 @@ This method returns a `Promise` which indicates that the update check has comple
If the current tab needs to be updated to the latest app version immediately, it can ask to do so with the `activateUpdate()` method:
-
+
Doing this could break lazy-loading into currently running apps, especially if the lazy-loaded chunks use filenames with hashes, which change every version.
+
+## More on Angular service workers
+
+You may also be interested in the following:
+* [Service Worker in Production](guide/service-worker-devops).
diff --git a/aio/content/guide/service-worker-configref.md b/aio/content/guide/service-worker-config.md
similarity index 97%
rename from aio/content/guide/service-worker-configref.md
rename to aio/content/guide/service-worker-config.md
index e77bc78861..aacd04b1fb 100644
--- a/aio/content/guide/service-worker-configref.md
+++ b/aio/content/guide/service-worker-config.md
@@ -1,6 +1,13 @@
{@a glob}
-# Reference: Configuration file
+# Service Worker Configuration
+
+#### Prerequisites
+
+A basic understanding of the following:
+* [Service Worker in Production](guide/service-worker-devops).
+
+
The `src/ngsw-config.json` configuration file specifies which files and data URLs the Angular
service worker should cache and how it should update the cached files and data. The
@@ -8,7 +15,7 @@ CLI processes the configuration file during `ng build --prod`. Manually, you can
it with the `ngsw-config` tool:
```sh
-ngsw-config dist src/ngswn-config.json /base/href
+ngsw-config dist src/ngsw-config.json /base/href
```
The configuration file uses the JSON format. All file paths must begin with `/`, which is the deployment directory—usually `dist` in CLI projects.
@@ -159,3 +166,4 @@ The Angular service worker can use either of two caching strategies for data res
* `performance`, the default, optimizes for responses that are as fast as possible. If a resource exists in the cache, the cached version is used. This allows for some staleness, depending on the `maxAge`, in exchange for better performance. This is suitable for resources that don't change often; for example, user avatar images.
* `freshness` optimizes for currency of data, preferentially fetching requested data from the network. Only if the network times out, according to `timeout`, does the request fall back to the cache. This is useful for resources that change frequently; for example, account balances.
+
diff --git a/aio/content/guide/service-worker-devops.md b/aio/content/guide/service-worker-devops.md
index 6af45fdd7d..fceca5f06e 100644
--- a/aio/content/guide/service-worker-devops.md
+++ b/aio/content/guide/service-worker-devops.md
@@ -1,7 +1,14 @@
-# DevOps: Angular service worker in production
+# Service Worker in Production
This page is a reference for deploying and supporting production apps that use the Angular service worker. It explains how the Angular service worker fits into the larger production environment, the service worker's behavior under various conditions, and available recourses and fail-safes.
+#### Prerequisites
+
+A basic understanding of the following:
+* [Service Worker Communication](guide/service-worker-communications).
+
+
+
## Service worker and caching of app resources
Conceptually, you can imagine the Angular service worker as a forward cache or a CDN edge that is installed in the end user's web browser. The service worker's job is to satisfy requests made by the Angular app for resources or data from a local cache, without needing to wait for the network. Like any cache, it has rules for how content is expired and updated.
@@ -297,4 +304,8 @@ for `ngsw.json` returns a `404`, then the service worker
removes all of its caches and de-registers itself,
essentially self-destructing.
+## More on Angular service workers
+
+You may also be interested in the following:
+* [Service Worker Configuration](guide/service-worker-config).
diff --git a/aio/content/guide/service-worker-getstart.md b/aio/content/guide/service-worker-getting-started.md
similarity index 85%
rename from aio/content/guide/service-worker-getstart.md
rename to aio/content/guide/service-worker-getting-started.md
index aaa51b60b3..915dbcccbb 100644
--- a/aio/content/guide/service-worker-getstart.md
+++ b/aio/content/guide/service-worker-getting-started.md
@@ -1,10 +1,15 @@
-# Getting started
+# Getting Started with Service Workers
+
+#### Prerequisites
+
+A basic understanding of the following:
+* [Introduction to Angular service workers](guide/service-worker-intro).
+
+
+
Beginning in Angular 5.0.0, you can easily enable Angular service worker support in any CLI project. This document explains how to enable Angular service worker support in new and existing projects. It then uses a simple example to show you a service worker in action, demonstrating loading and basic caching.
-See the .
-
-
## Adding a service worker to a new application
If you're generating a new CLI project, you can use the CLI to set up the Angular service worker as part of creating the project. To do so, add the `--service-worker` flag to the `ng new` command:
@@ -54,12 +59,12 @@ To import and register the Angular service worker:
At the top of the root module, `src/app/app.module.ts`, import `ServiceWorkerModule` and `environment`.
-
+
Add `ServiceWorkerModule` to the `@NgModule` `imports` array. Use the `register()` helper to take care of registering the service worker, taking care to disable the service worker when not running in production mode.
-
+
The file `ngsw-worker.js` is the name of the prebuilt service worker script, which the CLI copies into `dist/` to deploy along with your server.
@@ -72,7 +77,7 @@ You can begin with the boilerplate version from the CLI, which configures sensib
Alternately, save the following as `src/ngsw-config.json`:
-
+
### Step 5: Build the project
@@ -92,7 +97,9 @@ using an example application.
### Serving with `http-server`
-As `ng serve` does not work with service workers, you must use a real HTTP server to test your project locally. It's a good idea to test on a dedicated port.
+Because `ng serve` does not work with service workers, you must use a seperate HTTP server to test your project locally. You can use any HTTP server. The example below uses the [http-server](https://www.npmjs.com/package/http-server) package from npm. To reduce the possibility of conflicts, test on a dedicated port.
+
+To serve with `http-server`, change to the directory containing your web files and start the web server:
```sh
cd dist
@@ -181,7 +188,6 @@ What went wrong? Nothing, actually. The Angular service worker is doing its job
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.
2. Refresh the page.
-![The text has changed to say "Bienvenue à app!"](generated/images/guide/service-worker/welcome-msg-fr.png)