Skip to content

Commit 829ee78

Browse files
committed
Enabled workbox replacing SW-Precache
1 parent e20ea70 commit 829ee78

8 files changed

+819
-970
lines changed

package-lock.json

+694-946
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"@pwa/cli": "latest",
3030
"@pwa/plugin-brotli": "latest",
3131
"@pwa/plugin-gzip": "latest",
32-
"@pwa/plugin-sw-precache": "latest",
3332
"@pwa/preset-vue": "latest",
3433
"babel-cli": "^6.26.0",
3534
"babel-preset-env": "^1.7.0",
@@ -39,7 +38,8 @@
3938
"stylus": "^0.54.0",
4039
"stylus-loader": "^3.0.0",
4140
"ts-loader": "^5.3.3",
42-
"typescript": "^3.3.1"
41+
"typescript": "^3.3.1",
42+
"workbox-webpack-plugin": "^5.1.2"
4343
},
4444
"browserslist": [
4545
">0.25%",

pwa.config.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1+
const { InjectManifest } = require("workbox-webpack-plugin");
12
const { join } = require("path");
23

34
exports.webpack = function(config, env) {
5+
// Add module aliases
46
const src = config.context;
57
config.resolve.alias["@styles"] = join(src, "styles");
68
config.resolve.alias["@js"] = join(src, "js");
79
config.resolve.alias["@store"] = join(src, "store");
810
config.devServer.host = "0.0.0.0";
11+
12+
// Add Workbox plugin
13+
config.plugins.push(
14+
new InjectManifest({
15+
swSrc: join(src, "sw.js"),
16+
})
17+
);
918
};
1019

1120
exports.brotli = {
@@ -17,6 +26,6 @@ exports.brotli = {
1726
size_hint: 0,
1827
lgblock: 0,
1928
lgwin: 22,
20-
mode: 0
21-
}
29+
mode: 0,
30+
},
2231
};

src/index.js

+16-17
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,28 @@ import App from "./App";
1414
import router from "./router";
1515
import filters from "./plugins/filters";
1616
import store from "./store";
17+
import wb from "./registerServiceWorker";
1718

1819
import "./styles/index.styl";
1920

2021
Vue.config.productionTip = false;
21-
const render = h => h(App);
22+
const render = (h) => h(App);
2223

2324
Vue.use(Toasted, {
2425
position: "bottom-center",
2526
duration: 3000,
2627
iconPack: "fontawesome",
2728
action: {
2829
icon: "times",
29-
onClick: (e, toastObject) => toastObject.goAway(0)
30+
onClick: (e, toastObject) => toastObject.goAway(0),
3031
},
3132
fitToScreen: true,
32-
singleton: true
33+
singleton: true,
3334
});
3435

