@qwik.dev/router 2.0.0-alpha.1 → 2.0.0-alpha.10
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/lib/adapters/azure-swa/vite/index.cjs +3 -3
- package/lib/adapters/azure-swa/vite/index.d.ts +13 -13
- package/lib/adapters/bun-server/vite/index.cjs +3 -3
- package/lib/adapters/bun-server/vite/index.d.ts +14 -14
- package/lib/adapters/cloud-run/vite/index.cjs +3 -3
- package/lib/adapters/cloud-run/vite/index.d.ts +13 -13
- package/lib/adapters/cloudflare-pages/vite/index.cjs +3 -3
- package/lib/adapters/cloudflare-pages/vite/index.d.ts +27 -27
- package/lib/adapters/deno-server/vite/index.cjs +3 -3
- package/lib/adapters/deno-server/vite/index.d.ts +14 -14
- package/lib/adapters/netlify-edge/vite/index.cjs +3 -3
- package/lib/adapters/netlify-edge/vite/index.d.ts +44 -44
- package/lib/adapters/node-server/vite/index.cjs +3 -3
- package/lib/adapters/node-server/vite/index.d.ts +14 -14
- package/lib/adapters/shared/vite/index.cjs +10 -3
- package/lib/adapters/shared/vite/index.d.ts +114 -114
- package/lib/adapters/shared/vite/index.mjs +7 -0
- package/lib/adapters/static/vite/index.cjs +10 -3
- package/lib/adapters/static/vite/index.d.ts +10 -10
- package/lib/adapters/static/vite/index.mjs +7 -0
- package/lib/adapters/vercel-edge/vite/index.cjs +3 -3
- package/lib/adapters/vercel-edge/vite/index.d.ts +45 -45
- package/lib/index.d.ts +878 -801
- package/lib/index.qwik.cjs +84 -84
- package/lib/index.qwik.mjs +64 -64
- package/lib/middleware/aws-lambda/index.d.ts +48 -50
- package/lib/middleware/azure-swa/index.d.ts +28 -28
- package/lib/middleware/bun/index.d.ts +35 -35
- package/lib/middleware/cloudflare-pages/index.d.ts +35 -35
- package/lib/middleware/deno/index.d.ts +47 -47
- package/lib/middleware/firebase/index.d.ts +26 -26
- package/lib/middleware/netlify-edge/index.d.ts +27 -27
- package/lib/middleware/node/index.cjs +3 -3
- package/lib/middleware/node/index.d.ts +64 -66
- package/lib/middleware/request-handler/index.cjs +37 -38
- package/lib/middleware/request-handler/index.d.ts +676 -681
- package/lib/middleware/request-handler/index.mjs +34 -35
- package/lib/middleware/vercel-edge/index.d.ts +26 -26
- package/lib/service-worker.cjs +13 -263
- package/lib/service-worker.d.ts +15 -4
- package/lib/service-worker.mjs +13 -263
- package/lib/static/index.cjs +3 -3
- package/lib/static/index.d.ts +96 -98
- package/lib/static/node.cjs +3 -3
- package/lib/vite/index.cjs +129 -247
- package/lib/vite/index.d.ts +154 -154
- package/lib/vite/index.mjs +127 -245
- package/package.json +10 -9
|
@@ -1,17 +1,11 @@
|
|
|
1
1
|
// packages/qwik-router/src/middleware/request-handler/error-handler.ts
|
|
2
2
|
var ServerError = class extends Error {
|
|
3
3
|
constructor(status, data) {
|
|
4
|
-
super();
|
|
4
|
+
super(typeof data === "string" ? data : void 0);
|
|
5
5
|
this.status = status;
|
|
6
6
|
this.data = data;
|
|
7
7
|
}
|
|
8
8
|
};
|
|
9
|
-
var ErrorResponse = class extends Error {
|
|
10
|
-
constructor(status, message) {
|
|
11
|
-
super(message);
|
|
12
|
-
this.status = status;
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
9
|
function getErrorHtml(status, e) {
|
|
16
10
|
let message = "Server Error";
|
|
17
11
|
if (e != null) {
|
|
@@ -459,8 +453,7 @@ function createCacheControl(cacheControl) {
|
|
|
459
453
|
cacheControl = {
|
|
460
454
|
public: true,
|
|
461
455
|
immutable: true,
|
|
462
|
-
maxAge: 60 * 60 * 24 * 365
|
|
463
|
-
staleWhileRevalidate: 60 * 60 * 24 * 365
|
|
456
|
+
maxAge: 60 * 60 * 24 * 365
|
|
464
457
|
};
|
|
465
458
|
} else if (cacheControl === "no-cache") {
|
|
466
459
|
cacheControl = {
|
|
@@ -470,8 +463,7 @@ function createCacheControl(cacheControl) {
|
|
|
470
463
|
if (typeof cacheControl === "number") {
|
|
471
464
|
cacheControl = {
|
|
472
465
|
maxAge: cacheControl,
|
|
473
|
-
sMaxAge: cacheControl
|
|
474
|
-
staleWhileRevalidate: cacheControl
|
|
466
|
+
sMaxAge: cacheControl
|
|
475
467
|
};
|
|
476
468
|
}
|
|
477
469
|
if (cacheControl.immutable) {
|
|
@@ -516,14 +508,13 @@ import("node:async_hooks").then((module) => {
|
|
|
516
508
|
err
|
|
517
509
|
);
|
|
518
510
|
});
|
|
519
|
-
function runQwikRouter(serverRequestEv, loadedRoute, requestHandlers,
|
|
511
|
+
function runQwikRouter(serverRequestEv, loadedRoute, requestHandlers, trailingSlash = true, basePathname = "/", qwikSerializer) {
|
|
520
512
|
let resolve;
|
|
521
513
|
const responsePromise = new Promise((r) => resolve = r);
|
|
522
514
|
const requestEv = createRequestEvent(
|
|
523
515
|
serverRequestEv,
|
|
524
516
|
loadedRoute,
|
|
525
517
|
requestHandlers,
|
|
526
|
-
manifest,
|
|
527
518
|
trailingSlash,
|
|
528
519
|
basePathname,
|
|
529
520
|
qwikSerializer,
|
|
@@ -542,12 +533,18 @@ async function runNext(requestEv, resolve) {
|
|
|
542
533
|
if (e instanceof RedirectMessage) {
|
|
543
534
|
const stream = requestEv.getWritableStream();
|
|
544
535
|
await stream.close();
|
|
545
|
-
} else if (e instanceof
|
|
546
|
-
console.error(e);
|
|
536
|
+
} else if (e instanceof ServerError) {
|
|
547
537
|
if (!requestEv.headersSent) {
|
|
548
|
-
const html = getErrorHtml(e.status, e);
|
|
549
538
|
const status = e.status;
|
|
550
|
-
requestEv.
|
|
539
|
+
const accept = requestEv.request.headers.get("Accept");
|
|
540
|
+
if (accept && !accept.includes("text/html")) {
|
|
541
|
+
const qwikSerializer = requestEv[RequestEvQwikSerializer];
|
|
542
|
+
requestEv.headers.set("Content-Type", "application/qwik-json");
|
|
543
|
+
requestEv.send(status, await qwikSerializer._serialize([e.data]));
|
|
544
|
+
} else {
|
|
545
|
+
const html = getErrorHtml(e.status, e.data);
|
|
546
|
+
requestEv.html(status, html);
|
|
547
|
+
}
|
|
551
548
|
}
|
|
552
549
|
} else if (!(e instanceof AbortMessage)) {
|
|
553
550
|
if (getRequestMode(requestEv) !== "dev") {
|
|
@@ -600,7 +597,7 @@ var RequestRouteName = "@routeName";
|
|
|
600
597
|
var RequestEvSharedActionId = "@actionId";
|
|
601
598
|
var RequestEvSharedActionFormData = "@actionFormData";
|
|
602
599
|
var RequestEvSharedNonce = "@nonce";
|
|
603
|
-
function createRequestEvent(serverRequestEv, loadedRoute, requestHandlers,
|
|
600
|
+
function createRequestEvent(serverRequestEv, loadedRoute, requestHandlers, trailingSlash, basePathname, qwikSerializer, resolved) {
|
|
604
601
|
const { request, platform, env } = serverRequestEv;
|
|
605
602
|
const sharedMap = /* @__PURE__ */ new Map();
|
|
606
603
|
const cookie = new Cookie(request.headers.get("cookie"));
|
|
@@ -613,7 +610,6 @@ function createRequestEvent(serverRequestEv, loadedRoute, requestHandlers, manif
|
|
|
613
610
|
}
|
|
614
611
|
sharedMap.set(IsQData, true);
|
|
615
612
|
}
|
|
616
|
-
sharedMap.set("@manifest", manifest);
|
|
617
613
|
let routeModuleIndex = -1;
|
|
618
614
|
let writableStream = null;
|
|
619
615
|
let requestData = void 0;
|
|
@@ -647,17 +643,25 @@ function createRequestEvent(serverRequestEv, loadedRoute, requestHandlers, manif
|
|
|
647
643
|
} else {
|
|
648
644
|
status = statusOrResponse.status;
|
|
649
645
|
statusOrResponse.headers.forEach((value, key) => {
|
|
646
|
+
if (key.toLowerCase() === "set-cookie") {
|
|
647
|
+
return;
|
|
648
|
+
}
|
|
650
649
|
headers.append(key, value);
|
|
651
650
|
});
|
|
651
|
+
statusOrResponse.headers.getSetCookie().forEach((ck) => {
|
|
652
|
+
const index = ck.indexOf("=");
|
|
653
|
+
if (index === -1) {
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
656
|
+
const key = ck.slice(0, index).trim();
|
|
657
|
+
const value = ck.slice(index + 1).trim();
|
|
658
|
+
cookie.set(key, value);
|
|
659
|
+
});
|
|
652
660
|
if (statusOrResponse.body) {
|
|
653
661
|
const writableStream2 = requestEv.getWritableStream();
|
|
654
662
|
statusOrResponse.body.pipeTo(writableStream2);
|
|
655
663
|
} else {
|
|
656
|
-
|
|
657
|
-
return new RedirectMessage();
|
|
658
|
-
} else {
|
|
659
|
-
requestEv.getWritableStream().getWriter().close();
|
|
660
|
-
}
|
|
664
|
+
requestEv.getWritableStream().getWriter().close();
|
|
661
665
|
}
|
|
662
666
|
}
|
|
663
667
|
return exit();
|
|
@@ -728,7 +732,7 @@ function createRequestEvent(serverRequestEv, loadedRoute, requestHandlers, manif
|
|
|
728
732
|
},
|
|
729
733
|
error: (statusCode, message) => {
|
|
730
734
|
status = statusCode;
|
|
731
|
-
return new
|
|
735
|
+
return new ServerError(statusCode, message);
|
|
732
736
|
},
|
|
733
737
|
redirect: (statusCode, url2) => {
|
|
734
738
|
check();
|
|
@@ -829,7 +833,7 @@ var parseRequest = async ({ request, method, query }, sharedMap, qwikSerializer)
|
|
|
829
833
|
if (data) {
|
|
830
834
|
try {
|
|
831
835
|
return qwikSerializer._deserialize(decodeURIComponent(data));
|
|
832
|
-
} catch
|
|
836
|
+
} catch {
|
|
833
837
|
}
|
|
834
838
|
}
|
|
835
839
|
}
|
|
@@ -1140,13 +1144,9 @@ async function pureServerFunction(ev) {
|
|
|
1140
1144
|
}
|
|
1141
1145
|
} catch (err) {
|
|
1142
1146
|
if (err instanceof ServerError) {
|
|
1143
|
-
ev.
|
|
1144
|
-
ev.send(err.status, await qwikSerializer._serialize([err.data]));
|
|
1145
|
-
return;
|
|
1147
|
+
throw ev.error(err.status, err.data);
|
|
1146
1148
|
}
|
|
1147
|
-
ev.
|
|
1148
|
-
ev.send(500, await qwikSerializer._serialize([err]));
|
|
1149
|
-
return;
|
|
1149
|
+
throw ev.error(500, "Invalid request");
|
|
1150
1150
|
}
|
|
1151
1151
|
if (isAsyncIterator(result)) {
|
|
1152
1152
|
ev.headers.set("Content-Type", "text/qwik-json-stream");
|
|
@@ -1388,13 +1388,13 @@ async function measure(requestEv, name, fn) {
|
|
|
1388
1388
|
}
|
|
1389
1389
|
function isContentType(headers, ...types) {
|
|
1390
1390
|
var _a;
|
|
1391
|
-
const type = ((_a = headers.get("content-type")) == null ? void 0 : _a.split(
|
|
1391
|
+
const type = ((_a = headers.get("content-type")) == null ? void 0 : _a.split(/;/, 1)[0].trim()) ?? "";
|
|
1392
1392
|
return types.includes(type);
|
|
1393
1393
|
}
|
|
1394
1394
|
|
|
1395
1395
|
// packages/qwik-router/src/middleware/request-handler/request-handler.ts
|
|
1396
1396
|
async function requestHandler(serverRequestEv, opts, qwikSerializer) {
|
|
1397
|
-
const { render, qwikRouterConfig,
|
|
1397
|
+
const { render, qwikRouterConfig, checkOrigin } = opts;
|
|
1398
1398
|
if (!qwikRouterConfig) {
|
|
1399
1399
|
throw new Error("qwikRouterConfig is required.");
|
|
1400
1400
|
}
|
|
@@ -1413,7 +1413,6 @@ async function requestHandler(serverRequestEv, opts, qwikSerializer) {
|
|
|
1413
1413
|
serverRequestEv,
|
|
1414
1414
|
route,
|
|
1415
1415
|
requestHandlers,
|
|
1416
|
-
manifest,
|
|
1417
1416
|
qwikRouterConfig.trailingSlash,
|
|
1418
1417
|
qwikRouterConfig.basePathname,
|
|
1419
1418
|
qwikSerializer
|
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import type { ServerRenderOptions } from '@qwik.dev/router/middleware/request-handler';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @deprecated Use `createQwikRouter` instead. Will be removed in V3
|
|
5
|
-
* @public
|
|
6
|
-
*/
|
|
7
|
-
export declare const createQwikCity: typeof createQwikRouter;
|
|
8
|
-
|
|
9
|
-
/** @public */
|
|
10
|
-
export declare function createQwikRouter(opts: QwikRouterVercelEdgeOptions): (request: Request) => Promise<Response>;
|
|
11
|
-
|
|
12
|
-
/** @public */
|
|
13
|
-
export declare interface PlatformVercel {
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @deprecated Use `QwikRouterVercelEdgeOptions` instead. Will be removed in V3
|
|
18
|
-
* @public
|
|
19
|
-
*/
|
|
20
|
-
export declare type QwikCityVercelEdgeOptions = QwikRouterVercelEdgeOptions;
|
|
21
|
-
|
|
22
|
-
/** @public */
|
|
23
|
-
export declare interface QwikRouterVercelEdgeOptions extends ServerRenderOptions {
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export { }
|
|
1
|
+
import type { ServerRenderOptions } from '@qwik.dev/router/middleware/request-handler';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated Use `createQwikRouter` instead. Will be removed in V3
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
export declare const createQwikCity: typeof createQwikRouter;
|
|
8
|
+
|
|
9
|
+
/** @public */
|
|
10
|
+
export declare function createQwikRouter(opts: QwikRouterVercelEdgeOptions): (request: Request) => Promise<Response>;
|
|
11
|
+
|
|
12
|
+
/** @public */
|
|
13
|
+
export declare interface PlatformVercel {
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @deprecated Use `QwikRouterVercelEdgeOptions` instead. Will be removed in V3
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
export declare type QwikCityVercelEdgeOptions = QwikRouterVercelEdgeOptions;
|
|
21
|
+
|
|
22
|
+
/** @public */
|
|
23
|
+
export declare interface QwikRouterVercelEdgeOptions extends ServerRenderOptions {
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { }
|
package/lib/service-worker.cjs
CHANGED
|
@@ -1,267 +1,17 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const hashes = bundle[2];
|
|
17
|
-
if (hashes) {
|
|
18
|
-
for (const hash of hashes) {
|
|
19
|
-
appSymbols.set(hash, bundle[0]);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return appSymbols;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
const cachedFetch = (cache, fetch, awaitingRequests, request) => new Promise((promiseResolve, promiseReject) => {
|
|
27
|
-
const url = request.url;
|
|
28
|
-
const awaitingRequestResolves = awaitingRequests.get(url);
|
|
29
|
-
if (awaitingRequestResolves) {
|
|
30
|
-
// there's already an active request happening
|
|
31
|
-
// don't start a new request
|
|
32
|
-
awaitingRequestResolves.push([promiseResolve, promiseReject]);
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
// there isn't already an active request for this url
|
|
36
|
-
// start a new request
|
|
37
|
-
const resolve = (response) => {
|
|
38
|
-
// the response has been resolved
|
|
39
|
-
const resolves = awaitingRequests.get(url);
|
|
40
|
-
if (resolves) {
|
|
41
|
-
awaitingRequests.delete(url);
|
|
42
|
-
// loop through each of the active requests
|
|
43
|
-
for (const [awaitingResolve] of resolves) {
|
|
44
|
-
// clone a new response for each of the active requests
|
|
45
|
-
awaitingResolve(response.clone());
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
// somehow the array of awaiting requests doesn't exist
|
|
50
|
-
promiseResolve(response.clone());
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
const reject = (msg) => {
|
|
54
|
-
const resolves = awaitingRequests.get(url);
|
|
55
|
-
if (resolves) {
|
|
56
|
-
awaitingRequests.delete(url);
|
|
57
|
-
for (const [_, awaitingReject] of resolves) {
|
|
58
|
-
awaitingReject(msg);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
promiseReject(msg);
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
// create a new array of the request waiting to be resolved
|
|
66
|
-
awaitingRequests.set(url, [[promiseResolve, promiseReject]]);
|
|
67
|
-
cache
|
|
68
|
-
.match(url)
|
|
69
|
-
.then((cachedResponse) => {
|
|
70
|
-
if (useCache(request, cachedResponse)) {
|
|
71
|
-
// cached response found and user did not specifically send
|
|
72
|
-
// a request header to NOT use the cache (wasn't a hard refresh)
|
|
73
|
-
resolve(cachedResponse);
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
// no cached response found or user didn't want to use the cache
|
|
77
|
-
// do a full network request
|
|
78
|
-
return fetch(request).then(async (networkResponse) => {
|
|
79
|
-
if (networkResponse.ok) {
|
|
80
|
-
// network response was good, let's cache it
|
|
81
|
-
await cache.put(url, networkResponse.clone());
|
|
82
|
-
}
|
|
83
|
-
resolve(networkResponse);
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
})
|
|
87
|
-
.catch((err) => {
|
|
88
|
-
// network error, probably offline
|
|
89
|
-
return cache.match(url).then((cachedResponse) => {
|
|
90
|
-
if (cachedResponse) {
|
|
91
|
-
// luckily we have a cached version, let's use it instead of an offline message
|
|
92
|
-
resolve(cachedResponse);
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
// darn, we've got no connectivity and no cached response
|
|
96
|
-
reject(err);
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
const qBuildCacheName = 'QwikBuild';
|
|
104
|
-
const existingPrefetchUrls = new Set();
|
|
105
|
-
const awaitingRequests = new Map();
|
|
106
|
-
const prefetchQueue = [];
|
|
107
|
-
|
|
108
|
-
const prefetchBundleNames = (appBundles, qBuildCache, fetch, baseUrl, prefetchAppBundleNames, highPriority = false) => {
|
|
109
|
-
if (Array.isArray(prefetchAppBundleNames)) {
|
|
110
|
-
addBundlesToPrefetchQueue(prefetchAppBundleNames, appBundles, baseUrl, highPriority);
|
|
111
|
-
}
|
|
112
|
-
drainQueue(qBuildCache, fetch);
|
|
113
|
-
};
|
|
114
|
-
function addBundlesToPrefetchQueue(bundlesToPrefetch, appBundles, baseUrl, highPriority) {
|
|
115
|
-
for (const prefetchAppBundleName of bundlesToPrefetch) {
|
|
116
|
-
try {
|
|
117
|
-
const appBundle = getAppBundleByName(appBundles, prefetchAppBundleName);
|
|
118
|
-
if (appBundle) {
|
|
119
|
-
const importedBundleNames = getAppBundlesNamesFromIds(appBundles, appBundle[1]);
|
|
120
|
-
const url = new URL(prefetchAppBundleName, baseUrl).href;
|
|
121
|
-
const queueIndex = prefetchQueue.indexOf(url);
|
|
122
|
-
if (queueIndex > -1) {
|
|
123
|
-
// already in the queue
|
|
124
|
-
if (highPriority) {
|
|
125
|
-
// move to the front of the queue
|
|
126
|
-
prefetchQueue.splice(queueIndex, 1);
|
|
127
|
-
prefetchQueue.unshift(url);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
else {
|
|
131
|
-
if (highPriority) {
|
|
132
|
-
// add to the front of the queue
|
|
133
|
-
prefetchQueue.unshift(url);
|
|
134
|
-
}
|
|
135
|
-
else {
|
|
136
|
-
// add to the end of the queue
|
|
137
|
-
prefetchQueue.push(url);
|
|
138
|
-
}
|
|
139
|
-
addBundlesToPrefetchQueue(importedBundleNames, appBundles, baseUrl, highPriority);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
catch (e) {
|
|
144
|
-
console.error(e);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
function drainQueue(qBuildCache, fetch) {
|
|
149
|
-
// do not prefetch more than 6 requests at a time to ensure
|
|
150
|
-
// the browser is able to handle a user request as soon as possible
|
|
151
|
-
while (prefetchQueue.length > 0 && awaitingRequests.size < 6) {
|
|
152
|
-
const url = prefetchQueue.shift();
|
|
153
|
-
if (!existingPrefetchUrls.has(url)) {
|
|
154
|
-
const request = new Request(url);
|
|
155
|
-
existingPrefetchUrls.add(url);
|
|
156
|
-
cachedFetch(qBuildCache, fetch, awaitingRequests, request)
|
|
157
|
-
.catch(() => {
|
|
158
|
-
existingPrefetchUrls.delete(url);
|
|
159
|
-
})
|
|
160
|
-
.finally(() => drainQueue(qBuildCache, fetch));
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
const prefetchLinkBundles = (appBundles, libraryBundleIds, linkBundles, qBuildCache, fetch, baseUrl, linkPathnames) => {
|
|
165
|
-
try {
|
|
166
|
-
prefetchBundleNames(appBundles, qBuildCache, fetch, baseUrl, getAppBundlesNamesFromIds(appBundles, libraryBundleIds));
|
|
167
|
-
}
|
|
168
|
-
catch (e) {
|
|
169
|
-
console.error(e);
|
|
170
|
-
}
|
|
171
|
-
for (const linkPathname of linkPathnames) {
|
|
172
|
-
try {
|
|
173
|
-
for (const linkBundle of linkBundles) {
|
|
174
|
-
const [route, linkBundleIds] = linkBundle;
|
|
175
|
-
console;
|
|
176
|
-
if (route.test(linkPathname)) {
|
|
177
|
-
prefetchBundleNames(appBundles, qBuildCache, fetch, baseUrl, getAppBundlesNamesFromIds(appBundles, linkBundleIds));
|
|
178
|
-
break;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
catch (e) {
|
|
183
|
-
console.error(e);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
|
-
const prefetchWaterfall = (appBundles, qBuildCache, fetch, requestedBuildUrl) => {
|
|
188
|
-
try {
|
|
189
|
-
const { baseUrl, requestedBundleName } = splitUrlToBaseAndBundle(requestedBuildUrl);
|
|
190
|
-
prefetchBundleNames(appBundles, qBuildCache, fetch, baseUrl, [requestedBundleName], true);
|
|
191
|
-
}
|
|
192
|
-
catch (e) {
|
|
193
|
-
console.error(e);
|
|
194
|
-
}
|
|
195
|
-
};
|
|
196
|
-
function splitUrlToBaseAndBundle(fullUrl) {
|
|
197
|
-
const segments = fullUrl.href.split('/');
|
|
198
|
-
const requestedBundleName = segments[segments.length - 1];
|
|
199
|
-
segments[segments.length - 1] = '';
|
|
200
|
-
const baseUrl = new URL(segments.join('/'));
|
|
201
|
-
return {
|
|
202
|
-
baseUrl,
|
|
203
|
-
requestedBundleName,
|
|
204
|
-
};
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
const setupServiceWorkerScope = (swScope, appBundles, libraryBundleIds, linkBundles) => {
|
|
208
|
-
const swFetch = swScope.fetch.bind(swScope);
|
|
209
|
-
const appSymbols = computeAppSymbols(appBundles);
|
|
210
|
-
swScope.addEventListener('activate', (event) => {
|
|
211
|
-
(async () => {
|
|
212
|
-
try {
|
|
213
|
-
// Delete any other caches that are not the current SW cache name
|
|
214
|
-
event.waitUntil(swScope.caches.keys().then((keys) => Promise.all(keys.map((key) => {
|
|
215
|
-
if (key !== qBuildCacheName) {
|
|
216
|
-
return caches.delete(key);
|
|
217
|
-
}
|
|
218
|
-
}))));
|
|
219
|
-
// Delete old bundles
|
|
220
|
-
const qBuildCache = await swScope.caches.open(qBuildCacheName);
|
|
221
|
-
const cachedRequestKeys = await qBuildCache.keys();
|
|
222
|
-
const cachedUrls = cachedRequestKeys.map((r) => r.url);
|
|
223
|
-
const cachedRequestsToDelete = getCacheToDelete(appBundles, cachedUrls);
|
|
224
|
-
await Promise.all(cachedRequestsToDelete.map((r) => qBuildCache.delete(r)));
|
|
225
|
-
}
|
|
226
|
-
catch (e) {
|
|
227
|
-
console.error(e);
|
|
228
|
-
}
|
|
229
|
-
})();
|
|
230
|
-
});
|
|
231
|
-
swScope.addEventListener('message', async ({ data }) => {
|
|
232
|
-
if (data.type === 'qprefetch' && typeof data.base === 'string') {
|
|
233
|
-
const qBuildCache = await swScope.caches.open(qBuildCacheName);
|
|
234
|
-
const baseUrl = new URL(data.base, swScope.origin);
|
|
235
|
-
if (Array.isArray(data.links)) {
|
|
236
|
-
prefetchLinkBundles(appBundles, libraryBundleIds, linkBundles, qBuildCache, swFetch, baseUrl, data.links);
|
|
237
|
-
}
|
|
238
|
-
if (Array.isArray(data.bundles)) {
|
|
239
|
-
prefetchBundleNames(appBundles, qBuildCache, swFetch, baseUrl, data.bundles);
|
|
240
|
-
}
|
|
241
|
-
if (Array.isArray(data.symbols)) {
|
|
242
|
-
prefetchBundleNames(appBundles, qBuildCache, swFetch, baseUrl, resolveSymbols(appSymbols, data.symbols));
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
});
|
|
246
|
-
swScope.addEventListener('fetch', (event) => {
|
|
247
|
-
const request = event.request;
|
|
248
|
-
if (request.method === 'GET') {
|
|
249
|
-
const url = new URL(request.url);
|
|
250
|
-
if (isAppBundleRequest(appBundles, url.pathname)) {
|
|
251
|
-
event.respondWith(swScope.caches.open(qBuildCacheName).then((qBuildCache) => {
|
|
252
|
-
prefetchWaterfall(appBundles, qBuildCache, swFetch, url);
|
|
253
|
-
return cachedFetch(qBuildCache, swFetch, awaitingRequests, request);
|
|
254
|
-
}));
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
});
|
|
258
|
-
};
|
|
259
|
-
|
|
260
|
-
/** @public */
|
|
261
|
-
const setupServiceWorker = () => {
|
|
262
|
-
if (typeof self !== 'undefined' && typeof appBundles !== 'undefined') {
|
|
263
|
-
setupServiceWorkerScope(self, appBundles, libraryBundleIds, linkBundles);
|
|
264
|
-
}
|
|
265
|
-
};
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated This is no longer needed, Qwik now automatically embeds preloading logic into the
|
|
5
|
+
* application.
|
|
6
|
+
*
|
|
7
|
+
* If your service-worker.ts file contains no custom code, you should deploy to production until
|
|
8
|
+
* you're sure that all users picked up the new version, then you can remove it and also remove
|
|
9
|
+
* the `<ServiceWorkerRegister />` component from your `Root.tsx`.
|
|
10
|
+
*
|
|
11
|
+
* If you do have custom service worker logic, you should keep the `service-worker.ts` file and
|
|
12
|
+
* `<ServiceWorkerRegister />` component, but remove the `setupServiceWorker()` call.
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
const setupServiceWorker = () => { };
|
|
266
16
|
|
|
267
17
|
exports.setupServiceWorker = setupServiceWorker;
|
package/lib/service-worker.d.ts
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated This is no longer needed, Qwik now automatically embeds preloading logic into the
|
|
3
|
+
* application.
|
|
4
|
+
*
|
|
5
|
+
* If your service-worker.ts file contains no custom code, you should deploy to production until
|
|
6
|
+
* you're sure that all users picked up the new version, then you can remove it and also remove
|
|
7
|
+
* the `<ServiceWorkerRegister />` component from your `Root.tsx`.
|
|
8
|
+
*
|
|
9
|
+
* If you do have custom service worker logic, you should keep the `service-worker.ts` file and
|
|
10
|
+
* `<ServiceWorkerRegister />` component, but remove the `setupServiceWorker()` call.
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export declare const setupServiceWorker: () => void;
|
|
14
|
+
|
|
15
|
+
export { }
|