Skip to content
This repository was archived by the owner on Feb 12, 2022. It is now read-only.

Commit 1e106bc

Browse files
committed
Add app update notifier #12
1 parent d05a268 commit 1e106bc

File tree

10 files changed

+180
-23
lines changed

10 files changed

+180
-23
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ludo",
3-
"version": "v1.2.3",
3+
"version": "v1.3.2",
44
"private": true,
55
"scripts": {
66
"start": "yarn serve",
@@ -17,6 +17,7 @@
1717
"mersenne-twister": "1.1.0",
1818
"register-service-worker": "1.6.2",
1919
"sass-rem": "2.0.1",
20+
"toastify-js": "https://github.com/mort3za/toastify-js.git",
2021
"vue": "2.6.10",
2122
"vue-class-component": "7.0.2",
2223
"vue-property-decorator": "8.1.0",

src/App.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ body,
1616

1717
<template>
1818
<div id="app">
19-
<router-view/>
19+
<router-view />
2020
</div>
2121
</template>
2222

2323
<script>
24-
import Navigation from "@/components/Navigation.vue";
25-
export default {
26-
name: "app",
27-
};
24+
import { Component, Vue } from "vue-property-decorator";
25+
26+
@Component
27+
export default class App extends Vue {}
2828
</script>

src/functions/general-helpers.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// @ts-ignore
2+
import Toastify from "toastify-js";
3+
14
import {
25
PositionInBoard,
36
Marble,
@@ -12,6 +15,27 @@ import {
1215
import { getPositionAfterMove } from "@/functions/path-helpers.ts";
1316
import store from "@/store/index";
1417

18+
export function notify(options: any = {}) {
19+
if (!options.text) {
20+
console.log('No text');
21+
return;
22+
}
23+
Toastify({
24+
className: "shadow",
25+
text: "",
26+
duration: 600000,
27+
// destination: undefined,
28+
// newWindow: false,
29+
// onClick: () => {}
30+
close: false,
31+
gravity: "bottom", // `top` or `bottom`
32+
position: "left", // `left`, `center` or `right`
33+
backgroundColor: "white",
34+
stopOnFocus: true,
35+
...options
36+
}).showToast();
37+
}
38+
1539
// TODO: rename to isSamePosition
1640
export function isSameStep(position1: PositionInBoard, position2: PositionInBoard): boolean {
1741
return position1.row === position2.row && position1.column === position2.column;

src/registerServiceWorker.ts

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,55 @@
11
/* tslint:disable:no-console */
22

3-
import { register } from 'register-service-worker';
3+
import { register } from "register-service-worker";
4+
import { notify } from "./functions/general-helpers";
45

5-
if (process.env.NODE_ENV === 'production') {
6+
const notifyUserAboutUpdate = (worker: any) => {
7+
notify({
8+
text: "New version available 🥳, Click here to run the new version!",
9+
close: false,
10+
onClick: () => {
11+
console.log("upgrade confirmed");
12+
worker.postMessage({ action: "skipWaiting" });
13+
}
14+
});
15+
};
16+
17+
if (process.env.NODE_ENV === "production") {
618
register(`${process.env.BASE_URL}service-worker.js`, {
719
ready() {
820
console.log(
9-
'App is being served from cache by a service worker.\n' +
10-
'For more details, visit https://goo.gl/AFskqB',
21+
"App is being served from cache by a service worker.\n" +
22+
"For more details, visit https://goo.gl/AFskqB"
1123
);
1224
},
1325
registered() {
14-
console.log('Service worker has been registered.');
26+
console.log("Service worker has been registered.");
1527
},
1628
cached() {
17-
console.log('Content has been cached for offline use.');
29+
console.log("Content has been cached for offline use.");
1830
},
1931
updatefound() {
20-
console.log('New content is downloading.');
32+
console.log("New content is downloading.");
2133
},
22-
updated() {
23-
console.log('New content is available; please refresh.');
34+
updated(registration) {
35+
console.log("New content is available; please refresh.");
36+
notifyUserAboutUpdate(registration.waiting);
2437
},
2538
offline() {
26-
console.log('No internet connection found. App is running in offline mode.');
39+
console.log("No internet connection found. App is running in offline mode.");
2740
},
2841
error(error) {
29-
console.error('Error during service worker registration:', error);
30-
},
42+
console.error("Error during service worker registration:", error);
43+
}
44+
});
45+
46+
let refreshing: any;
47+
navigator.serviceWorker.addEventListener("controllerchange", () => {
48+
console.log('controllerchange...');
49+
if (refreshing) {
50+
return;
51+
}
52+
window.location.reload();
53+
refreshing = true;
3154
});
3255
}

src/service-worker.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* The workboxSW.precacheAndRoute() method efficiently caches and responds to
3+
* requests for URLs in the manifest.
4+
* See https://goo.gl/S9QRab
5+
*/
6+
self.__precacheManifest = [].concat(self.__precacheManifest || []);
7+
workbox.precaching.suppressWarnings();
8+
workbox.precaching.precacheAndRoute(self.__precacheManifest, {});
9+
10+
self.addEventListener("message", msg => {
11+
if (msg.data.action === "skipWaiting") {
12+
console.log('skip waiting...');
13+
self.skipWaiting();
14+
}
15+
});

src/styles/global.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@import "./toastify.scss";
12
@import "./global-ghost.scss";
23
@import "./bootstrap.scss";
34
@import "./animations.scss";

src/styles/toastify.scss

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*!
2+
* Toastify js 1.6.1
3+
* https://github.com/apvarun/toastify-js
4+
* @license MIT licensed
5+
*
6+
* Copyright (C) 2018 Varun A P
7+
*/
8+
9+
.toastify {
10+
padding: rem(12px 20px);
11+
color: $dark;
12+
display: inline-block;
13+
background: white;
14+
position: fixed;
15+
opacity: 0;
16+
transition: all 300ms cubic-bezier(0.215, 0.61, 0.355, 1);
17+
border-radius: $border-radius;
18+
cursor: pointer;
19+
text-decoration: none;
20+
max-width: calc(50% - 20px);
21+
z-index: 5000;
22+
}
23+
24+
.toastify.on {
25+
opacity: 1;
26+
}
27+
28+
.toast-close {
29+
opacity: 0.4;
30+
}
31+
32+
.toastify-right {
33+
right: rem(16px);
34+
.toast-close {
35+
padding: rem(0 0 0 16px);
36+
}
37+
}
38+
39+
.toastify-left {
40+
left: rem(16px);
41+
.toast-close {
42+
padding: rem(0 16px 0 0);
43+
}
44+
}
45+
46+
.toastify-top {
47+
top: rem(-150px);
48+
}
49+
50+
.toastify-bottom {
51+
bottom: rem(-150px);
52+
}
53+
54+
// .toastify-rounded {
55+
// border-radius: 25px;
56+
// }
57+
58+
// .toastify-avatar {
59+
// width: 1.5em;
60+
// height: 1.5em;
61+
// margin: 0 5px;
62+
// border-radius: 2px;
63+
// }
64+
65+
.toastify-center {
66+
margin-left: auto;
67+
margin-right: auto;
68+
left: 0;
69+
right: 0;
70+
max-width: fit-content;
71+
}
72+
73+
@media only screen and (max-width: 360px) {
74+
.toastify-right,
75+
.toastify-left {
76+
margin-left: auto;
77+
margin-right: auto;
78+
left: 0;
79+
right: 0;
80+
max-width: fit-content;
81+
}
82+
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@
3737
"exclude": [
3838
"node_modules"
3939
]
40-
}
40+
}

vue.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ module.exports = {
55
productionSourceMap: false,
66
publicPath: process.env.NODE_ENV === "production" ? "/ludo/" : "/",
77
pwa: {
8+
// service worker guides:
9+
// https://levelup.gitconnected.com/vue-pwa-example-298a8ea953c9
10+
// https://redfin.engineering/how-to-fix-the-refresh-button-when-using-service-workers-a8e27af6df68
11+
workboxPluginMode: 'InjectManifest',
12+
workboxOptions: {
13+
swSrc: 'src/service-worker.js'
14+
},
815
name: "Ludo Game",
916
themeColor: "#00ffbb",
1017
msTileColor: "#2c3939",

yarn.lock

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8702,6 +8702,10 @@ to-regex@^3.0.1, to-regex@^3.0.2:
87028702
regex-not "^1.0.2"
87038703
safe-regex "^1.1.0"
87048704

8705+
"toastify-js@https://github.com/mort3za/toastify-js.git":
8706+
version "1.6.1"
8707+
resolved "https://github.com/mort3za/toastify-js.git#39974891c867e58bf476004bd8f31f25d209a477"
8708+
87058709
87068710
version "1.0.0"
87078711
resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.0.tgz#7e1be3470f1e77948bc43d94a3c8f4d7752ba553"
@@ -8872,10 +8876,10 @@ typedarray@^0.0.6:
88728876
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
88738877
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
88748878

8875-
typescript@3.4.3:
8876-
version "3.4.3"
8877-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.4.3.tgz#0eb320e4ace9b10eadf5bc6103286b0f8b7c224f"
8878-
integrity sha512-FFgHdPt4T/duxx6Ndf7hwgMZZjZpB+U0nMNGVCYPq0rEzWKjEDobm4J6yb3CS7naZ0yURFqdw9Gwc7UOh/P9oQ==
8879+
typescript@3.5.3:
8880+
version "3.5.3"
8881+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.5.3.tgz#c830f657f93f1ea846819e929092f5fe5983e977"
8882+
integrity sha512-ACzBtm/PhXBDId6a6sDJfroT2pOWt/oOnk4/dElG5G33ZL776N3Y6/6bKZJBFpd+b05F3Ct9qDjMeJmRWtE2/g==
88798883

88808884
88818885
version "3.4.10"

0 commit comments

Comments
 (0)