1 - We can generate a project with service-worker
$ng new simple-pwa --service-worker
What's new in Angular boiler-plate?
2 - A new file ngsw-config.json in order to configure our service worker
3 - A ServiceWorker module available in order to manage it programmatically
{
appData,
index,
assetGroups : [{
name,
installMode,
updateMode,
resources
}],
dataGroups : [{
name,
urls,
version,
cacheConfig
}]
}
source : https://angular.io/guide/service-worker-config
/**
* Subscribe to update notifications from the Service Worker, trigger update
* checks, and forcibly activate updates.
*
* @experimental
*/
export declare class SwUpdate {
private sw;
readonly available: Observable<UpdateAvailableEvent>;
readonly activated: Observable<UpdateActivatedEvent>;
constructor(sw: NgswCommChannel);
/**
* Returns true if the Service Worker is enabled (supported by the browser and enabled via
* ServiceWorkerModule).
*/
readonly isEnabled: boolean;
checkForUpdate(): Promise<void>;
activateUpdate(): Promise<void>;
}
#ng build --prod
#cd dist/
#http-server -p 8080
build it for prod env
go to generated directory
serve generated files
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.
.angular-cli-json
...
"assets": [
"assets",
"favicon.ico",
"manifest.json"
]
...
refresh SW
Unregister SW :
chrome://serviceworker-internals/