diff --git a/index.html b/index.html index 36587a2..5c76d24 100644 --- a/index.html +++ b/index.html @@ -14,7 +14,7 @@ <meta property="og:type" content="website" /> <meta property="og:url" content="https://webclient.prismarine.js.org" /> <meta property="og:image" content="https://webclient.prismarine.js.org/favicon.png" /> - <link rel="manifest" href="manifest.json"> + <link rel="manifest" href="manifest.json" crossorigin="use-credentials"> </head> <body> <debug-menu id="debugmenu" style="display: none;"></debug-menu> diff --git a/index.js b/index.js index ed98720..a601e27 100644 --- a/index.js +++ b/index.js @@ -18,6 +18,16 @@ const pathfinder = require('mineflayer-pathfinder') const { Vec3 } = require('vec3') global.THREE = require('three') +if ('serviceWorker' in navigator) { + window.addEventListener('load', () => { + navigator.serviceWorker.register('/service-worker.js').then(registration => { + console.log('SW registered: ', registration) + }).catch(registrationError => { + console.log('SW registration failed: ', registrationError) + }) + }) +} + const maxPitch = 0.5 * Math.PI const minPitch = -0.5 * Math.PI diff --git a/package.json b/package.json index 87bd06c..aee8d4d 100644 --- a/package.json +++ b/package.json @@ -70,6 +70,7 @@ "webpack-cli": "^4.2.0", "webpack-dev-middleware": "^4.1.0", "webpack-dev-server": "^3.11.0", - "webpack-merge": "^5.7.3" + "webpack-merge": "^5.7.3", + "workbox-webpack-plugin": "^6.1.2" } } diff --git a/webpack.common.js b/webpack.common.js index 120171b..c968010 100644 --- a/webpack.common.js +++ b/webpack.common.js @@ -2,6 +2,7 @@ const webpack = require('webpack') const path = require('path') const CopyPlugin = require('copy-webpack-plugin') const HtmlWebpackPlugin = require('html-webpack-plugin') +const WorkboxPlugin = require('workbox-webpack-plugin') // https://webpack.js.org/guides/production/ const config = { @@ -57,6 +58,13 @@ const config = { /prismarine-viewer[/|\\]viewer[/|\\]lib[/|\\]utils/, './utils.web.js' ), + new WorkboxPlugin.GenerateSW({ + // these options encourage the ServiceWorkers to get in there fast + // and not allow any straggling "old" SWs to hang around + clientsClaim: true, + skipWaiting: true, + include: ['index.html', 'manifest.json'] // not caching a lot as anyway this works only online + }), new CopyPlugin({ patterns: [ { from: path.join(__dirname, '/styles.css'), to: './styles.css' },