From e7677d8c317aa4c189dfe563a2ec5e279d53d691 Mon Sep 17 00:00:00 2001 From: Alexander Lichter Date: Sun, 22 Jul 2018 12:50:17 +0200 Subject: [PATCH] feat(workbox): disable caching for sw.js by default --- packages/workbox/index.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/workbox/index.js b/packages/workbox/index.js index 6353efa2..feefe202 100755 --- a/packages/workbox/index.js +++ b/packages/workbox/index.js @@ -21,6 +21,7 @@ module.exports = function nuxtWorkbox (moduleOptions) { debug('Adding workbox') const options = getOptions.call(this, moduleOptions) workboxInject.call(this, options) + setHeaders.call(this, options) emitAssets.call(this, options) addTemplates.call(this, options) } @@ -178,4 +179,27 @@ function workboxInject (options) { } } +// ============================================= +// setHeaders +// ============================================= + +function setHeaders (options) { + if (options.customHeaders) { + return + } + + const originalSetHeadersMethod = this.options.render.static.setHeaders + + this.options.render.static.setHeaders = (res, path) => { + if (path.match(/sw\.js$/)) { + // Prevent caching service worker + res.setHeader('Cache-Control', 'no-cache') + } else { + if (typeof originalSetHeadersMethod !== 'undefined') { + originalSetHeadersMethod(res, path) + } + } + } +} + module.exports.meta = require('./package.json')