Skip to content

Commit

Permalink
Fix skip causing loading to always be true during SSR (#7567)
Browse files Browse the repository at this point in the history
PR #7310 introduced a regression that caused skipped SSR queries to
always set `loading` to `true`, which doesn't match the behavior of
`useQuery()` on the client and can result in hydration mismatches.

The problem is that `skip: true` was being treated as equivalent to
`ssr: false`, but they're not actually equivalent. I think the correct
solution is to let `this.getQueryResult()` provide a suitable result
when `skip` is truthy, which ensures that skipped SSR queries will get
the same result as skipped non-SSR queries.

Fixes #7380

Co-authored-by: Ben Newman <ben@apollographql.com>
  • Loading branch information
rgrove and benjamn authored Jan 14, 2021
1 parent 24f7429 commit 00284d2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## Apollo Client 3.3.7

### Bug Fixes

- Fix a regression due to [#7310](https://github.com/apollographql/apollo-client/pull/7310) that caused `loading` always to be `true` for `skip: true` results during server-side rendering. <br/>
[@rgrove](https://github.com/rgrove) in [#7567](https://github.com/apollographql/apollo-client/pull/7567)

## Apollo Client 3.3.6

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion src/react/data/OperationData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export abstract class OperationData<TOptions = any> {
TOptions
>;
public context: any = {};
public client: ApolloClient<object> | undefined;
public client: ApolloClient<object>;

private options: CommonOptions<TOptions> = {} as CommonOptions<TOptions>;

Expand Down
21 changes: 13 additions & 8 deletions src/react/data/QueryData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export class QueryData<TData, TVariables> extends OperationData {

private getExecuteSsrResult() {
const { ssr, skip } = this.getOptions();
const ssrDisabled = ssr === false || skip;
const ssrDisabled = ssr === false;
const fetchDisabled = this.refreshClient().client.disableNetworkFetches;

const ssrLoading = {
Expand All @@ -175,11 +175,15 @@ export class QueryData<TData, TVariables> extends OperationData {

let result;
if (this.ssrInitiated()) {
result =
this.context.renderPromises!.addQueryPromise(
this,
this.getQueryResult
) || ssrLoading;
if (skip) {
result = this.getQueryResult();
} else {
result =
this.context.renderPromises!.addQueryPromise(
this,
this.getQueryResult
) || ssrLoading;
};
}

return result;
Expand Down Expand Up @@ -333,7 +337,7 @@ export class QueryData<TData, TVariables> extends OperationData {
}

private getQueryResult = (): QueryResult<TData, TVariables> => {
let result: any = this.observableQueryFields();
let result = this.observableQueryFields() as QueryResult<TData, TVariables>;
const options = this.getOptions();

// When skipping a query (ie. we're not querying for data but still want
Expand All @@ -352,7 +356,8 @@ export class QueryData<TData, TVariables> extends OperationData {
data: undefined,
error: undefined,
loading: false,
called: true
networkStatus: NetworkStatus.ready,
called: true,
};
} else if (this.currentObservable) {
// Fetch the current result (if any) from the store.
Expand Down
18 changes: 11 additions & 7 deletions src/react/ssr/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,17 @@ describe('useQuery Hook SSR', () => {
it('should skip SSR tree rendering if `skip` option is `true`', async () => {
let renderCount = 0;
const Component = () => {
const { data, loading } = useQuery(CAR_QUERY, { skip: true });
const {
loading,
networkStatus,
data,
} = useQuery(CAR_QUERY, { skip: true });
renderCount += 1;

if (!loading) {
const { make } = data.cars[0];
return <div>{make}</div>;
}
expect(loading).toBeFalsy();
expect(networkStatus).toBe(7);
expect(data).toBeUndefined();

return null;
};

Expand All @@ -186,9 +190,9 @@ describe('useQuery Hook SSR', () => {
</MockedProvider>
);

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

0 comments on commit 00284d2

Please sign in to comment.