@qwik.dev/router 2.0.0-rc.3 → 2.0.0-rc.5
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/shared/vite/index.mjs +1 -1
- package/lib/chunks/head.qwik.mjs +227 -114
- package/lib/chunks/request-path.mjs +9 -1
- package/lib/chunks/system.mjs +65 -10
- package/lib/chunks/user-response.mjs +283 -319
- package/lib/chunks/worker-thread.mjs +22 -11
- package/lib/index.d.ts +14 -12
- package/lib/index.qwik.mjs +278 -116
- package/lib/middleware/node/index.mjs +7 -1
- package/lib/middleware/request-handler/index.d.ts +6 -6
- package/lib/middleware/request-handler/index.mjs +4 -5
- package/lib/ssg/index.d.ts +7 -0
- package/lib/ssg/index.mjs +52 -6
- package/lib/vite/index.mjs +67 -20
- package/package.json +4 -4
|
@@ -1,62 +1,268 @@
|
|
|
1
1
|
import { t as ensureSlash } from "./pathname.mjs";
|
|
2
|
-
import {
|
|
3
|
-
import { a as
|
|
2
|
+
import { D as resolveRouteLoaderByHash, M as setRouteLoaders, P as isPromise, T as matchesRouteLoaderId, _t as setETagHeader, b as getRouteLoaderResponse, dt as QFN_KEY, ft as Q_ROUTE, gt as resolvePreETag, h as getLoaderRequestEvent, ht as performETagMatch, lt as QACTION_KEY, mt as normalizeETag, o as FULLPATH_HEADER, pt as hash, r as resolveRouteConfig, s as ROUTE_PATH_HEADER, u as clearRouteLoaderData, ut as QDATA_KEY, v as getRouteLoaderCtx, vt as RequestEvRouterConfig, w as loadRouteLoader, x as getRouteLoaderValues, y as getRouteLoaderParams, yt as _asyncRequestStore } from "./head.qwik.mjs";
|
|
3
|
+
import { a as QActionId, c as recognizeRequest, d as trimRecognizedInternalPathname, l as resolveValidInternalFullPathname, o as QLoaderId, t as IsQAction } from "./request-path.mjs";
|
|
4
4
|
import { a as shareClassIdentity, i as RedirectMessage, n as throwIfControlFlowSignal, r as AbortMessage, t as ServerError } from "./server-error.mjs";
|
|
5
5
|
import { inlinedQrl, isDev } from "@qwik.dev/core";
|
|
6
|
-
import { _deserialize,
|
|
6
|
+
import { _deserialize, _verifySerializable, isDev as isDev$1 } from "@qwik.dev/core/internal";
|
|
7
|
+
import { _serialize } from "@qwik.dev/core/internal/serializer";
|
|
8
|
+
var MAX_CACHE_SIZE = typeof globalThis.__SSR_CACHE_SIZE__ === "number" ? globalThis.__SSR_CACHE_SIZE__ : 50;
|
|
9
|
+
function createLruCache() {
|
|
10
|
+
const cache = /* @__PURE__ */ new Map();
|
|
11
|
+
return {
|
|
12
|
+
get(key) {
|
|
13
|
+
const entry = cache.get(key);
|
|
14
|
+
if (entry !== void 0) {
|
|
15
|
+
cache.delete(key);
|
|
16
|
+
cache.set(key, entry);
|
|
17
|
+
}
|
|
18
|
+
return entry;
|
|
19
|
+
},
|
|
20
|
+
set(key, entry) {
|
|
21
|
+
if (MAX_CACHE_SIZE <= 0) return;
|
|
22
|
+
if (cache.size >= MAX_CACHE_SIZE) {
|
|
23
|
+
const firstKey = cache.keys().next().value;
|
|
24
|
+
cache.delete(firstKey);
|
|
25
|
+
}
|
|
26
|
+
cache.set(key, entry);
|
|
27
|
+
},
|
|
28
|
+
clear(key) {
|
|
29
|
+
if (key != null) cache.delete(key);
|
|
30
|
+
else cache.clear();
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
var ssrCache = createLruCache();
|
|
35
|
+
var loaderCache = createLruCache();
|
|
36
|
+
/**
|
|
37
|
+
* Resolve an eTag value from a ContentModuleETag (string, function, or undefined). Returns the eTag
|
|
38
|
+
* string or null.
|
|
39
|
+
*/
|
|
40
|
+
function resolveETag(eTagExport, headProps) {
|
|
41
|
+
if (eTagExport === void 0) return null;
|
|
42
|
+
if (typeof eTagExport === "function") return eTagExport(headProps);
|
|
43
|
+
return eTagExport;
|
|
44
|
+
}
|
|
7
45
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
46
|
+
* Resolve a cache key from a CacheKeyFn. The default key callback is only called when `cacheKey ===
|
|
47
|
+
* true`.
|
|
10
48
|
*/
|
|
11
|
-
function
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
49
|
+
function resolveCacheKey(cacheKeyExport, defaultKey, requestEv, eTag) {
|
|
50
|
+
if (cacheKeyExport === true) return defaultKey(requestEv, eTag);
|
|
51
|
+
if (typeof cacheKeyExport !== "function") return "";
|
|
52
|
+
return cacheKeyExport(requestEv, eTag) || "";
|
|
53
|
+
}
|
|
54
|
+
/** Build the default SSR cache key. eTag slot is dropped when no eTag was resolved. */
|
|
55
|
+
function defaultSsrCacheKey(requestEv, eTag) {
|
|
56
|
+
const status = requestEv.status();
|
|
57
|
+
const { origin, pathname } = requestEv.originalUrl;
|
|
58
|
+
const url = origin + pathname;
|
|
59
|
+
return eTag ? `${status}|${eTag}|${url}` : `${status}|${url}`;
|
|
60
|
+
}
|
|
61
|
+
/** Build the default loader cache key. eTag slot is dropped when no eTag was resolved. */
|
|
62
|
+
function defaultLoaderCacheKey(requestEv, eTag) {
|
|
63
|
+
const loaderId = requestEv.sharedMap.get(QLoaderId);
|
|
64
|
+
const { origin, pathname, search } = requestEv.url;
|
|
65
|
+
const base = `${origin}${pathname}|${search}|${loaderId}`;
|
|
66
|
+
return eTag ? `${base}|${eTag}` : base;
|
|
67
|
+
}
|
|
68
|
+
/** Get a cached SSR entry by key (LRU touch). */
|
|
69
|
+
function getCachedSsr(key) {
|
|
70
|
+
return ssrCache.get(key);
|
|
71
|
+
}
|
|
72
|
+
/** Cache an SSR entry by key, evicting the least recently used entry if at capacity. */
|
|
73
|
+
function setCachedSsr(key, entry) {
|
|
74
|
+
ssrCache.set(key, entry);
|
|
75
|
+
}
|
|
76
|
+
/** Get a cached loader entry by key (LRU touch). */
|
|
77
|
+
function getCachedLoader(key) {
|
|
78
|
+
return loaderCache.get(key);
|
|
79
|
+
}
|
|
80
|
+
/** Cache a loader entry by key, evicting the least recently used entry if at capacity. */
|
|
81
|
+
function setCachedLoader(key, entry) {
|
|
82
|
+
loaderCache.set(key, entry);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Clear the in-memory SSR cache. Call after deployments or data changes.
|
|
86
|
+
*
|
|
87
|
+
* When `cacheKey` is provided, only that single entry is removed; otherwise the entire cache is
|
|
88
|
+
* cleared.
|
|
89
|
+
*
|
|
90
|
+
* @public
|
|
91
|
+
*/
|
|
92
|
+
function clearSsrCache(cacheKey) {
|
|
93
|
+
ssrCache.clear(cacheKey);
|
|
18
94
|
}
|
|
19
95
|
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
96
|
+
* Clear the in-memory loader cache. Call after deployments or data changes.
|
|
97
|
+
*
|
|
98
|
+
* When `cacheKey` is provided, only that single entry is removed; otherwise the entire cache is
|
|
99
|
+
* cleared.
|
|
100
|
+
*
|
|
101
|
+
* @public
|
|
25
102
|
*/
|
|
26
|
-
function
|
|
27
|
-
|
|
28
|
-
if (tag.endsWith("\"") && tag.startsWith("W/\"")) tag = tag.slice(3);
|
|
29
|
-
tag = tag.replace(/[^!#-~]/g, "");
|
|
30
|
-
return tag;
|
|
103
|
+
function clearLoaderCache(cacheKey) {
|
|
104
|
+
loaderCache.clear(cacheKey);
|
|
31
105
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
106
|
+
/**
|
|
107
|
+
* Create per-loader request event views. Loader-specific URL/search data is scoped to the view,
|
|
108
|
+
* while request-wide state (headers, cookies, loader value maps, etc.) still delegates to the
|
|
109
|
+
* original request event.
|
|
110
|
+
*/
|
|
111
|
+
function createLoaderRequestEventFactory(requestEv) {
|
|
112
|
+
return (loader) => getLoaderRequestEvent(loader, requestEv);
|
|
35
113
|
}
|
|
36
114
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
115
|
+
* Handler that executes the requested loader and returns the result as JSON. Runs AFTER
|
|
116
|
+
* plugin/route middleware, so middleware redirects/errors are handled by `jsonRequestWrapper`. The
|
|
117
|
+
* loader function's own redirects/errors are caught by getRouteLoaderResponse and serialized in the
|
|
118
|
+
* LoaderResponse envelope ({ d, r, e }).
|
|
40
119
|
*/
|
|
41
|
-
function
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
120
|
+
function loaderHandler(routeLoaders, route) {
|
|
121
|
+
return async (requestEvent) => {
|
|
122
|
+
const requestEv = requestEvent;
|
|
123
|
+
if (!requestEv.sharedMap.has("@isQLoader")) return;
|
|
124
|
+
if (requestEv.headersSent || requestEv.exited) return;
|
|
125
|
+
const loaderId = requestEv.sharedMap.get(QLoaderId);
|
|
126
|
+
const loader = resolveRouteLoaderByHash(routeLoaders, loaderId);
|
|
127
|
+
if (!loader) {
|
|
128
|
+
requestEv.json(404, { error: "Loader not found" });
|
|
129
|
+
return;
|
|
51
130
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
131
|
+
setRouteLoaderData(requestEv, routeLoaders, route);
|
|
132
|
+
await runRouteGuards(routeLoaders, loader, requestEv);
|
|
133
|
+
const loaderRequestEv = createLoaderRequestEventFactory(requestEv)(loader);
|
|
134
|
+
const cacheControl = resolveLoaderCacheControl(loader.__cacheControl, loaderRequestEv);
|
|
135
|
+
const applyCacheControl = () => {
|
|
136
|
+
if (cacheControl !== null && !requestEv.headers.has("Cache-Control")) requestEv.cacheControl(cacheControl);
|
|
137
|
+
};
|
|
138
|
+
const normalizedETag = loader.__eTag !== void 0 ? resolvePreETag(loader.__eTag, loaderRequestEv) : "";
|
|
139
|
+
if (normalizedETag && performETagMatch(loaderRequestEv, normalizedETag, applyCacheControl)) return;
|
|
140
|
+
let cacheKey = "";
|
|
141
|
+
if (loader.__cacheKey) cacheKey = resolveCacheKey(loader.__cacheKey, defaultLoaderCacheKey, loaderRequestEv, normalizedETag);
|
|
142
|
+
if (cacheKey) {
|
|
143
|
+
const cached = getCachedLoader(cacheKey);
|
|
144
|
+
if (cached) {
|
|
145
|
+
if (!normalizedETag) {
|
|
146
|
+
if (performETagMatch(loaderRequestEv, cached.eTag, applyCacheControl)) return;
|
|
147
|
+
} else setETagHeader(loaderRequestEv, cached.eTag);
|
|
148
|
+
await sendLoaderResponse(requestEv, cached.body, cacheControl);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
57
151
|
}
|
|
152
|
+
const responseData = await getRouteLoaderResponse(loader.__qrl, loader.__validators, loaderRequestEv);
|
|
153
|
+
const data = await _serialize(responseData);
|
|
154
|
+
const failed = responseData.d && typeof responseData.d === "object" && responseData.d.failed;
|
|
155
|
+
const cacheable = cacheKey && !responseData.r && !responseData.e && !failed;
|
|
156
|
+
const finalETag = normalizedETag || (cacheable ? hash(data) : "");
|
|
157
|
+
if (cacheable && finalETag) setCachedLoader(cacheKey, {
|
|
158
|
+
eTag: finalETag,
|
|
159
|
+
body: data
|
|
160
|
+
});
|
|
161
|
+
if (!normalizedETag && finalETag && performETagMatch(loaderRequestEv, finalETag, applyCacheControl)) return;
|
|
162
|
+
if (responseData.e || failed) requestEv.headers.set("Cache-Control", "no-store");
|
|
163
|
+
await sendLoaderResponse(requestEv, data, cacheControl);
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Resolve the loader's cacheControl option. Loaders default to private revalidation; pair with
|
|
168
|
+
* `eTag` for cheap 304s. A function form may return `null` to skip the header entirely.
|
|
169
|
+
*/
|
|
170
|
+
function resolveLoaderCacheControl(option, requestEv) {
|
|
171
|
+
const value = typeof option === "function" ? option(requestEv) : option;
|
|
172
|
+
return value === void 0 ? "private" : value;
|
|
173
|
+
}
|
|
174
|
+
/** Blocking loaders guard the route, so they all run whatever the export order. */
|
|
175
|
+
async function runRouteGuards(routeLoaders, targetLoader, requestEv) {
|
|
176
|
+
for (const loader of routeLoaders) if (loader !== targetLoader && loader.__blockSSR) await loadRouteLoader(loader, requestEv);
|
|
177
|
+
}
|
|
178
|
+
function setRouteLoaderData(requestEv, routeLoaders, route) {
|
|
179
|
+
if (route?.$loaderPaths$) Object.assign(getRouteLoaderCtx(requestEv).loaderPaths, route.$loaderPaths$);
|
|
180
|
+
if (route?.$loaderParams$) Object.assign(getRouteLoaderParams(requestEv), route.$loaderParams$);
|
|
181
|
+
setRouteLoaders(requestEv, routeLoaders);
|
|
182
|
+
}
|
|
183
|
+
async function sendLoaderResponse(requestEv, data, cacheControl) {
|
|
184
|
+
requestEv.headers.set("Content-Type", "application/json; charset=utf-8");
|
|
185
|
+
addVaryHeader(requestEv, FULLPATH_HEADER);
|
|
186
|
+
if (isDev) addVaryHeader(requestEv, ROUTE_PATH_HEADER);
|
|
187
|
+
if (cacheControl !== null && !requestEv.headers.has("Cache-Control")) requestEv.cacheControl(cacheControl);
|
|
188
|
+
requestEv.send(200, data);
|
|
189
|
+
}
|
|
190
|
+
function addVaryHeader(requestEv, value) {
|
|
191
|
+
const vary = requestEv.headers.get("Vary");
|
|
192
|
+
if (!vary) {
|
|
193
|
+
requestEv.headers.set("Vary", value);
|
|
194
|
+
return;
|
|
58
195
|
}
|
|
59
|
-
|
|
196
|
+
if (!vary.split(",").map((item) => item.trim().toLowerCase()).includes(value.toLowerCase())) requestEv.headers.set("Vary", `${vary}, ${value}`);
|
|
197
|
+
}
|
|
198
|
+
/** Serialize and send a JSON response (used by error/redirect paths in jsonRequestWrapper). */
|
|
199
|
+
async function sendJsonResponse(requestEv, responseData, status = 200) {
|
|
200
|
+
const data = await _serialize(responseData);
|
|
201
|
+
requestEv.headers.set("Content-Type", "application/json; charset=utf-8");
|
|
202
|
+
if (!requestEv.headers.has("Cache-Control")) requestEv.cacheControl("private");
|
|
203
|
+
requestEv.send(status, data);
|
|
204
|
+
}
|
|
205
|
+
async function sendActionResponse(requestEv, responseData) {
|
|
206
|
+
const data = await _serialize(responseData);
|
|
207
|
+
requestEv.headers.set("Content-Type", "application/json; charset=utf-8");
|
|
208
|
+
requestEv.send(responseData.s || 200, data);
|
|
209
|
+
}
|
|
210
|
+
/** Shared by route selection and the URL rewrite so both agree on the page. */
|
|
211
|
+
function resolveLoaderPagePathname(request, loaderPathname, isDevRequest = isDev) {
|
|
212
|
+
const routePath = request.headers.get("X-Qwik-fullpath") ?? (isDevRequest ? request.headers.get("X-Qwik-route-path") : null);
|
|
213
|
+
return resolveValidInternalFullPathname(loaderPathname, routePath);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Early handler that wraps `next()` for JSON API requests (q-loader and q-action).
|
|
217
|
+
*
|
|
218
|
+
* For `IsQLoader` requests, it also rewrites the URL using the `X-Qwik-fullpath` header (or the dev
|
|
219
|
+
* `X-Qwik-route-path` header) so that downstream middleware sees the real page URL.
|
|
220
|
+
*
|
|
221
|
+
* By calling `await next()` inside a try/catch, middleware redirects and errors are captured and
|
|
222
|
+
* returned as JSON envelopes instead of HTTP redirects/error pages. This keeps SPA navigation
|
|
223
|
+
* intact on the client.
|
|
224
|
+
*/
|
|
225
|
+
function jsonRequestWrapper() {
|
|
226
|
+
return async (requestEvent) => {
|
|
227
|
+
const requestEv = requestEvent;
|
|
228
|
+
const isLoader = requestEv.internalRequest === "loader";
|
|
229
|
+
const isActionJson = requestEv.internalRequest === "action";
|
|
230
|
+
if (!isLoader && !isActionJson) return;
|
|
231
|
+
if (isLoader) {
|
|
232
|
+
addVaryHeader(requestEv, FULLPATH_HEADER);
|
|
233
|
+
if (isDev) addVaryHeader(requestEv, ROUTE_PATH_HEADER);
|
|
234
|
+
const pagePathname = resolveLoaderPagePathname(requestEv.request, requestEv.url.pathname);
|
|
235
|
+
if (pagePathname) requestEv.url.pathname = pagePathname;
|
|
236
|
+
}
|
|
237
|
+
try {
|
|
238
|
+
await requestEv.next();
|
|
239
|
+
} catch (err) {
|
|
240
|
+
if (requestEv.headersSent) return;
|
|
241
|
+
if (err instanceof RedirectMessage) {
|
|
242
|
+
const location = requestEv.headers.get("Location") || "/";
|
|
243
|
+
requestEv.headers.delete("Location");
|
|
244
|
+
if (isLoader) await sendJsonResponse(requestEv, { r: location });
|
|
245
|
+
else await sendActionResponse(requestEv, { redirect: location });
|
|
246
|
+
} else if (err instanceof ServerError) {
|
|
247
|
+
requestEv.headers.set("Cache-Control", "no-store");
|
|
248
|
+
if (isLoader) await sendJsonResponse(requestEv, { e: err });
|
|
249
|
+
else await sendActionResponse(requestEv, {
|
|
250
|
+
e: err,
|
|
251
|
+
s: err.status
|
|
252
|
+
});
|
|
253
|
+
} else if (err instanceof Error) {
|
|
254
|
+
console.error("JSON request error:", err);
|
|
255
|
+
requestEv.headers.set("Cache-Control", "no-store");
|
|
256
|
+
const message = isDev ? `${err.message}\n(this is only visible in dev mode)` : "Internal Server Error";
|
|
257
|
+
const se = new ServerError(500, message);
|
|
258
|
+
if (isLoader) await sendJsonResponse(requestEv, { e: se });
|
|
259
|
+
else await sendActionResponse(requestEv, {
|
|
260
|
+
e: se,
|
|
261
|
+
s: 500
|
|
262
|
+
});
|
|
263
|
+
} else throw err;
|
|
264
|
+
}
|
|
265
|
+
};
|
|
60
266
|
}
|
|
61
267
|
function createCacheControl(cacheControl) {
|
|
62
268
|
const controls = [];
|
|
@@ -219,11 +425,6 @@ var encoder = /*#__PURE__*/ new TextEncoder();
|
|
|
219
425
|
function getContentType(headers) {
|
|
220
426
|
return (headers.get("content-type")?.split(/[;,]/, 1)[0].trim() ?? "").toLowerCase();
|
|
221
427
|
}
|
|
222
|
-
function isContentType(headers, ...types) {
|
|
223
|
-
const type = getContentType(headers);
|
|
224
|
-
for (let i = 0; i < types.length; i++) if (types[i].toLowerCase() === type) return true;
|
|
225
|
-
return false;
|
|
226
|
-
}
|
|
227
428
|
var RequestEvMode = Symbol("RequestEvMode");
|
|
228
429
|
var RequestEvRoute = Symbol("RequestEvRoute");
|
|
229
430
|
var RequestRouteName = "@routeName";
|
|
@@ -234,6 +435,7 @@ var RequestEvIsRewrite = "@rewrite";
|
|
|
234
435
|
var RequestEvShareServerTiming = "@serverTiming";
|
|
235
436
|
var RequestEvETagCacheKey = "@eTagCacheKey";
|
|
236
437
|
var RequestEvCaughtError = "@caughtError";
|
|
438
|
+
var RequestEvSentServerError = "@sentServerError";
|
|
237
439
|
var RequestEvHttpStatusMessage = "@httpStatusMessage";
|
|
238
440
|
function createRequestEvent(serverRequestEv, loadedRoute, requestHandlers, basePathname, resolved, routerConfig) {
|
|
239
441
|
const { request, platform, env } = serverRequestEv;
|
|
@@ -246,7 +448,7 @@ function createRequestEvent(serverRequestEv, loadedRoute, requestHandlers, baseP
|
|
|
246
448
|
if (recognized) {
|
|
247
449
|
url.pathname = trimRecognizedInternalPathname(url.pathname, recognized);
|
|
248
450
|
sharedMap.set(recognized.type, true);
|
|
249
|
-
internalRequest = "loader";
|
|
451
|
+
if (recognized.type === "@isQLoader") internalRequest = "loader";
|
|
250
452
|
if (recognized.data?.loaderId) sharedMap.set(QLoaderId, recognized.data.loaderId);
|
|
251
453
|
}
|
|
252
454
|
const actionId = url.searchParams.get(QACTION_KEY);
|
|
@@ -538,6 +740,7 @@ var formToObj = (formData) => {
|
|
|
538
740
|
const childNode = pathNode.children.get(key);
|
|
539
741
|
const nextValue = object[key];
|
|
540
742
|
if (nextValue !== void 0) {
|
|
743
|
+
if (!Array.isArray(nextValue) && Object.getPrototypeOf(nextValue) !== null) break;
|
|
541
744
|
object = nextValue;
|
|
542
745
|
pathNode = childNode;
|
|
543
746
|
continue;
|
|
@@ -549,262 +752,6 @@ var formToObj = (formData) => {
|
|
|
549
752
|
}
|
|
550
753
|
return values;
|
|
551
754
|
};
|
|
552
|
-
var MAX_CACHE_SIZE = typeof globalThis.__SSR_CACHE_SIZE__ === "number" ? globalThis.__SSR_CACHE_SIZE__ : 50;
|
|
553
|
-
function createLruCache() {
|
|
554
|
-
const cache = /* @__PURE__ */ new Map();
|
|
555
|
-
return {
|
|
556
|
-
get(key) {
|
|
557
|
-
const entry = cache.get(key);
|
|
558
|
-
if (entry !== void 0) {
|
|
559
|
-
cache.delete(key);
|
|
560
|
-
cache.set(key, entry);
|
|
561
|
-
}
|
|
562
|
-
return entry;
|
|
563
|
-
},
|
|
564
|
-
set(key, entry) {
|
|
565
|
-
if (MAX_CACHE_SIZE <= 0) return;
|
|
566
|
-
if (cache.size >= MAX_CACHE_SIZE) {
|
|
567
|
-
const firstKey = cache.keys().next().value;
|
|
568
|
-
cache.delete(firstKey);
|
|
569
|
-
}
|
|
570
|
-
cache.set(key, entry);
|
|
571
|
-
},
|
|
572
|
-
clear(key) {
|
|
573
|
-
if (key != null) cache.delete(key);
|
|
574
|
-
else cache.clear();
|
|
575
|
-
}
|
|
576
|
-
};
|
|
577
|
-
}
|
|
578
|
-
var ssrCache = createLruCache();
|
|
579
|
-
var loaderCache = createLruCache();
|
|
580
|
-
/**
|
|
581
|
-
* Resolve an eTag value from a ContentModuleETag (string, function, or undefined). Returns the eTag
|
|
582
|
-
* string or null.
|
|
583
|
-
*/
|
|
584
|
-
function resolveETag(eTagExport, headProps) {
|
|
585
|
-
if (eTagExport === void 0) return null;
|
|
586
|
-
if (typeof eTagExport === "function") return eTagExport(headProps);
|
|
587
|
-
return eTagExport;
|
|
588
|
-
}
|
|
589
|
-
/**
|
|
590
|
-
* Resolve a cache key from a CacheKeyFn. The default key callback is only called when `cacheKey ===
|
|
591
|
-
* true`.
|
|
592
|
-
*/
|
|
593
|
-
function resolveCacheKey(cacheKeyExport, defaultKey, requestEv, eTag) {
|
|
594
|
-
if (cacheKeyExport === true) return defaultKey(requestEv, eTag);
|
|
595
|
-
if (typeof cacheKeyExport !== "function") return "";
|
|
596
|
-
return cacheKeyExport(requestEv, eTag) || "";
|
|
597
|
-
}
|
|
598
|
-
/** Build the default SSR cache key. eTag slot is dropped when no eTag was resolved. */
|
|
599
|
-
function defaultSsrCacheKey(requestEv, eTag) {
|
|
600
|
-
const status = requestEv.status();
|
|
601
|
-
const pathname = requestEv.originalUrl.pathname;
|
|
602
|
-
return eTag ? `${status}|${eTag}|${pathname}` : `${status}|${pathname}`;
|
|
603
|
-
}
|
|
604
|
-
/** Build the default loader cache key. eTag slot is dropped when no eTag was resolved. */
|
|
605
|
-
function defaultLoaderCacheKey(requestEv, eTag) {
|
|
606
|
-
const loaderId = requestEv.sharedMap.get(QLoaderId);
|
|
607
|
-
const base = `${requestEv.url.pathname}|${requestEv.url.search}|${loaderId}`;
|
|
608
|
-
return eTag ? `${base}|${eTag}` : base;
|
|
609
|
-
}
|
|
610
|
-
/** Get a cached SSR entry by key (LRU touch). */
|
|
611
|
-
function getCachedSsr(key) {
|
|
612
|
-
return ssrCache.get(key);
|
|
613
|
-
}
|
|
614
|
-
/** Cache an SSR entry by key, evicting the least recently used entry if at capacity. */
|
|
615
|
-
function setCachedSsr(key, entry) {
|
|
616
|
-
ssrCache.set(key, entry);
|
|
617
|
-
}
|
|
618
|
-
/** Get a cached loader entry by key (LRU touch). */
|
|
619
|
-
function getCachedLoader(key) {
|
|
620
|
-
return loaderCache.get(key);
|
|
621
|
-
}
|
|
622
|
-
/** Cache a loader entry by key, evicting the least recently used entry if at capacity. */
|
|
623
|
-
function setCachedLoader(key, entry) {
|
|
624
|
-
loaderCache.set(key, entry);
|
|
625
|
-
}
|
|
626
|
-
/**
|
|
627
|
-
* Clear the in-memory SSR cache. Call after deployments or data changes.
|
|
628
|
-
*
|
|
629
|
-
* When `cacheKey` is provided, only that single entry is removed; otherwise the entire cache is
|
|
630
|
-
* cleared.
|
|
631
|
-
*
|
|
632
|
-
* @public
|
|
633
|
-
*/
|
|
634
|
-
function clearSsrCache(cacheKey) {
|
|
635
|
-
ssrCache.clear(cacheKey);
|
|
636
|
-
}
|
|
637
|
-
/**
|
|
638
|
-
* Clear the in-memory loader cache. Call after deployments or data changes.
|
|
639
|
-
*
|
|
640
|
-
* When `cacheKey` is provided, only that single entry is removed; otherwise the entire cache is
|
|
641
|
-
* cleared.
|
|
642
|
-
*
|
|
643
|
-
* @public
|
|
644
|
-
*/
|
|
645
|
-
function clearLoaderCache(cacheKey) {
|
|
646
|
-
loaderCache.clear(cacheKey);
|
|
647
|
-
}
|
|
648
|
-
/**
|
|
649
|
-
* Create per-loader request event views. Loader-specific URL/search data is scoped to the view,
|
|
650
|
-
* while request-wide state (headers, cookies, loader value maps, etc.) still delegates to the
|
|
651
|
-
* original request event.
|
|
652
|
-
*/
|
|
653
|
-
function createLoaderRequestEventFactory(requestEv) {
|
|
654
|
-
return (loader) => getLoaderRequestEvent(loader, requestEv);
|
|
655
|
-
}
|
|
656
|
-
/**
|
|
657
|
-
* Handler that executes the requested loader and returns the result as JSON. Runs AFTER
|
|
658
|
-
* plugin/route middleware, so middleware redirects/errors are handled by `jsonRequestWrapper`. The
|
|
659
|
-
* loader function's own redirects/errors are caught by getRouteLoaderResponse and serialized in the
|
|
660
|
-
* LoaderResponse envelope ({ d, r, e }).
|
|
661
|
-
*/
|
|
662
|
-
function loaderHandler(routeLoaders, route) {
|
|
663
|
-
return async (requestEvent) => {
|
|
664
|
-
const requestEv = requestEvent;
|
|
665
|
-
if (!requestEv.sharedMap.has("@isQLoader")) return;
|
|
666
|
-
if (requestEv.headersSent || requestEv.exited) return;
|
|
667
|
-
const loaderId = requestEv.sharedMap.get(QLoaderId);
|
|
668
|
-
const loader = resolveRouteLoaderByHash(routeLoaders, loaderId);
|
|
669
|
-
if (!loader) {
|
|
670
|
-
requestEv.json(404, { error: "Loader not found" });
|
|
671
|
-
return;
|
|
672
|
-
}
|
|
673
|
-
setRouteLoaderData(requestEv, routeLoaders, route);
|
|
674
|
-
await runBlockingLoadersBeforeTarget(routeLoaders, loader, requestEv);
|
|
675
|
-
const loaderRequestEv = createLoaderRequestEventFactory(requestEv)(loader);
|
|
676
|
-
const cacheControl = resolveLoaderCacheControl(loader.__cacheControl, loaderRequestEv);
|
|
677
|
-
const applyCacheControl = () => {
|
|
678
|
-
if (cacheControl !== null && !requestEv.headers.has("Cache-Control")) requestEv.cacheControl(cacheControl);
|
|
679
|
-
};
|
|
680
|
-
const normalizedETag = loader.__eTag !== void 0 ? resolvePreETag(loader.__eTag, loaderRequestEv) : "";
|
|
681
|
-
if (normalizedETag && performETagMatch(loaderRequestEv, normalizedETag, applyCacheControl)) return;
|
|
682
|
-
let cacheKey = "";
|
|
683
|
-
if (loader.__cacheKey) cacheKey = resolveCacheKey(loader.__cacheKey, defaultLoaderCacheKey, loaderRequestEv, normalizedETag);
|
|
684
|
-
if (cacheKey) {
|
|
685
|
-
const cached = getCachedLoader(cacheKey);
|
|
686
|
-
if (cached) {
|
|
687
|
-
if (!normalizedETag) {
|
|
688
|
-
if (performETagMatch(loaderRequestEv, cached.eTag, applyCacheControl)) return;
|
|
689
|
-
} else setETagHeader(loaderRequestEv, cached.eTag);
|
|
690
|
-
await sendLoaderResponse(requestEv, cached.body, cacheControl);
|
|
691
|
-
return;
|
|
692
|
-
}
|
|
693
|
-
}
|
|
694
|
-
const responseData = await getRouteLoaderResponse(loader.__qrl, loader.__validators, loaderRequestEv);
|
|
695
|
-
const data = await _serialize(responseData);
|
|
696
|
-
const failed = responseData.d && typeof responseData.d === "object" && responseData.d.failed;
|
|
697
|
-
const cacheable = cacheKey && !responseData.r && !responseData.e && !failed;
|
|
698
|
-
const finalETag = normalizedETag || (cacheable ? hash(data) : "");
|
|
699
|
-
if (cacheable && finalETag) setCachedLoader(cacheKey, {
|
|
700
|
-
eTag: finalETag,
|
|
701
|
-
body: data
|
|
702
|
-
});
|
|
703
|
-
if (!normalizedETag && finalETag && performETagMatch(loaderRequestEv, finalETag, applyCacheControl)) return;
|
|
704
|
-
await sendLoaderResponse(requestEv, data, cacheControl);
|
|
705
|
-
};
|
|
706
|
-
}
|
|
707
|
-
/**
|
|
708
|
-
* Resolve the loader's cacheControl option. Loaders default to private revalidation; pair with
|
|
709
|
-
* `eTag` for cheap 304s. A function form may return `null` to skip the header entirely.
|
|
710
|
-
*/
|
|
711
|
-
function resolveLoaderCacheControl(option, requestEv) {
|
|
712
|
-
const value = typeof option === "function" ? option(requestEv) : option;
|
|
713
|
-
return value === void 0 ? "private" : value;
|
|
714
|
-
}
|
|
715
|
-
async function runBlockingLoadersBeforeTarget(routeLoaders, targetLoader, requestEv) {
|
|
716
|
-
for (const loader of routeLoaders) {
|
|
717
|
-
if (loader === targetLoader) return;
|
|
718
|
-
if (loader.__blockSSR) await loadRouteLoader(loader, requestEv);
|
|
719
|
-
}
|
|
720
|
-
}
|
|
721
|
-
function setRouteLoaderData(requestEv, routeLoaders, route) {
|
|
722
|
-
if (route?.$loaderPaths$) Object.assign(getRouteLoaderCtx(requestEv).loaderPaths, route.$loaderPaths$);
|
|
723
|
-
if (route?.$loaderParams$) Object.assign(getRouteLoaderParams(requestEv), route.$loaderParams$);
|
|
724
|
-
setRouteLoaders(requestEv, routeLoaders);
|
|
725
|
-
}
|
|
726
|
-
/** Resolve eTag from a static string or function (before running the loader). */
|
|
727
|
-
function resolvePreETag(eTagOption, requestEv) {
|
|
728
|
-
if (typeof eTagOption === "string") return normalizeETag(eTagOption);
|
|
729
|
-
const result = eTagOption(requestEv);
|
|
730
|
-
return result ? normalizeETag(result) : "";
|
|
731
|
-
}
|
|
732
|
-
async function sendLoaderResponse(requestEv, data, cacheControl) {
|
|
733
|
-
requestEv.headers.set("Content-Type", "application/json; charset=utf-8");
|
|
734
|
-
addVaryHeader(requestEv, FULLPATH_HEADER);
|
|
735
|
-
if (cacheControl !== null && !requestEv.headers.has("Cache-Control")) requestEv.cacheControl(cacheControl);
|
|
736
|
-
requestEv.send(200, data);
|
|
737
|
-
}
|
|
738
|
-
function addVaryHeader(requestEv, value) {
|
|
739
|
-
const vary = requestEv.headers.get("Vary");
|
|
740
|
-
if (!vary) {
|
|
741
|
-
requestEv.headers.set("Vary", value);
|
|
742
|
-
return;
|
|
743
|
-
}
|
|
744
|
-
if (!vary.split(",").map((item) => item.trim().toLowerCase()).includes(value.toLowerCase())) requestEv.headers.set("Vary", `${vary}, ${value}`);
|
|
745
|
-
}
|
|
746
|
-
/** Serialize and send a JSON response (used by error/redirect paths in jsonRequestWrapper). */
|
|
747
|
-
async function sendJsonResponse(requestEv, responseData, status = 200) {
|
|
748
|
-
const data = await _serialize(responseData);
|
|
749
|
-
requestEv.headers.set("Content-Type", "application/json; charset=utf-8");
|
|
750
|
-
if (!requestEv.headers.has("Cache-Control")) requestEv.cacheControl("private");
|
|
751
|
-
requestEv.send(status, data);
|
|
752
|
-
}
|
|
753
|
-
async function sendActionResponse(requestEv, responseData) {
|
|
754
|
-
const data = await _serialize(responseData);
|
|
755
|
-
requestEv.headers.set("Content-Type", "application/json; charset=utf-8");
|
|
756
|
-
requestEv.send(responseData.s || 200, data);
|
|
757
|
-
}
|
|
758
|
-
/**
|
|
759
|
-
* Early handler that wraps `next()` for JSON API requests (q-loader and q-action).
|
|
760
|
-
*
|
|
761
|
-
* For `IsQLoader` requests, it also rewrites the URL using the `X-Qwik-fullpath` header so that
|
|
762
|
-
* downstream middleware sees the real page URL.
|
|
763
|
-
*
|
|
764
|
-
* By calling `await next()` inside a try/catch, middleware redirects and errors are captured and
|
|
765
|
-
* returned as JSON envelopes instead of HTTP redirects/error pages. This keeps SPA navigation
|
|
766
|
-
* intact on the client.
|
|
767
|
-
*/
|
|
768
|
-
function jsonRequestWrapper() {
|
|
769
|
-
return async (requestEvent) => {
|
|
770
|
-
const requestEv = requestEvent;
|
|
771
|
-
const isLoader = requestEv.internalRequest === "loader";
|
|
772
|
-
const isActionJson = requestEv.internalRequest === "action";
|
|
773
|
-
if (!isLoader && !isActionJson) return;
|
|
774
|
-
if (isLoader) {
|
|
775
|
-
addVaryHeader(requestEv, FULLPATH_HEADER);
|
|
776
|
-
const pagePath = requestEv.request.headers.get(FULLPATH_HEADER);
|
|
777
|
-
const pagePathname = resolveValidInternalFullPathname(requestEv.url.pathname, pagePath);
|
|
778
|
-
if (pagePathname) requestEv.url.pathname = pagePathname;
|
|
779
|
-
}
|
|
780
|
-
try {
|
|
781
|
-
await requestEv.next();
|
|
782
|
-
} catch (err) {
|
|
783
|
-
if (requestEv.headersSent) return;
|
|
784
|
-
if (err instanceof RedirectMessage) {
|
|
785
|
-
const location = requestEv.headers.get("Location") || "/";
|
|
786
|
-
requestEv.headers.delete("Location");
|
|
787
|
-
if (isLoader) await sendJsonResponse(requestEv, { r: location });
|
|
788
|
-
else await sendActionResponse(requestEv, { redirect: location });
|
|
789
|
-
} else if (err instanceof ServerError) {
|
|
790
|
-
if (isLoader) await sendJsonResponse(requestEv, { e: err });
|
|
791
|
-
else await sendActionResponse(requestEv, {
|
|
792
|
-
e: err,
|
|
793
|
-
s: err.status
|
|
794
|
-
});
|
|
795
|
-
} else if (err instanceof Error) {
|
|
796
|
-
console.error("JSON request error:", err);
|
|
797
|
-
const message = isDev ? `${err.message}\n(this is only visible in dev mode)` : "Internal Server Error";
|
|
798
|
-
const se = new ServerError(500, message);
|
|
799
|
-
if (isLoader) await sendJsonResponse(requestEv, { e: se });
|
|
800
|
-
else await sendActionResponse(requestEv, {
|
|
801
|
-
e: se,
|
|
802
|
-
s: 500
|
|
803
|
-
});
|
|
804
|
-
} else throw err;
|
|
805
|
-
}
|
|
806
|
-
};
|
|
807
|
-
}
|
|
808
755
|
/**
|
|
809
756
|
* Handler for action requests (`?qaction={actionId}`).
|
|
810
757
|
*
|
|
@@ -888,6 +835,21 @@ async function measure$1(requestEv, name, fn) {
|
|
|
888
835
|
}
|
|
889
836
|
}
|
|
890
837
|
/**
|
|
838
|
+
* Answers `q-route.json` requests with the pathname the URL finally routes to, after any
|
|
839
|
+
* `rewrite()`. It sits after the plugins and route middleware, so a rewrite has already re-resolved
|
|
840
|
+
* the route by the time it runs. Lets the client load a rewrite target it can't match by itself.
|
|
841
|
+
*
|
|
842
|
+
* The response carries the headers set so far, so a middleware can make a rewrite cacheable with
|
|
843
|
+
* `cacheControl()` / `headers.set('Cache-Tag', ...)` before `throw rewrite(...)`.
|
|
844
|
+
*/
|
|
845
|
+
function routeResolveHandler() {
|
|
846
|
+
return (requestEv) => {
|
|
847
|
+
if (!requestEv.sharedMap.has("@isQRoute") || requestEv.headersSent || requestEv.exited) return;
|
|
848
|
+
if (!requestEv.headers.has("Cache-Control")) requestEv.cacheControl("private");
|
|
849
|
+
requestEv.json(200, { p: requestEv.url.pathname });
|
|
850
|
+
};
|
|
851
|
+
}
|
|
852
|
+
/**
|
|
891
853
|
* A set of static paths generated by the SSG, including loader json. Routes always have a trailing
|
|
892
854
|
* slash, short for `.../index.html`
|
|
893
855
|
*/
|
|
@@ -1306,6 +1268,7 @@ function createResolveRequestHandlers() {
|
|
|
1306
1268
|
else requestHandlers.unshift(csrfCheckMiddleware);
|
|
1307
1269
|
}
|
|
1308
1270
|
if (isPageRoute) {
|
|
1271
|
+
requestHandlers.push(routeResolveHandler());
|
|
1309
1272
|
requestHandlers.push(loaderHandler(routeLoaders, route));
|
|
1310
1273
|
requestHandlers.push(actionHandler(routeActions));
|
|
1311
1274
|
requestHandlers.push(route.$notFound$ ? fixStaticTrailingSlash : fixTrailingSlash);
|
|
@@ -1628,14 +1591,12 @@ function createResolveRequestHandlers() {
|
|
|
1628
1591
|
checkCSRF(requestEv);
|
|
1629
1592
|
}
|
|
1630
1593
|
function checkCSRF(requestEv, laxProto) {
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
if (forbidden) throw requestEv.error(403, `CSRF check failed. Cross-site ${requestEv.method} form submissions are forbidden.
|
|
1594
|
+
const inputOrigin = requestEv.request.headers.get("origin");
|
|
1595
|
+
const origin = requestEv.url.origin;
|
|
1596
|
+
let forbidden = inputOrigin !== origin;
|
|
1597
|
+
if (forbidden && laxProto && inputOrigin?.replace(/^http(s)?/g, "") === origin.replace(/^http(s)?/g, "")) forbidden = false;
|
|
1598
|
+
if (forbidden) throw requestEv.error(403, `CSRF check failed. Cross-site ${requestEv.method} requests are forbidden.
|
|
1637
1599
|
The request origin "${inputOrigin}" does not match the server origin "${origin}".`);
|
|
1638
|
-
}
|
|
1639
1600
|
}
|
|
1640
1601
|
function renderQwikMiddleware(render, trustForwardedHeaders = false) {
|
|
1641
1602
|
return async (requestEv) => {
|
|
@@ -1825,7 +1786,8 @@ async function runNext(requestEv, rebuildRouteInfo, resolve) {
|
|
|
1825
1786
|
const message = "Resource Not Found";
|
|
1826
1787
|
requestEv.status(status);
|
|
1827
1788
|
requestEv.html(status, getErrorHtml(status, message));
|
|
1828
|
-
|
|
1789
|
+
requestEv.sharedMap.set(RequestEvSentServerError, new ServerError(status, message));
|
|
1790
|
+
return;
|
|
1829
1791
|
}
|
|
1830
1792
|
let rewriteAttempt = 1;
|
|
1831
1793
|
async function runOnce() {
|
|
@@ -1834,7 +1796,7 @@ async function runNext(requestEv, rebuildRouteInfo, resolve) {
|
|
|
1834
1796
|
} catch (e) {
|
|
1835
1797
|
if (e instanceof RedirectMessage) {
|
|
1836
1798
|
await requestEv.getWritableStream().close();
|
|
1837
|
-
return
|
|
1799
|
+
return;
|
|
1838
1800
|
} else if (e instanceof RewriteMessage) {
|
|
1839
1801
|
if (rewriteAttempt > 50) return /* @__PURE__ */ new Error(`Infinite rewrite loop`);
|
|
1840
1802
|
rewriteAttempt += 1;
|
|
@@ -1846,14 +1808,16 @@ async function runNext(requestEv, rebuildRouteInfo, resolve) {
|
|
|
1846
1808
|
} else if (e instanceof AbortMessage) return;
|
|
1847
1809
|
else if (e instanceof ServerError && !requestEv.headersSent) {
|
|
1848
1810
|
const status = e.status;
|
|
1811
|
+
requestEv.headers.set("Cache-Control", "no-store");
|
|
1849
1812
|
const accept = requestEv.request.headers.get("Accept");
|
|
1850
1813
|
if (accept && !accept.includes("text/html")) {
|
|
1851
1814
|
requestEv.headers.set("Content-Type", "application/qwik-json");
|
|
1852
1815
|
requestEv.send(status, await _serialize(e.data));
|
|
1853
1816
|
} else requestEv.html(status, getErrorHtml(status, e.data));
|
|
1854
|
-
|
|
1817
|
+
requestEv.sharedMap.set(RequestEvSentServerError, e);
|
|
1818
|
+
return;
|
|
1855
1819
|
}
|
|
1856
|
-
if (!isDev$1) try {
|
|
1820
|
+
if (!isDev$1 || requestEv.headersSent) try {
|
|
1857
1821
|
if (!requestEv.headersSent) {
|
|
1858
1822
|
requestEv.headers.set("content-type", "text/html; charset=utf-8");
|
|
1859
1823
|
requestEv.cacheControl({ noCache: true });
|
|
@@ -1862,7 +1826,7 @@ async function runNext(requestEv, rebuildRouteInfo, resolve) {
|
|
|
1862
1826
|
const stream = requestEv.getWritableStream();
|
|
1863
1827
|
if (!stream.locked) {
|
|
1864
1828
|
const writer = stream.getWriter();
|
|
1865
|
-
await writer.write(encoder.encode(getErrorHtml(500, "Internal Server Error")));
|
|
1829
|
+
await writer.write(encoder.encode(getErrorHtml(500, isDev$1 ? e : "Internal Server Error")));
|
|
1866
1830
|
await writer.close();
|
|
1867
1831
|
}
|
|
1868
1832
|
} catch {
|
|
@@ -1877,4 +1841,4 @@ async function runNext(requestEv, rebuildRouteInfo, resolve) {
|
|
|
1877
1841
|
if (!requestEv.isDirty()) resolve(null);
|
|
1878
1842
|
}
|
|
1879
1843
|
}
|
|
1880
|
-
export { getStaticPathRedirect as a,
|
|
1844
|
+
export { getStaticPathRedirect as a, RequestEvHttpStatusMessage as c, mergeHeadersCookies as d, resolveLoaderPagePathname as f, resolveRequestHandlers as i, RequestEvSentServerError as l, clearSsrCache as m, getErrorHtml as n, isStaticPath as o, clearLoaderCache as p, renderQwikMiddleware as r, RequestEvCaughtError as s, runQwikRouter as t, RewriteMessage as u };
|