diff --git a/build/load-minified.js b/build/load-minified.js new file mode 100644 index 00000000..aeba1774 --- /dev/null +++ b/build/load-minified.js @@ -0,0 +1,11 @@ +'use strict' + +const fs = require('fs') +const UglifyJS = require('uglify-es') + +module.exports = function(filePath) { + const code = fs.readFileSync(filePath, 'utf-8') + const result = UglifyJS.minify(code) + if (result.error) return '' + return result.code +} diff --git a/build/service-worker-prod.js b/build/service-worker-prod.js index 13d94ce3..7c9d1ff3 100644 --- a/build/service-worker-prod.js +++ b/build/service-worker-prod.js @@ -5,18 +5,18 @@ // and that the current page is accessed from a secure origin. Using a // service worker from an insecure origin will trigger JS console errors. const isLocalhost = Boolean(window.location.hostname === 'localhost' || - // [::1] is the IPv6 localhost address. - window.location.hostname === '[::1]' || - // 127.0.0.1/8 is considered localhost for IPv4. - window.location.hostname.match( - /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ - ) - ); + // [::1] is the IPv6 localhost address. + window.location.hostname === '[::1]' || + // 127.0.0.1/8 is considered localhost for IPv4. + window.location.hostname.match( + /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ + ) + ); window.addEventListener('load', function() { - if ('serviceWorker' in navigator && - (window.location.protocol === 'https:' || isLocalhost)) { - navigator.serviceWorker.register('service-worker.js') + if ('serviceWorker' in navigator && + (window.location.protocol === 'https:' || isLocalhost)) { + navigator.serviceWorker.register('/service-worker.js') .then(function(registration) { // updatefound is fired if service-worker.js changes. registration.onupdatefound = function() { @@ -39,7 +39,7 @@ case 'redundant': throw new Error('The installing ' + - 'service worker became redundant.'); + 'service worker became redundant.'); default: // Ignore @@ -50,6 +50,6 @@ }).catch(function(e) { console.error('Error during service worker registration:', e); }); - } + } }); })(); \ No newline at end of file diff --git a/build/webpack.prod.conf.js b/build/webpack.prod.conf.js index 5181842d..67131c53 100644 --- a/build/webpack.prod.conf.js +++ b/build/webpack.prod.conf.js @@ -11,6 +11,9 @@ const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin') const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin') const LodashModuleReplacementPlugin = require('lodash-webpack-plugin') const ParallelUglifyPlugin = require('webpack-parallel-uglify-plugin') +const loadMinified = require('./load-minified') + +const PUBLIC_PATH = 'https://nicelinks.site/' const env = process.env.NODE_ENV === 'testing' ? require('../config/test.env') @@ -85,7 +88,7 @@ const webpackConfig = merge(baseWebpackConfig, { }, // necessary to consistently work with multiple chunks via CommonsChunkPlugin chunksSortMode: 'dependency', - serviceWorkerLoader: `` }), // split vendor js into its own file @@ -110,10 +113,11 @@ const webpackConfig = merge(baseWebpackConfig, { }), // service worker caching new SWPrecacheWebpackPlugin({ - cacheId: 'my-vue-app', + cacheId: 'nicelinksapp', filename: 'service-worker.js', staticFileGlobs: ['dist/**/*.{js,html,css}'], minify: true, + navigateFallback: PUBLIC_PATH + 'index.html', stripPrefix: 'dist/' }), new LodashModuleReplacementPlugin() diff --git a/index.ejs b/index.ejs index 93f80246..3329a531 100644 --- a/index.ejs +++ b/index.ejs @@ -43,15 +43,16 @@
- + <%= htmlWebpackPlugin.options.serviceWorkerLoader %> + -