diff --git a/.changeset/core-locale-node-builtin-import.md b/.changeset/core-locale-node-builtin-import.md new file mode 100644 index 00000000000..2d7198ee7e6 --- /dev/null +++ b/.changeset/core-locale-node-builtin-import.md @@ -0,0 +1,5 @@ +--- +'@qwik.dev/core': patch +--- + +fix(core): hide `node:async_hooks` import from non-Qwik bundlers (e.g. cypress E2E) diff --git a/packages/qwik/src/core/shared/platform/async-local-storage.ts b/packages/qwik/src/core/shared/platform/async-local-storage.ts new file mode 100644 index 00000000000..26c9bdcd5cd --- /dev/null +++ b/packages/qwik/src/core/shared/platform/async-local-storage.ts @@ -0,0 +1,15 @@ +import type { AsyncLocalStorage } from 'node:async_hooks'; + +type ProcessWithBuiltins = { + getBuiltinModule?: (id: string) => unknown; +}; + +/** @internal */ +export const getAsyncLocalStorage = () => { + const process = (globalThis as { process?: ProcessWithBuiltins }).process; + return ( + process?.getBuiltinModule?.('node:async_hooks') as + | { AsyncLocalStorage?: new () => AsyncLocalStorage } + | undefined + )?.AsyncLocalStorage; +}; diff --git a/packages/qwik/src/core/use/use-locale.ts b/packages/qwik/src/core/use/use-locale.ts index c1476d60d25..8f019259862 100644 --- a/packages/qwik/src/core/use/use-locale.ts +++ b/packages/qwik/src/core/use/use-locale.ts @@ -1,4 +1,5 @@ import { tryGetInvokeContext } from './use-core'; +import { getAsyncLocalStorage } from '../shared/platform/async-local-storage'; import { isServer } from '@qwik.dev/core/build'; import type { AsyncLocalStorage } from 'node:async_hooks'; @@ -7,13 +8,10 @@ let _locale: string | undefined = undefined; let localAsyncStore: AsyncLocalStorage | undefined; if (isServer) { - import('node:async_hooks') - .then((module) => { - localAsyncStore = new module.AsyncLocalStorage(); - }) - .catch(() => { - // ignore if AsyncLocalStorage is not available - }); + const AsyncLocalStorage = getAsyncLocalStorage(); + if (AsyncLocalStorage) { + localAsyncStore = new AsyncLocalStorage(); + } } /**