Skip to content

Commit

Permalink
Fix issue with useQuery returning loading: true state during SSR …
Browse files Browse the repository at this point in the history
…with `skip: true` (#9679)
  • Loading branch information
nathanmarks authored May 5, 2022
1 parent f9097a2 commit 95c8a1a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
- Prevent `useLazyQuery` from making duplicate requests when its execution function is first called, and stop rejecting the `Promise` it returns when `result.error` is defined. <br/>
[@benjamn](https://github.com/benjamn) in [#9684](https://github.com/apollographql/apollo-client/pull/9684)

- Fix issue with `useQuery` returning `loading: true` state during server-side rendering with `skip: true`. <br/>
[@nathanmarks](https://github.com/nathanmarks) in [#9679](https://github.com/apollographql/apollo-client/pull/9679)

## Apollo Client 3.6.2 (2022-05-02)

### Bug Fixes
Expand Down
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 95c8a1a

Please sign in to comment.