Skip to content

Commit 9038640

Browse files
Varixowmertens
authored andcommitted
fix: handle q-loaders-data and q-loader in middlewares
1 parent 2dd2417 commit 9038640

File tree

4 files changed

+48
-29
lines changed

4 files changed

+48
-29
lines changed

packages/qwik-router/src/middleware/request-handler/loader-endpoints.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export function loaderDataHandler(routeLoaders: LoaderInternal[]): RequestHandle
4141
let loaderRoute = qwikRouterConfig.loaderIdToRoute[loaderId];
4242
const params = requestEv.params;
4343
if (Object.keys(params).length > 0) {
44+
// TODO: use RequestEvRoute?
4445
const pathname = getPathnameForDynamicRoute(
4546
requestEv.url.pathname,
4647
Object.keys(params),

packages/qwik-router/src/middleware/request-handler/request-event.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
IsQData,
3636
IsQLoader,
3737
IsQLoaderData,
38+
OriginalQDataName,
3839
Q_LOADER_DATA_REGEX,
3940
QDATA_JSON,
4041
QDATA_JSON_LEN,
@@ -82,6 +83,7 @@ export function createRequestEvent(
8283
const requestRecognized = recognizeRequest(url.pathname);
8384
if (requestRecognized) {
8485
sharedMap.set(requestRecognized.type, true);
86+
sharedMap.set(OriginalQDataName, requestRecognized.originalQDataName);
8587
if (requestRecognized.type === IsQLoader && requestRecognized.data) {
8688
sharedMap.set(QLoaderId, requestRecognized.data.loaderId);
8789
}
@@ -470,6 +472,7 @@ export function recognizeRequest(pathname: string) {
470472
return {
471473
type: IsQData,
472474
trimLength: QDATA_JSON_LEN,
475+
originalQDataName: QDATA_JSON,
473476
data: null,
474477
};
475478
}
@@ -480,6 +483,7 @@ export function recognizeRequest(pathname: string) {
480483
return {
481484
type: IsQLoaderData,
482485
trimLength: loaderDataMatch[0].length,
486+
originalQDataName: loaderDataMatch[1],
483487
data: null,
484488
};
485489
}
@@ -489,7 +493,8 @@ export function recognizeRequest(pathname: string) {
489493
return {
490494
type: IsQLoader,
491495
trimLength: loaderMatch[0].length,
492-
data: { loaderId: loaderMatch[1] },
496+
originalQDataName: loaderMatch[1],
497+
data: { loaderId: loaderMatch[2] },
493498
};
494499
}
495500

packages/qwik-router/src/middleware/request-handler/resolve-request-handlers.ts

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type QRL } from '@qwik.dev/core';
2-
import { _serialize, _UNINITIALIZED, _verifySerializable } from '@qwik.dev/core/internal';
2+
import { _serialize, _verifySerializable } from '@qwik.dev/core/internal';
33
import type { Render, RenderToStringResult } from '@qwik.dev/core/server';
44
import { QACTION_KEY, QFN_KEY } from '../../runtime/src/constants';
55
import {
@@ -13,25 +13,26 @@ import {
1313
type RouteModule,
1414
} from '../../runtime/src/types';
1515
import { HttpStatus } from './http-status-codes';
16+
import {
17+
executeLoader,
18+
loaderDataHandler,
19+
runValidators,
20+
singleLoaderHandler,
21+
} from './loader-endpoints';
22+
import { qDataHandler } from './qdata-endpoints';
1623
import {
1724
RequestEvShareQData,
1825
RequestEvShareServerTiming,
1926
RequestEvSharedActionId,
2027
RequestRouteName,
2128
getRequestLoaders,
2229
getRequestMode,
30+
recognizeRequest,
2331
type RequestEventInternal,
2432
} from './request-event';
2533
import { getQwikRouterServerData } from './response-page';
2634
import type { ErrorCodes, RequestEvent, RequestEventBase, RequestHandler } from './types';
27-
import { IsQData, QDATA_JSON } from './user-response';
28-
import {
29-
executeLoader,
30-
singleLoaderHandler,
31-
runValidators,
32-
loaderDataHandler,
33-
} from './loader-endpoints';
34-
import { qDataHandler } from './qdata-endpoints';
35+
import { IsQData, IsQLoader, IsQLoaderData, OriginalQDataName } from './user-response';
3536
// Import separately to avoid duplicate imports in the vite dev server
3637
import { RedirectMessage, ServerError } from '@qwik.dev/router/middleware/request-handler';
3738

@@ -80,24 +81,21 @@ export const resolveRequestHandlers = (
8081
requestHandlers.unshift(csrfCheckMiddleware);
8182
}
8283
}
84+
requestHandlers.push(handleRedirect);
8385
if (isPageRoute) {
8486
// server$
8587
if (method === 'POST' || method === 'GET') {
8688
requestHandlers.push(pureServerFunction);
8789
}
8890

89-
requestHandlers.push(fixTrailingSlash);
90-
requestHandlers.push(loaderDataHandler(routeLoaders));
91-
requestHandlers.push(singleLoaderHandler(routeLoaders));
92-
requestHandlers.push(qDataHandler);
93-
}
94-
requestHandlers.push(handleRedirect);
95-
96-
if (isPageRoute) {
9791
requestHandlers.push((ev) => {
9892
// Set the current route name
9993
ev.sharedMap.set(RequestRouteName, routeName);
10094
});
95+
requestHandlers.push(fixTrailingSlash);
96+
requestHandlers.push(loaderDataHandler(routeLoaders));
97+
requestHandlers.push(singleLoaderHandler(routeLoaders));
98+
requestHandlers.push(qDataHandler);
10199
requestHandlers.push(actionsMiddleware(routeActions));
102100
requestHandlers.push(loadersMiddleware(routeLoaders));
103101
requestHandlers.push(renderHandler);
@@ -313,7 +311,8 @@ async function pureServerFunction(ev: RequestEvent) {
313311
function fixTrailingSlash(ev: RequestEvent) {
314312
const { basePathname, originalUrl, sharedMap } = ev;
315313
const { pathname, search } = originalUrl;
316-
const isQData = sharedMap.has(IsQData);
314+
const isQData =
315+
sharedMap.has(IsQData) || sharedMap.has(IsQLoaderData) || sharedMap.has(IsQLoader);
317316
if (!isQData && pathname !== basePathname && !pathname.endsWith('.html')) {
318317
// only check for slash redirect on pages
319318
if (!globalThis.__NO_TRAILING_SLASH__) {
@@ -357,8 +356,11 @@ export function isLastModulePageRoute(routeModules: RouteModule[]) {
357356

358357
export function getPathname(url: URL) {
359358
url = new URL(url);
360-
if (url.pathname.endsWith(QDATA_JSON)) {
361-
url.pathname = url.pathname.slice(0, -QDATA_JSON.length);
359+
360+
const qDataInfo = recognizeRequest(url.pathname);
361+
362+
if (qDataInfo) {
363+
url.pathname = url.pathname.slice(0, -qDataInfo.trimLength);
362364
}
363365
if (!globalThis.__NO_TRAILING_SLASH__) {
364366
if (!url.pathname.endsWith('/')) {
@@ -417,7 +419,10 @@ export function renderQwikMiddleware(render: Render) {
417419
if (requestEv.headersSent) {
418420
return;
419421
}
420-
const isPageDataReq = requestEv.sharedMap.has(IsQData);
422+
const isPageDataReq =
423+
requestEv.sharedMap.has(IsQData) ||
424+
requestEv.sharedMap.has(IsQLoaderData) ||
425+
requestEv.sharedMap.has(IsQLoader);
421426
if (isPageDataReq) {
422427
return;
423428
}
@@ -469,7 +474,10 @@ export function renderQwikMiddleware(render: Render) {
469474
}
470475

471476
export async function handleRedirect(requestEv: RequestEvent) {
472-
const isPageDataReq = requestEv.sharedMap.has(IsQData);
477+
const isPageDataReq =
478+
requestEv.sharedMap.has(IsQData) ||
479+
requestEv.sharedMap.has(IsQLoaderData) ||
480+
requestEv.sharedMap.has(IsQLoader);
473481
if (!isPageDataReq) {
474482
return;
475483
}
@@ -490,7 +498,7 @@ export async function handleRedirect(requestEv: RequestEvent) {
490498
const isRedirect = status >= 301 && status <= 308 && location;
491499

492500
if (isRedirect) {
493-
const adaptedLocation = makeQDataPath(location);
501+
const adaptedLocation = makeQDataPath(location, requestEv.sharedMap);
494502
if (adaptedLocation) {
495503
requestEv.headers.set('Location', adaptedLocation);
496504
requestEv.getWritableStream().close();
@@ -502,12 +510,16 @@ export async function handleRedirect(requestEv: RequestEvent) {
502510
}
503511
}
504512

505-
function makeQDataPath(href: string) {
513+
function makeQDataPath(href: string, sharedMap: Map<string, unknown>) {
506514
if (href.startsWith('/')) {
507-
const append = QDATA_JSON;
508515
const url = new URL(href, 'http://localhost');
509-
510516
const pathname = url.pathname.endsWith('/') ? url.pathname.slice(0, -1) : url.pathname;
517+
const append = sharedMap.get(OriginalQDataName) as string;
518+
519+
if (!append) {
520+
return undefined;
521+
}
522+
511523
return pathname + (append.startsWith('/') ? '' : '/') + append + url.search;
512524
} else {
513525
return undefined;

packages/qwik-router/src/middleware/request-handler/user-response.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ export const IsQData = '@isQData';
186186
export const IsQLoader = '@isQLoader';
187187
export const QLoaderId = '@qLoaderId';
188188
export const IsQLoaderData = '@isQLoaderData';
189+
export const OriginalQDataName = '@originalQDataName';
189190
export const QDATA_JSON = '/q-data.json';
190191
export const QDATA_JSON_LEN = QDATA_JSON.length;
191-
export const Q_LOADER_DATA_REGEX = /\/q-loader-data\.(.+)\.json$/;
192-
export const SINGLE_LOADER_REGEX = /\/q-loader-(.+)\.(.+)\.json$/;
192+
export const Q_LOADER_DATA_REGEX = /\/(q-loader-data\.(.+)\.json)$/;
193+
export const SINGLE_LOADER_REGEX = /\/(q-loader-(.+)\.(.+)\.json)$/;

0 commit comments

Comments
 (0)