angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

service worker not refreshing files on new build

Open Argentan opened this issue 8 years ago • 2 comments

Versions

Angular CLI: 1.6.0
Node: 8.9.1
OS: win32 x64
Angular: 5.1.0
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router, service-worker

@angular/cdk: 5.0.1
@angular/cli: 1.6.0
@angular/flex-layout: 2.0.0-beta.10-4905443
@angular/material: 5.0.1
@angular-devkit/build-optimizer: 0.0.35
@angular-devkit/core: 0.0.22
@angular-devkit/schematics: 0.0.41
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.0
@schematics/angular: 0.1.10
@schematics/schematics: 0.0.10
typescript: 2.4.2
webpack-bundle-analyzer: 2.9.1
webpack: 3.10.0

Repro steps

  • ng new test-SW --service-worker
  • ng build -prod
  • change something -anything- in the app
  • ng build

Observed behavior

The service worker doesn't refresh new files created by the modified build There is a good chance that I am the one doing something wrong, but I couldn't find an erros on my code, and i even tried this with a fresh new project just to be sure there was no bad interactions with other packages

Desired behavior

The service worker should refresh new files created by the modified build and thus present the updated version of the app

Proposed solution

The problem seams to be caused by ngsw-worker.js being exactely the same between builds. That way, when the browser compares the new worker with the old one, it won't refresh it, thus not reloading ngsw.json, nor the modified files.

One solution would be to prepend ngsw-worker.js with a commented line containing the date and time of each build, that way the browser would recognize a change, reinstall the worker, that in turn would cause the worker to reload ngsw.json, and that will cause the caching of the new files. This solution doesn't serve the new app the first time the user comes back, but on the second (first time the worker is refreshed, and only upon a new reload the new assets are used).

With only this minor change this could be fixed

previous code on angular-cli/packages/@angular/cli/utilities/service-worker/index.ts lines 87 to 89:

// Copy worker script to dist directory.
const workerCode = fs.readFileSync(workerPath);
fs.writeFileSync(path.resolve(outputPath, 'ngsw-worker.js'), workerCode);

proposed code:

 // Copy worker script to dist directory and stamp it with built time.
const date_str = `/* built on ${new Date().toISOString()} */\n`;
const workerCode = fs.readFileSync(workerPath); 
fs.writeFileSync(path.resolve(outputPath, 'ngsw-worker.js'), date_str + workerCode);

Argentan avatar Dec 14 '17 01:12 Argentan

Given the recent comments by @andreasonny83 and @alxhub at #7665, I am closing this one. Their got the right solution and explanation.

Argentan avatar Dec 14 '17 02:12 Argentan

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.