-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.vue
64 lines (59 loc) · 1.88 KB
/
app.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<template>
<div>
<SplashScreen :src="'/favicon.ico'" :time="1000" width="100px" />
<NuxtLoadingIndicator />
<NuxtPage />
</div>
</template>
<script>
export default {
mounted() {
this.registerServiceWorker();
},
methods: {
/**
* * Register the service worker
* * Load the service worker file and set the scope.
* ? The scope is the path where the service worker will be active.
* ? The scope is relative to the domain.
* ! The scope is not the same as the path to the service worker file.
* TODO: Investigate if there is any usefull function to load in this function.
* TODO: Export this function to a separate file ?
*/
async registerServiceWorker() {
// Define file and scope from root directory
const scope = '.';
const workerPath = 'worker.js';
// Check if service worker is supported and register it
if ('serviceWorker' in navigator) {
try {
/**
* * Register the service worker
* .register() @returns a promise with a ServiceWorkerRegistration object
*/
const registration = await navigator.serviceWorker
.register(workerPath, {
scope: scope,
})
.then((registration) => {
console.log(
'Service worker registration successful with scope: ',
registration.scope
);
return registration;
});
if (registration.installing) {
console.log('Service worker installing');
} else if (registration.waiting) {
console.log('Service worker installed');
} else if (registration.active) {
console.log('Service worker active');
}
} catch (error) {
console.error(`Registration failed with ${error}`);
}
}
},
},
};
</script>