Giter Site home page Giter Site logo

rlopes-fixeads / serviceworker-webpack-plugin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from oliviertassinari/serviceworker-webpack-plugin

0.0 0.0 0.0 104 KB

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

Home Page: https://oliviertassinari.github.io/serviceworker-webpack-plugin/

JavaScript 94.36% Shell 5.64%

serviceworker-webpack-plugin's Introduction

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.
  • 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.

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

serviceworker-webpack-plugin's People

Contributors

melvey avatar oliviertassinari avatar oller avatar sabinmarcu avatar shidhincr avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.