LoginSignup
2
2

More than 5 years have passed since last update.

vue-pwa-boilerplate 上の service worker をカスタマイズする

Posted at

はじめに

vue で pwa をしようと思うと、 vue-pwa-boilerplate を使うことになると思います。
しかし vue-pwa-boilerplate で npm run build 等と deploy を行うと、dist/service-worker.js が自動的に生成されてしまいます。

chache の操作や設定は以下の URL にあるように設定ファイルから行うことはできますが、
Push 通知の notification などその他イベントの操作方法がどこにも書かれていなかったため、共有します。

やり方

やり方は簡単で、build/webpack.prod.conf.js 内の SWPrecacheWebpackPlugin に、
importScripts: ['path/to/your.js'] を書き、自前で用意した service worker 用の js ファイルを import します。

サンプル

build/webpack.prod.conf.js
...
    // service worker caching
    new SWPrecacheWebpackPlugin({
      cacheId: 'test',
      filename: 'service-worker.js',
      staticFileGlobs: ['dist/**/*.{js,html,css}'],
      minify: true,
      stripPrefix: 'dist/',
      importScripts: ['/static/mysw.js'] // 自前の service worker スクリプトをインポート
    })
...
static/mysw.js
self.addEventListener('fetch', function(event) {
  console.log('fetch url : ' + event.request.url);
});
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2