forked from itwanger/toBeBetterJavaer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch-service-worker.js
More file actions
40 lines (30 loc) · 1.2 KB
/
Copy pathpatch-service-worker.js
File metadata and controls
40 lines (30 loc) · 1.2 KB
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
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const targetPath = process.argv[2];
if (!targetPath) {
console.error("Usage: patch-service-worker.js <service-worker-path>");
process.exit(1);
}
const resolvedPath = path.resolve(targetPath);
if (!fs.existsSync(resolvedPath)) {
console.error(`Service worker not found: ${resolvedPath}`);
process.exit(1);
}
const source = fs.readFileSync(resolvedPath, "utf8");
const marker = "self.__JAVABETTER_PWA_REFRESH_FIX__=true,";
if (source.includes(marker)) {
process.exit(0);
}
const needle = '"use strict";';
const injection =
'"use strict";' +
"self.__JAVABETTER_PWA_REFRESH_FIX__=true," +
'self.addEventListener("install",(event=>{event.waitUntil(self.skipWaiting())})),' +
'self.addEventListener("activate",(event=>{event.waitUntil((async()=>{const clients=await self.clients.matchAll({type:"window",includeUncontrolled:true});for(const client of clients)try{await client.navigate(client.url)}catch{}})())})),';
if (!source.includes(needle)) {
console.error(`Could not find injection point in: ${resolvedPath}`);
process.exit(1);
}
const patched = source.replace(needle, injection);
fs.writeFileSync(resolvedPath, patched);