This repository has been archived by the owner on Jun 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fix-sw.js
75 lines (63 loc) · 2.39 KB
/
fix-sw.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
*
* Temporary fix for https://github.com/angular/angular/issues/21636
*
* Include in your project; run after the `ng build --prod` step by running `node location/of/this/script/fix-sw`
*
* Warning: removes any instances of setting EXISTING_CLIENTS_ONLY state, which will likely have adverse effects in some situations
*
*/
/*
* enter the value set as the --base-href flag when deploying to a live URL
* if not deploying to a live URL, and only running locally, set this to null, as the URL parsing fix will not be necessary
*/
const SITE_BASE_HREF = 'https://devoxx2018-pwa.firebaseapp.com/';
/*
*
* Begin script
*
*/
const replace = require('replace-in-file');
const existingStateReplacements = {
files: 'dist/ngsw-worker.js',
from: /this\.state = DriverReadyState\.EXISTING_CLIENTS_ONLY;/g,
to: '/*this.state = DriverReadyState.EXISTING_CLIENTS_ONLY;*/ ' +
'// removing EXISTING_CLIENTS_ONLY state, as it behaves incorrectly in offline testing, both locally & on GitHub pages'
}
const baseHrefInstances = {
files: 'dist/ngsw.json',
from: '"' + SITE_BASE_HREF + 'index.html",',
to: '"' + SITE_BASE_HREF + 'index.html", ', // whitespace-only change indicates that the baseHref was found, so we should make the URL fix
};
const serviceWorkerURLFix = {
files: 'dist/ngsw-worker.js',
from: /return parsed\.path;/g,
to: '/*return parsed.path;*/ ' +
'return url; ' +
'// overriding default @angular/service-worker URL behavior, to handle routing bug angular/angular #21636'
}
try {
const existingInstances = replace.sync(existingStateReplacements);
console.log('Replacements of EXISTING_CLIENTS_ONLY states: ', existingInstances.join(', '));
}
catch(error) {
console.error('Error occurred while replacing EXISTING_CLIENTS_ONLY states: ', error);
}
try {
const foundBaseHref = replace.sync(baseHrefInstances);
if (foundBaseHref && foundBaseHref.length > 0) {
try {
const override = replace.sync(serviceWorkerURLFix);
console.log('Changes made: ', override.join(', '))
}
catch (error) {
console.error('Error occurred while overriding default service worker URL behavior: ', error)
}
}
else {
console.log('baseHref was not set; no URL matching changes needed', foundBaseHref);
}
}
catch (error) {
console.error('Error occurred while looking for baseHref: ', error);
}