srvx 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/service-worker.mjs +34 -24
- package/package.json +2 -1
|
@@ -2,7 +2,8 @@ import { w as wrapFetch } from '../shared/srvx.DhN4g5wJ.mjs';
|
|
|
2
2
|
import { w as wrapFetchOnError } from '../shared/srvx.BD9sRkkl.mjs';
|
|
3
3
|
|
|
4
4
|
const Response = globalThis.Response;
|
|
5
|
-
const
|
|
5
|
+
const isBrowserWindow = typeof window !== "undefined" && typeof navigator !== "undefined";
|
|
6
|
+
const isServiceWorker = typeof self !== "undefined" && "skipWaiting" in self;
|
|
6
7
|
function serve(options) {
|
|
7
8
|
return new ServiceWorkerServer(options);
|
|
8
9
|
}
|
|
@@ -30,7 +31,12 @@ class ServiceWorkerServer {
|
|
|
30
31
|
#fetchListener;
|
|
31
32
|
#listeningPromise;
|
|
32
33
|
serve() {
|
|
33
|
-
if (
|
|
34
|
+
if (isBrowserWindow) {
|
|
35
|
+
if (!navigator.serviceWorker) {
|
|
36
|
+
throw new Error(
|
|
37
|
+
"Service worker is not supported in the current window."
|
|
38
|
+
);
|
|
39
|
+
}
|
|
34
40
|
const swURL = this.options.serviceWorker?.url;
|
|
35
41
|
if (!swURL) {
|
|
36
42
|
throw new Error(
|
|
@@ -41,28 +47,32 @@ class ServiceWorkerServer {
|
|
|
41
47
|
type: "module",
|
|
42
48
|
scope: this.options.serviceWorker?.scope
|
|
43
49
|
}).then((registration) => {
|
|
44
|
-
registration.
|
|
50
|
+
if (registration.active) {
|
|
45
51
|
location.replace(location.href);
|
|
46
|
-
}
|
|
52
|
+
} else {
|
|
53
|
+
registration.addEventListener("updatefound", () => {
|
|
54
|
+
location.replace(location.href);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
} else if (isServiceWorker) {
|
|
59
|
+
this.#fetchListener = async (event) => {
|
|
60
|
+
if (/\/[^/]*\.[a-zA-Z0-9]+$/.test(new URL(event.request.url).pathname)) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const response = await this.fetch(event.request, event);
|
|
64
|
+
if (response.status !== 404) {
|
|
65
|
+
event.respondWith(response);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
addEventListener("fetch", this.#fetchListener);
|
|
69
|
+
self.addEventListener("install", () => {
|
|
70
|
+
self.skipWaiting();
|
|
71
|
+
});
|
|
72
|
+
self.addEventListener("activate", () => {
|
|
73
|
+
self.clients?.claim?.();
|
|
47
74
|
});
|
|
48
|
-
return;
|
|
49
75
|
}
|
|
50
|
-
this.#fetchListener = async (event) => {
|
|
51
|
-
if (/\/[^/]*\.[a-zA-Z0-9]+$/.test(new URL(event.request.url).pathname)) {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
const response = await this.fetch(event.request, event);
|
|
55
|
-
if (response.status !== 404) {
|
|
56
|
-
event.respondWith(response);
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
addEventListener("fetch", this.#fetchListener);
|
|
60
|
-
globalThis.addEventListener("install", () => {
|
|
61
|
-
globalThis.skipWaiting();
|
|
62
|
-
});
|
|
63
|
-
globalThis.addEventListener("activate", (event) => {
|
|
64
|
-
event.waitUntil(globalThis.clients?.claim?.());
|
|
65
|
-
});
|
|
66
76
|
}
|
|
67
77
|
ready() {
|
|
68
78
|
return Promise.resolve(this.#listeningPromise).then(() => this);
|
|
@@ -71,15 +81,15 @@ class ServiceWorkerServer {
|
|
|
71
81
|
if (this.#fetchListener) {
|
|
72
82
|
removeEventListener("fetch", this.#fetchListener);
|
|
73
83
|
}
|
|
74
|
-
if (
|
|
84
|
+
if (isBrowserWindow) {
|
|
75
85
|
const registrations = await navigator.serviceWorker.getRegistrations();
|
|
76
86
|
for (const registration of registrations) {
|
|
77
87
|
if (registration.active) {
|
|
78
88
|
await registration.unregister();
|
|
79
89
|
}
|
|
80
90
|
}
|
|
81
|
-
} else {
|
|
82
|
-
await
|
|
91
|
+
} else if (isServiceWorker) {
|
|
92
|
+
await self.registration.unregister();
|
|
83
93
|
}
|
|
84
94
|
}
|
|
85
95
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "srvx",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "Universal Server API based on web platform standards. Works seamlessly with Deno, Bun and Node.js.",
|
|
5
5
|
"repository": "h3js/srvx",
|
|
6
6
|
"homepage": "https://srvx.h3.dev",
|
|
@@ -59,6 +59,7 @@
|
|
|
59
59
|
"@types/bun": "^1.2.10",
|
|
60
60
|
"@types/deno": "^2.2.0",
|
|
61
61
|
"@types/node": "^22.14.1",
|
|
62
|
+
"@types/serviceworker": "^0.0.132",
|
|
62
63
|
"@vitest/coverage-v8": "^3.1.2",
|
|
63
64
|
"automd": "^0.4.0",
|
|
64
65
|
"changelogen": "^0.6.1",
|