• This repository has been archived on 20/Nov/2019
  • Stars
    star
    458
  • Rank 95,591 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simplifies creation of a service worker to serve your webpack bundles. ♻️

serviceworker-webpack-plugin

Simplifies creation of a service worker to serve your webpack bundles.

npm version npm downloads Build Status

Dependencies DevDependencies

Installation

npm install serviceworker-webpack-plugin

The problem solved

When building a service worker, you probably want to cache all your assets during the install phase. But in order to do so, you need their names. That's not simple when you are using Webpack:

  • The assets names are non-deterministic when taking advantage of the long term caching.
  • The assets list can even evolve over time as you add splitting points or more resources.
  • You want to be able to use your service worker with the dev-server mode of Webpack.
  • You want to keep the build process as simple as possible.

Setup

1. Add the plugin to your webpack config

import ServiceWorkerWebpackPlugin from 'serviceworker-webpack-plugin';

...

  plugins: [
    new ServiceWorkerWebpackPlugin({
      entry: path.join(__dirname, 'src/sw.js'),
    }),
  ],

2. Register the service worker in your main JS thread

import runtime from 'serviceworker-webpack-plugin/lib/runtime';

if ('serviceWorker' in navigator) {
  const registration = runtime.register();
}

3. Write your own sw.js

You can now use the global serviceWorkerOption variable in your sw.js. E.g. In our example this object looks like:

{
  assets: [
    './main.256334452761ef349e91.js',
  ],
}

Simple example

You can have a look at the /docs folder if you need a full working example.

API

ServiceWorkerWebpackPlugin(options)

  • options
  • entry, required, string: Path to the actual service worker implementation.
  • filename, string, default 'sw.js': Relative (from the webpack's config output.path) output path for emitted script.
  • excludes, array, default ['**/.*', '**/*.map']: Exclude matched assets from being added to the serviceWorkerOption.assets variable. (Blacklist)
  • includes, array, default ['**/*']: Include matched assets added to the serviceWorkerOption.assets variable. (Whitelist)
  • publicPath, string, default '/': Specifies the public URL address of the output files when referenced in a browser. We use this value to load the service worker over the network.
  • template, function, default noop: This callback function can be used to inject statically generated service worker. It's taking a serviceWorkerOption argument and must return a promise.
  • transformOptions, function: This callback function receives a raw serviceWorkerOption argument. The jsonStats key contains all the webpack build information.
  • minimize: Whether to minimize output. Defaults to process.env.NODE_ENV === 'production'

runtime(options)

Credit

Why simply not use the offline-plugin?

I wouldn't have been able to write this plugin without the offline-plugin project. Thanks @NekR for sharing it!

Still, soon after using it, I realized that it wasn't what I was looking for.

  • The abstraction provided was too high. (I needed to build some custom fetch logic.)
  • It was making me, even more, dependent on Webpack. (What if later, I want to switch to another build system?)

Hence, I decided to change the approach and created this thin layer on top of Webpack to solve the assets name issue. Nothing more.

If you don't care about my two issues with offline-plugin then you don't need to use this package, offline-plugin is great.

The specs

License

MIT

More Repositories

1

react-swipeable-views

A React component for swipeable views. ❄️
JavaScript
4,450
star
2

babel-plugin-transform-react-remove-prop-types

Remove unnecessary React propTypes from the production build. 🎈
JavaScript
898
star
3

babel-plugin-react-remove-properties

Babel plugin for removing React properties. 💨
JavaScript
380
star
4

react-event-listener

A React component for binding events on the global scope. 💫
JavaScript
359
star
5

a-journey-toward-better-style

A Journey toward better style
JavaScript
263
star
6

i18n-extract

Manage localization with static analysis. 🔍
JavaScript
170
star
7

SplitMe

Split expenses with friends.
JavaScript
154
star
8

browser-metrics

A collection of metrics tools for measuring performance ⚡️
JavaScript
125
star
9

babel-plugin-transform-dev-warning

Remove development warning from production code
JavaScript
15
star
10

react-benchmark

Environment to benchmark React Component 🏁
JavaScript
14
star
11

react-with-styles-interface-jss

Interface to use react-with-styles with JSS. 🐠
JavaScript
12
star
12

plume

Plume is an accurate indoor tracking system which uses the power of magnetic fields to compute a device's position and orientation. We are aiming at an accuracy of less than a centimeter for the position, and less than a degree for the orientation.
C
9
star
13

material-ui-generate-css-file

CSS
5
star
14

Kinect

Kinect processing
Java
4
star
15

Salonrama

PHP
3
star
16

mui-olivier-tools

TypeScript
3
star
17

mui-merge

test-repo
JavaScript
2
star
18

ELEC222

Architectures reconfigurables et langages HDL
VHDL
2
star
19

ELEC223

Pratique des systèmes à microprocesseurs
C
2
star
20

react-conf-2024-pigment-css

TypeScript
2
star
21

toolpad-issue-2340

https://github.com/mui/mui-toolpad/issues/2340
2
star
22

bug-markdown-release

2
star
23

AirDJ

AirDJ est un logiciel qui permet de mixer de la musique à partir du mouvement des mains, grâce à une kinect.
Java
2
star
24

test-appsmith

1
star
25

appsmith-git-sync

1
star
26

SplitMeBord

Présentation pour le cours de SIM203 - Entrepreneuriat
JavaScript
1
star
27

fooc-nextjs-css-modules

TypeScript
1
star
28

tmp

1
star
29

next-55817

CSS
1
star