Skip to content

Commit

Permalink
(react/preact) - Fix suspense-ssr runs (#1138)
Browse files Browse the repository at this point in the history
* fix suspense-ssr runs

* Update .changeset/cool-news-buy.md

Co-authored-by: Phil Pluckthun <[email protected]>

Co-authored-by: Phil Pluckthun <[email protected]>
  • Loading branch information
JoviDeCroock and kitten authored Nov 8, 2020
1 parent 93d8df7 commit bba7aec
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/cool-news-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@urql/preact': patch
'urql': patch
---

Fix server-side rendering by disabling the new Suspense cache on the server-side and clear it for prepasses.
10 changes: 8 additions & 2 deletions packages/preact-urql/src/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export function useQuery<Data = any, Variables = object>(
let source: Source<OperationResult> | void = suspense
? sources.get(request.key)
: undefined;

if (!source) {
source = client.executeQuery(request, {
requestPolicy: args.requestPolicy,
Expand All @@ -117,8 +118,12 @@ export function useQuery<Data = any, Variables = object>(
});

// Create a suspense source and cache it for the given request
if (suspense)
sources.set(request.key, (source = toSuspenseSource(source)));
if (suspense) {
source = toSuspenseSource(source);
if (typeof window !== 'undefined') {
sources.set(request.key, source);
}
}
}

return source;
Expand Down Expand Up @@ -182,6 +187,7 @@ export function useQuery<Data = any, Variables = object>(
}, [update, client, query$, request, args.context]);

if (isSuspense(client, args.context)) {
sources.delete(request.key); // Delete any cached suspense source
update(query$);
}

Expand Down
10 changes: 8 additions & 2 deletions packages/react-urql/src/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export function useQuery<Data = any, Variables = object>(
let source: Source<OperationResult> | void = suspense
? sources.get(request.key)
: undefined;

if (!source) {
source = client.executeQuery(request, {
requestPolicy: args.requestPolicy,
Expand All @@ -117,8 +118,12 @@ export function useQuery<Data = any, Variables = object>(
});

// Create a suspense source and cache it for the given request
if (suspense)
sources.set(request.key, (source = toSuspenseSource(source)));
if (suspense) {
source = toSuspenseSource(source);
if (typeof window !== 'undefined') {
sources.set(request.key, source);
}
}
}

return source;
Expand Down Expand Up @@ -182,6 +187,7 @@ export function useQuery<Data = any, Variables = object>(
}, [update, client, query$, request, args.context]);

if (isSuspense(client, args.context)) {
sources.delete(request.key); // Delete any cached suspense source
update(query$);
}

Expand Down

0 comments on commit bba7aec

Please sign in to comment.