precache all static assets in service worker with Workbox

This commit is contained in:
Jesse D. McDonald 2020-04-27 00:55:05 -05:00
parent 156704f0f3
commit 33d1c84fe7
4 changed files with 1741 additions and 9 deletions

1720
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -29,7 +29,11 @@
"svgo-loader": "^2.2.1",
"webpack": "^4.42.1",
"webpack-cli": "^3.3.11",
"webpack-jquery-ui": "^2.0.1"
"webpack-jquery-ui": "^2.0.1",
"workbox-precaching": "^5.1.3",
"workbox-routing": "^5.1.3",
"workbox-strategies": "^5.1.3",
"workbox-webpack-plugin": "^5.1.3"
},
"dependencies": {}
}

15
sw.js
View File

@ -1,7 +1,10 @@
self.addEventListener('fetch', function(event) {
event.respondWith(
fetch(event.request).catch(function() {
return caches.match(event.request);
})
import {precacheAndRoute} from 'workbox-precaching';
import {registerRoute} from 'workbox-routing';
import {StaleWhileRevalidate} from 'workbox-strategies';
precacheAndRoute(self.__WB_MANIFEST);
registerRoute(
new RegExp('^https://fonts\\.googleapis\\.com/|https://fonts\\.gstatic\\.com/'),
new StaleWhileRevalidate()
);
});

View File

@ -4,6 +4,7 @@ const CopyPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { InjectManifest } = require('workbox-webpack-plugin');
module.exports = {
mode: 'production',
@ -82,8 +83,12 @@ module.exports = {
}),
new CopyPlugin([
{ from: '.htaccess' },
{ from: 'sw.js' },
]),
new InjectManifest({
swSrc: './sw.js',
dontCacheBustURLsMatching: /\.[0-9a-f]{16,}\.(css|js|svg|png|jpg|wav)$/,
exclude: [ /(^|[/\\])\.htaccess$/ ],
}),
],
optimization: {
moduleIds: 'hashed',