forked from SilverHoodCorp/polar-bookshelf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workbox-config.js
161 lines (116 loc) · 3.68 KB
/
workbox-config.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const fs = require('fs');
const libpath = require('path');
const globDirectory = 'dist/public';
const STATIC_FILE_EXTENSIONS = ["css", "html", "png", "svg", "ico", "woff2"];
const JAVASCRIPT_AND_STATIC_FILE_EXTENSIONS = ["js", ...STATIC_FILE_EXTENSIONS];
function recurse(dir) {
const files = fs.readdirSync(dir);
const result = [];
for (const file of files) {
const path = libpath.join(dir, file);
const stat = fs.statSync(path);
if (stat.isDirectory()) {
result.push(...recurse(path));
}
if (stat.isFile()) {
result.push(path);
}
}
return result;
}
function toExtension(path) {
if (! path) {
return undefined;
}
const matches = path.match(/\.([a-z0-9]{1,15})$/);
if (matches && matches.length === 2) {
return matches[1];
}
return undefined;
}
/**
* Return true if the path has one of the given extensions.
*/
function hasExtension(path, exts) {
const ext = toExtension(path);
if (! ext) {
return false;
}
return exts.includes(ext);
}
function stripDirPrefix(path, prefix) {
if (! prefix.endsWith(libpath.sep)) {
prefix = prefix + libpath.sep
}
if (path.startsWith(prefix)) {
return path.substring(prefix.length);
}
return path;
}
function createGlobsRecursively(path, exts) {
if (! exts) {
exts = STATIC_FILE_EXTENSIONS;
}
return recurse(libpath.join(globDirectory, path))
.filter(current => hasExtension(current, exts))
.map(current => stripDirPrefix(current, globDirectory));
}
const globPatterns = [
...createGlobsRecursively('apps', STATIC_FILE_EXTENSIONS),
...createGlobsRecursively('pdfviewer-custom', JAVASCRIPT_AND_STATIC_FILE_EXTENSIONS),
...createGlobsRecursively('web/dist', JAVASCRIPT_AND_STATIC_FILE_EXTENSIONS),
...createGlobsRecursively('web/assets', JAVASCRIPT_AND_STATIC_FILE_EXTENSIONS),
'icon.ico',
'icon.png',
'icon.svg',
'index.html',
'manifest.json',
'apps/init.js',
'apps/service-worker-registration.js',
];
console.log("Using static file globs: \n ", globPatterns.join("\n "));
console.log("====");
// https://stackoverflow.com/questions/49482680/workbox-the-danger-of-self-skipwaiting
// https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-build#.generateSW
module.exports = {
globDirectory: 'dist/public',
skipWaiting: true,
globPatterns,
globIgnores: [],
globStrict: false,
// stripPrefix: 'dist/public',
maximumFileSizeToCacheInBytes: 150000000,
swDest: 'dist/public/service-worker.js',
runtimeCaching: [
{
// https://js.stripe.com/v3
urlPattern: /https:\/\/js\.stripe\.com\/v3/,
handler: 'staleWhileRevalidate'
}
],
// {
// urlPattern: /.*/,
// handler: 'staleWhileRevalidate'
// },
// {
// // these URLs are immutable based on content hash as computed by
// // webpack so just use cacheFirst which only fetches them the
// // first time
// urlPattern: /https:\/\/storage.google.com\/stash/,
// handler: 'CacheFirst'
// }
// ],
// runtimeCaching: [
// {
// // these URLs are immutable based on content hash as computed by
// // webpack so just use cacheFirst which only fetches them the
// // first time
// urlPattern: /web\/dist\/images\/.*/,
// handler: 'CacheFirst'
// }
// ],
modifyURLPrefix: {
// Remove a '/dist' prefix from the URLs:
'/dist/public': ''
}
};