Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 01dadff

Browse files
alxhubfilipesilva
authored andcommittedJan 31, 2018
feat(@angular/cli): copy safety SW from NPM on SW build
Deploying apps with service workers can be tricky. This commit has the CLI copy the safety-worker.js file from @angular/service-worker (if it's present) into the dist/ directory of the bundled app, both providing a quick option for developers who need to deactivate an already installed worker. It also copies the file into the path dist/worker-basic.min.js, used by apps built on the previous SW. Once a SW path is deployed on the web, it needs to be served until all users have migrated off of that path.
1 parent 8247bb9 commit 01dadff

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed
 

Diff for: ‎packages/@angular/cli/utilities/service-worker/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export function augmentAppWithServiceWorker(projectRoot: string, appRoot: string
7070
outputPath: string, baseHref: string): Promise<void> {
7171
// Path to the worker script itself.
7272
const workerPath = resolveProjectModule(projectRoot, '@angular/service-worker/ngsw-worker.js');
73+
const safetyPath = path.join(path.dirname(workerPath), 'safety-worker.js');
7374
const configPath = path.resolve(appRoot, 'ngsw-config.json');
7475

7576
if (!fs.existsSync(configPath)) {
@@ -89,5 +90,12 @@ export function augmentAppWithServiceWorker(projectRoot: string, appRoot: string
8990
// Copy worker script to dist directory.
9091
const workerCode = fs.readFileSync(workerPath);
9192
fs.writeFileSync(path.resolve(outputPath, 'ngsw-worker.js'), workerCode);
93+
94+
// If @angular/service-worker has the safety script, copy it into two locations.
95+
if (fs.existsSync(safetyPath)) {
96+
const safetyCode = fs.readFileSync(safetyPath);
97+
fs.writeFileSync(path.resolve(outputPath, 'worker-basic.min.js'), safetyCode);
98+
fs.writeFileSync(path.resolve(outputPath, 'safety-worker.js'), safetyCode);
99+
}
92100
});
93101
}

Diff for: ‎tests/e2e/tests/build/service-worker.ts

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export default function() {
4242
.then(() => expectFileToMatch('dist/ngsw.json', /"\/foo\/bar\/index.html"/))
4343
.then(() => ng('build', '--prod', '--service-worker=false'))
4444
.then(() => expectFileNotToExist('dist/ngsw.json'))
45+
.then(() => writeFile('node_modules/@angular/service-worker/safety-worker.js', 'false'))
46+
.then(() => ng('build', '--prod'))
47+
.then(() => expectFileToExist('dist/safety-worker.js'))
48+
.then(() => expectFileToExist('dist/worker-basic.min.js'))
4549
.then(() => ng('eject', '--prod'))
4650
.then(() => silentNpm('install'))
4751
.then(() => npm('run', 'build'))

0 commit comments

Comments
 (0)
Please sign in to comment.