Skip to content

Commit a50d819

Browse files
committed
refactor(router): replace asyncRequestStore with global qcAsyncRequestStore for improved async handling
1 parent 23966f2 commit a50d819

File tree

6 files changed

+22
-41
lines changed

6 files changed

+22
-41
lines changed

packages/qwik-router/global.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ type RequestEventInternal =
55
import('./middleware/request-handler/request-event').RequestEventInternal;
66
type AsyncStore = import('node:async_hooks').AsyncLocalStorage<RequestEventInternal>;
77
type SerializationStrategy = import('@qwik.dev/core/internal').SerializationStrategy;
8-
8+
declare var qcAsyncRequestStore: AsyncStore | undefined;
99
declare var _qwikActionsMap: Map<string, ActionInternal> | undefined;
1010

1111
type ExperimentalFeatures = import('@qwik.dev/core/optimizer').ExperimentalFeatures;

packages/qwik-router/src/middleware/request-handler/async-request-store.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
RewriteMessage,
2121
} from '@qwik.dev/router/middleware/request-handler';
2222
import { encoder, getRouteLoaderPromise } from './resolve-request-handlers';
23-
import { asyncRequestStore } from './async-request-store';
23+
2424
import type {
2525
CacheControl,
2626
CacheControlTarget,
@@ -81,7 +81,7 @@ export function createRequestEvent(
8181

8282
while (routeModuleIndex < requestHandlers.length) {
8383
const moduleRequestHandler = requestHandlers[routeModuleIndex];
84-
const asyncStore = asyncRequestStore;
84+
const asyncStore = globalThis.qcAsyncRequestStore;
8585
const result = asyncStore?.run
8686
? asyncStore.run(requestEv, moduleRequestHandler, requestEv)
8787
: moduleRequestHandler(requestEv);

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { getErrorHtml } from './error-handler';
99
import { createRequestEvent, getRequestMode, type RequestEventInternal } from './request-event';
1010
import { encoder } from './resolve-request-handlers';
1111
import { withLocale } from '@qwik.dev/core';
12-
import { asyncRequestStore } from './async-request-store';
1312
import type { ServerRequestEvent, StatusCodes } from './types';
1413
// Import separately to avoid duplicate imports in the vite dev server
1514
import {
@@ -19,6 +18,19 @@ import {
1918
ServerError,
2019
} from '@qwik.dev/router/middleware/request-handler';
2120

21+
let asyncStore: AsyncStore | undefined;
22+
import('node:async_hooks')
23+
.then((module) => {
24+
const AsyncLocalStorage = module.AsyncLocalStorage;
25+
asyncStore = new AsyncLocalStorage<RequestEventInternal>();
26+
globalThis.qcAsyncRequestStore = asyncStore;
27+
})
28+
.catch((err) => {
29+
console.warn(
30+
'AsyncLocalStorage not available, continuing without it. This might impact concurrent server calls.',
31+
err
32+
);
33+
});
2234
export interface QwikRouterRun<T> {
2335
response: Promise<T | null>;
2436
requestEv: RequestEvent;
@@ -47,8 +59,8 @@ export function runQwikRouter<T>(
4759
requestEv,
4860
completion: withLocale(
4961
requestEv.locale(),
50-
asyncRequestStore
51-
? () => asyncRequestStore!.run(requestEv, runNext, requestEv, rebuildRouteInfo, resolve!)
62+
asyncStore
63+
? () => asyncStore!.run(requestEv, runNext, requestEv, rebuildRouteInfo, resolve!)
5264
: () => runNext(requestEv, rebuildRouteInfo, resolve!)
5365
),
5466
};

packages/qwik-router/src/runtime/src/head.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import type {
1414
ContentModuleHead,
1515
} from './types';
1616
import { isPromise } from './utils';
17-
import { asyncRequestStore } from '../../middleware/request-handler/async-request-store';
1817

1918
export const resolveHead = (
2019
endpoint: EndpointResponse | ClientPageData,
@@ -54,25 +53,18 @@ export const resolveHead = (
5453
}
5554
}
5655
if (fns.length) {
57-
const hasAsyncStore = !!asyncRequestStore;
5856
const headProps: DocumentHeadProps = {
5957
head,
60-
withLocale: hasAsyncStore ? (fn) => fn() : (fn) => withLocale(locale, fn),
58+
withLocale: (fn) => withLocale(locale, fn),
6159
resolveValue: getData,
6260
...routeLocation,
6361
};
6462

65-
if (hasAsyncStore) {
63+
withLocale(locale, () => {
6664
for (const fn of fns) {
6765
resolveDocumentHead(head, fn(headProps));
6866
}
69-
} else {
70-
withLocale(locale, () => {
71-
for (const fn of fns) {
72-
resolveDocumentHead(head, fn(headProps));
73-
}
74-
});
75-
}
67+
});
7668
}
7769

7870
return head;

packages/qwik-router/src/runtime/src/server-functions.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ import type {
6464
import { useAction, useLocation, useQwikRouterEnv } from './use-functions';
6565

6666
import type { FormSubmitCompletedDetail } from './form-component';
67-
import { asyncRequestStore } from '../../middleware/request-handler/async-request-store';
6867
import { deepFreeze } from './deepFreeze';
6968

7069
/** @internal */
@@ -421,7 +420,7 @@ export const serverQrl = <T extends ServerFunction>(
421420

422421
if (isServer) {
423422
// Running during SSR, we can call the function directly
424-
let requestEvent = asyncRequestStore?.getStore() as RequestEvent | undefined;
423+
let requestEvent = globalThis.qcAsyncRequestStore?.getStore() as RequestEvent | undefined;
425424

426425
if (!requestEvent) {
427426
const contexts = [useQwikRouterEnv()?.ev, this, _getContextEvent()] as RequestEvent[];

0 commit comments

Comments
 (0)