Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide option to auto generate register bundle script #65

Open
goldhand opened this issue Apr 9, 2017 · 1 comment
Open

Provide option to auto generate register bundle script #65

goldhand opened this issue Apr 9, 2017 · 1 comment

Comments

@goldhand
Copy link
Owner

goldhand commented Apr 9, 2017

When a user opts in, emit a bundle with a registration script.

API:

  ...
  output: {
    path: path.resolve(__dirname, 'dist/'),
    filename: '[name]-[hash].js',
    publicPath: 'http://localhost:3000/',
  },
  plugins: [
    new SWPrecacheWebpackPlugin(
      {
        cacheId: 'my-project-name',
        filename: 'my-service-worker.js'
        register: true,
      }
    ),
  ],
  ...

emits: dist/register-sw-[hash].js

(function() {
  if('serviceWorker' in navigator) {
    navigator.serviceWorker.register('http://localhost:3000/my-service-worker.js');
  }
})();

This has the added benefit of being picked up by webpack template plugins that inject all bundles into an html template (html-webpack-plugin #62)

@goldhand
Copy link
Owner Author

apply(compiler) {
    const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
    compiler.plugin("entry-option", (context, entry) => {
      compiler.apply(new SingleEntryPlugin('./', 'register-service-worker', 'register-service-worker'));
    });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant