add service worker for PWA

This commit is contained in:
Romain Beaumont 2021-03-21 01:54:10 +00:00
parent 9b8481e4a7
commit c40b00ab5c
4 changed files with 21 additions and 2 deletions

View file

@ -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>

View file

@ -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

View file

@ -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"
}
}

View file

@ -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' },