Skip to content

Commit eaff266

Browse files
committed
fix(server-functions): only throw SSR guard when 'window' is undefined
Previously the SSR‐guard in `server-functions.ts` unconditionally threw whenever `isServer` was true, which blocked invocation of `routeAction$` under Vitest+JSDOM (and the QwikCityMockProvider) in user tests. Now we narrow the guard to: if (isServer && typeof window === 'undefined') { throw …; } After this fix: - True SSR(no 'window') still throws an error as beforev - JSDOM/Vitest tests(and browsers) have a global 'window', do not throw the error, and returns a promise This will unblock testing of actions in JSDOM environments without impacting real SSR safety Closes QwikDev#5874
1 parent 788b871 commit eaff266

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

.changeset/lovely-cycles-pay.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
'@builder.io/qwik-city': patch
3+
---
4+
5+
FIX: Bug #5874 - SSR guard: only throw in true SSR (when window is undefined), allowing actions to run in Vitest tests
6+
7+
How this change works:
8+
9+
Previously the SSR‐guard in `server-functions.ts` unconditionally threw whenever `isServer` was true,
10+
which blocked invocation of `routeAction$` under Vitest+JSDOM (and the QwikCityMockProvider) in user tests.
11+
12+
Now we narrow the guard to:
13+
if (isServer && typeof window === 'undefined') {
14+
throw …;
15+
}
16+
17+
This will ensure:
18+
19+
- True SSR (no window) still throws as before.
20+
- JSDOM/Vitest tests (and browsers) have a global window, skip the throw, and return a Promise.
21+
22+
This change unblocks testing of actions in JSDOM environments without impacting real SSR safety.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ export const routeActionQrl = ((
8888
});
8989

9090
const submit = $((input: unknown | FormData | SubmitEvent = {}) => {
91-
if (isServer) {
91+
// only throw in true SSR (no mock context)
92+
if (isServer && typeof window === 'undefined') {
9293
throw new Error(`Actions can not be invoked within the server during SSR.
9394
Action.run() can only be called on the browser, for example when a user clicks a button, or submits a form.`);
9495
}

0 commit comments

Comments
 (0)