Skip to content

Commit

Permalink
Fix issue with useQuery returning loading state in SSR with skip
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanmarks authored and benjamn committed May 5, 2022
1 parent f9097a2 commit ffa7a2e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/react/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ class InternalState<TData, TVariables> {

if (
(this.renderPromises || this.client.disableNetworkFetches) &&
this.queryHookOptions.ssr === false
this.queryHookOptions.ssr === false &&
!this.queryHookOptions.skip
) {
// If SSR has been explicitly disabled, and this function has been called
// on the server side, return the default loading state.
Expand Down
28 changes: 27 additions & 1 deletion src/react/ssr/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ describe('useQuery Hook SSR', () => {
return renderToStringWithData(app);
});

it('should skip SSR tree rendering if `ssr` option is `false`', async () => {
it('should skip SSR tree rendering and return a loading state if `ssr` option is `false`', async () => {
let renderCount = 0;
const Component = () => {
const { data, loading } = useQuery(CAR_QUERY, { ssr: false });
renderCount += 1;

expect(loading).toBeTruthy();

if (!loading) {
const { make } = data.cars[0];
return <div>{make}</div>;
Expand All @@ -108,6 +110,30 @@ describe('useQuery Hook SSR', () => {
});
});

it('should skip SSR tree rendering and not return a loading state loading if `ssr` option is `false` and `skip` is `true`', async () => {
let renderCount = 0;
const Component = () => {
const { data, loading } = useQuery(CAR_QUERY, { ssr: false, skip: true });
renderCount += 1;

expect(loading).toBeFalsy();
expect(data).toBeUndefined();

return null;
};

const app = (
<MockedProvider mocks={CAR_MOCKS}>
<Component />
</MockedProvider>
);

return renderToStringWithData(app).then(result => {
expect(renderCount).toBe(1);
expect(result).toEqual('');
});
});

it('should skip both SSR tree rendering and SSR component rendering if `ssr` option is `false` and `ssrMode` is `true`',
async () => {
const link = mockSingleLink({
Expand Down

0 comments on commit ffa7a2e

Please sign in to comment.