Skip to content

Commit c678479

Browse files
committed
fix(qrl): handle window reference errors in server mode
Add try-catch block to handle ReferenceError for 'window' in server mode during development. Improve error message to guide developers to use 'if (isBrowser)' checks when needed. Remove duplicate error handling code that was previously in the invoke function since it's now handled at the qrl level.
1 parent 3289e51 commit c678479

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

packages/qwik/src/core/shared/qrl/qrl-class.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,19 @@ export const createQRL = <TYPE>(
7474
}
7575

7676
let _containerEl: Element | undefined;
77-
7877
const qrl = async function (this: unknown, ...args: QrlArgs<TYPE>) {
79-
const boundedFn = bindFnToContext.call(this, tryGetInvokeContext());
80-
const result = await boundedFn(...args);
81-
return result;
78+
try {
79+
const boundedFn = bindFnToContext.call(this, tryGetInvokeContext());
80+
const result = await boundedFn(...args);
81+
return result;
82+
} catch (e) {
83+
if (isDev && isServer && e instanceof ReferenceError) {
84+
if (e.message.includes('window')) {
85+
e.message = 'It seems like you forgot to add "if (isBrowser) {...}" here:' + e.message;
86+
}
87+
throw e;
88+
}
89+
}
8290
} as QRLInternal<TYPE>;
8391

8492
const setContainer = (el: Element | undefined) => {
@@ -116,19 +124,6 @@ export const createQRL = <TYPE>(
116124
context.$event$ ||= this as Event;
117125
try {
118126
return invoke.call(this, context, symbolRef as any, ...(args as any));
119-
} catch (e) {
120-
if (isDev) {
121-
if (isServer) {
122-
if (
123-
e instanceof ReferenceError &&
124-
['window', 'document'].some((v) => e.message.includes(v))
125-
) {
126-
e.message =
127-
'It seems like you forgot to add "if (isBrowser) {...}" here: ' + e.message;
128-
}
129-
throw e;
130-
}
131-
}
132127
} finally {
133128
context.$qrl$ = prevQrl;
134129
context.$event$ = prevEvent;

0 commit comments

Comments
 (0)