@nagyv/parcel-plugin-workbox-precache
v0.1.9
Published
Parcel plugin that generates a precache manifest of the bundle and injects it in the `service_worker.js` file.
Downloads
2
Readme
parcel-plugin-workbox-precache
Parcel plugin that generates a precache manifest of the bundle and
injects it in the service_worker.js
file.
Installation
$ npm i -d parcel-plugin-workbox-precache
or
$ yarn add --dev parcel-plugin-workbox-precache
Attention: parcel-bundler has to be installed
Usage
This module can be used to generate a precache manifest for use with
workbox. It expects an asset named service_worker.js
after the
Parcel bundler has been run. It injects a importScripts
of workbox
from the Google CDN and injects a generated precache manifest as a
constant named precacheManifest
.
The script is injected in the head of the service worker
file. Injected code is surrounded with the comments //BEGIN workbox
and //END workbox
, when the comments are found the code is injected
between them. This is to make sure that parcel dev
works as
expected.
Note that this plugin probably does not work with multiple entry assets.
Example
index.html
<html>
<head>
<script src="./index.js"></script>
</head>
<body>
</body>
</html>
index.js
navigator.serviceWorker.register('/service_worker.js');
service_worker.js
/* global self, workbox, precacheManifest */
workbox.precacheAndRoute(precacheManifest);