-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
re.places.algolia.js
47 lines (42 loc) · 1.65 KB
/
re.places.algolia.js
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
if (self.WorkerGlobalScope) {
const swUrl = 'https://cdn.jsdelivr.net/npm/re.places.js@0.1.3/src/re.places.algolia.sw.js';
self.importScripts(swUrl);
} else {
// places isn't loaded .. worker not required
if (!window.places || !window.places.version) {
throw new Error('[Algolia Places.js must be imported before re:places.js]');
}
if(!('serviceWorker' in navigator)) {
throw new Error('[error creating service worker]');
}
// include the name of the file you are currently looking at here
// we will check if it has been correctly registered
let locationOfThisFile;
try {
const [url] = new Error().stack.match(/https.*?\.js(?=:)/) || '';
locationOfThisFile = new URL(url).pathname || 're.places.algolia.js';
} catch (e) {
locationOfThisFile = 're.places.algolia.js';
}
const initAlgoliaServiceWorker = () =>
navigator.serviceWorker
.register(locationOfThisFile)
.then((registration) => {
registration.update()
registration.addEventListener('updatefound', () => {
// testing purposes
// registration.installing.onstatechange = (e) =>
// console.log('state change!', e.target.state);
});
});
navigator.serviceWorker.getRegistrations().then((registrations) => {
for (const registration of registrations) {
const scriptURL = registration.active && registration.active.scriptURL;
if (scriptURL && !scriptURL.endsWith(locationOfThisFile)) {
throw new Error('[could not install re:places service worker: worker already exists at this scope]');
}
}
initAlgoliaServiceWorker();
// we're not catching errors - we want them to throw
});
}