3536
Vue.toasted.register(
3637
"error_post",
37-
payload => {
38+
(payload) => {
3839
const msg = payload.message || "Something went wrong!";
3940
return `Oops... ${msg}`;
4041
},
@@ -43,7 +44,7 @@ Vue.toasted.register(
4344

4445
Vue.toasted.register(
4546
"success",
46-
payload => {
47+
(payload) => {
4748
const msg = payload.message || "Success!";
4849
return `Yay! ${msg}`;
4950
},
@@ -60,21 +61,21 @@ Vue.use(vueMq, {
6061
md: 991,
6162
lg: 1367,
6263
xl: 1920,
63-
xxl: Infinity
64-
}
64+
xxl: Infinity,
65+
},
6566
});
6667

6768
Vue.use(scrollSpy, {
68-
easing: Easing.Cubic.In
69+
easing: Easing.Cubic.In,
6970
});
7071

7172
Vue.use(VueYoutube);
7273

7374
Vue.use(VueReCaptcha, {
7475
siteKey: "6LepxJ0UAAAAAMGBO1-1PxqQ_Y3TuJGt5DHp5cHk",
7576
loaderOptions: {
76-
useRecaptchaNet: true
77-
}
77+
useRecaptchaNet: true,
78+
},
7879
});
7980

8081
Vue.use(filters);
@@ -85,31 +86,29 @@ Vue.use(filters);
8586
const app = new Vue({
8687
router,
8788
store,
88-
render
89+
render,
8990
}).$mount("#app", true);
9091

9192
if (process.env.NODE_ENV === "production") {
9293
window.ga = new GAnalytics("UA-72222745-1");
9394

94-
router.afterEach(nxt => {
95+
router.afterEach((nxt) => {
9596
ga.send("pageview", {
96-
dp: nxt.path
97+
dp: nxt.path,
9798
});
9899
});
99100
}
100101

101102
// Service Worker registration
102-
if ("serviceWorker" in navigator) {
103-
navigator.serviceWorker.register("./sw.js");
104-
}
103+
Vue.prototype.$workbox = wb;
105104

106105
const config = {
107106
apiKey: "AIzaSyDgx3hMDrBTQ6ci9hKg0MMmbR36rBaH6Bo",
108107
authDomain: "codefest19.firebaseapp.com",
109108
databaseURL: "https://codefest19.firebaseio.com",
110109
projectId: "codefest19",
111110
storageBucket: "codefest19.appspot.com",
112-
messagingSenderId: "800543243585"
111+
messagingSenderId: "800543243585",
113112
};
114113

115114
firebase.initializeApp(config);

src/registerServiceWorker.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Workbox } from "workbox-window";
2+
3+
let wb;
4+
5+
if ("serviceWorker" in navigator) {
6+
wb = new Workbox(`/sw.js`);
7+
8+
wb.addEventListener("controlling", () => {
9+
window.location.reload();
10+
});
11+
12+
wb.register();
13+
} else {
14+
wb = null;
15+
}
16+
17+
export default wb;

src/sw.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
self.addEventListener("install", event => {});
1+
importScripts("./workbox-v4.3.1.js");
2+
3+
if (workbox) {
4+
workbox.precaching.precacheAndRoute(self.__WB_MANIFEST);
5+
} else {
6+
console.warn("Boo! Workbox did not load!");
7+
}

src/workbox-v4.3.1.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
!(function() {
2+
"use strict";
3+
try {
4+
self["workbox:sw:4.3.1"] && _();
5+
} catch (t) {}
6+
const t = "https://storage.googleapis.com/workbox-cdn/releases/4.3.1",
7+
e = {
8+
backgroundSync: "background-sync",
9+
broadcastUpdate: "broadcast-update",
10+
cacheableResponse: "cacheable-response",
11+
core: "core",
12+
expiration: "expiration",
13+
googleAnalytics: "offline-ga",
14+
navigationPreload: "navigation-preload",
15+
precaching: "precaching",
16+
rangeRequests: "range-requests",
17+
routing: "routing",
18+
strategies: "strategies",
19+
streams: "streams",
20+
};
21+
self.workbox = new (class {
22+
constructor() {
23+
return (
24+
(this.v = {}),
25+
(this.t = {
26+
debug: "localhost" === self.location.hostname,
27+
modulePathPrefix: null,
28+
modulePathCb: null,
29+
}),
30+
(this.s = this.t.debug ? "dev" : "prod"),
31+
(this.o = !1),
32+
new Proxy(this, {
33+
get(t, s) {
34+
if (t[s]) return t[s];
35+
const o = e[s];
36+
return o && t.loadModule(`workbox-${o}`), t[s];
37+
},
38+
})
39+
);
40+
}
41+
setConfig(t = {}) {
42+
if (this.o)
43+
throw new Error(
44+
"Config must be set before accessing workbox.* modules"
45+
);
46+
Object.assign(this.t, t), (this.s = this.t.debug ? "dev" : "prod");
47+
}
48+
loadModule(t) {
49+
const e = this.i(t);
50+
try {
51+
importScripts(e), (this.o = !0);
52+
} catch (s) {
53+
throw (console.error(`Unable to import module '${t}' from '${e}'.`), s);
54+
}
55+
}
56+
i(e) {
57+
if (this.t.modulePathCb) return this.t.modulePathCb(e, this.t.debug);
58+
let s = [t];
59+
const o = `${e}.${this.s}.js`,
60+
r = this.t.modulePathPrefix;
61+
return (
62+
r &&
63+
"" === (s = r.split("/"))[s.length - 1] &&
64+
s.splice(s.length - 1, 1),
65+
s.push(o),
66+
s.join("/")
67+
);
68+
}
69+
})();
70+
})();

vue.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
22
productionSourceMap: false,
33
devServer: {
4-
host: "0.0.0.0"
5-
}
4+
host: "0.0.0.0",
5+
},
66
};

0 commit comments

Comments
 (0)