Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
ssr false queries still being called on server (#3515)
Browse files Browse the repository at this point in the history
* ssr false queries still being called on server

* add test for this usecase

* my added test was not adding any value, because the test is not testing the query being executed or not since that is happening in the startQuerySubscription part

* Fully disable SSR when using `ssr: false` and `ssrMode: true`

When `ssrMode` is `true`, Apollo Client's `disableNetworkFetches`
is `true`, meaning we want all network fetches to be disabled.
Right now `ssr: false` doesn't take `disableNetworkFetches` into
consideration, but with these changes it will. So with these
changes:

- If `ssr: false` is set and we're coming from a React SSR
  `getDataFromTree` or `getMarkupFromTree` call
  (`treeRenderingInitiated` is `true`), we return a default SSR
  loading state to prevent the running of any subsequent queries
  in the same render.
- If `ssr: false` is set and we've disabled network fetching
  (`ssrMode` is `true`), we return a default SSR loading state to
  prevent the running of any subsequent queries in the same render.

* Prep for beta publish

* Publish

 - react-apollo@3.1.2-beta.0
 - @apollo/react-common@3.1.2-beta.0
 - @apollo/react-components@3.1.2-beta.0
 - @apollo/react-hoc@3.1.2-beta.0
 - @apollo/react-hooks@3.1.2-beta.0
 - @apollo/react-ssr@3.1.2-beta.0
 - @apollo/react-testing@3.1.2-beta.0

* Changelog updates
  • Loading branch information
maapteh authored and hwillson committed Oct 1, 2019
1 parent b07971e commit 7b4fc9f
Show file tree
Hide file tree
Showing 16 changed files with 141 additions and 67 deletions.
7 changes: 7 additions & 0 deletions Changelog.md
@@ -1,5 +1,12 @@
# Change log

## 3.1.2 (not yet released)

### Bug Fixes

- Make sure SSR is fully disabled when using `ssr: false` and `ssrMode: true`. <br/>
[@maapteh](https://github.com/maapteh) in [#3515](https://github.com/apollographql/react-apollo/pull/3515)

## 3.1.1 (2019-09-15)

### Improvements
Expand Down
1 change: 1 addition & 0 deletions examples/ssr/hooks/package.json
Expand Up @@ -8,6 +8,7 @@
},
"dependencies": {
"@apollo/react-hooks": "3.1.1",
"@apollo/react-ssr": "^3.1.1",
"@babel/runtime": "^7.4.5",
"apollo-cache-inmemory": "^1.6.0",
"apollo-client": "^2.6.0",
Expand Down
3 changes: 2 additions & 1 deletion examples/ssr/hooks/server/main.js
@@ -1,7 +1,8 @@
import { renderToString } from 'react-dom/server';
import { onPageLoad } from 'meteor/server-render';
import { ApolloClient } from 'apollo-client';
import { getMarkupFromTree, ApolloProvider } from '@apollo/react-hooks';
import { ApolloProvider } from '@apollo/react-hooks';
import { getMarkupFromTree } from '@apollo/react-ssr';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { HttpLink } from 'apollo-link-http';
import { WebApp } from 'meteor/webapp';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -17,7 +17,7 @@
"test:umd:ci": "npx jest --config ./config/jest.umd.config.js --ci --maxWorkers=2 --reporters=default --reporters=jest-junit --silent",
"bundlesize": "npx lerna run build && bundlesize",
"prettier": "npx prettier --config ./config/prettier.config.js --write \"./**/*.{js,jsx,ts*,md,graphql,json}\"",
"deploy": "npx lerna publish",
"deploy": "npx lerna publish --dist-tag beta",
"clean": "rm -Rf ./node_modules ./meta && npx lerna exec -- npm run clean"
},
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/all/package.json
@@ -1,7 +1,7 @@
{
"name": "react-apollo",
"description": "React Apollo Hooks, Components, and HOC.",
"version": "3.1.1",
"version": "3.1.2-beta.0",
"author": "opensource@apollographql.com",
"keywords": [
"apollo",
Expand Down Expand Up @@ -30,7 +30,7 @@
"build": "npx tsc -p ./config",
"postbuild": "npx rollup -c ./config/rollup.config.js",
"predeploy": "npm run build",
"deploy": "npm publish"
"deploy": "npm publish --tag beta"
},
"peerDependencies": {
"@types/react": "^16.8.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/common/package.json
@@ -1,7 +1,7 @@
{
"name": "@apollo/react-common",
"description": "React Apollo common utilities.",
"version": "3.1.1",
"version": "3.1.2-beta.0",
"author": "opensource@apollographql.com",
"keywords": [
"apollo",
Expand All @@ -25,7 +25,7 @@
"postbuild": "npx rollup -c ./config/rollup.config.js",
"watch": "npx tsc-watch --onSuccess \"npm run postbuild\" -p ./config",
"predeploy": "npm run build",
"deploy": "npm publish",
"deploy": "npm publish --tag beta",
"test": "npx jest --config ../../config/jest.config.js --testPathPattern packages/common",
"test:watch": "npx jest --config ../../config/jest.config.js --testPathPattern packages/common --watch",
"test:cjs": "npm run build && npx jest --config ../../config/jest.cjs.config.js --testPathPattern packages/common",
Expand Down
4 changes: 2 additions & 2 deletions packages/components/package.json
@@ -1,7 +1,7 @@
{
"name": "@apollo/react-components",
"description": "React Apollo Query, Mutation and Subscription components.",
"version": "3.1.1",
"version": "3.1.2-beta.0",
"author": "opensource@apollographql.com",
"keywords": [
"apollo",
Expand Down Expand Up @@ -29,7 +29,7 @@
"postbuild": "npx rollup -c ./config/rollup.config.js",
"watch": "npx tsc-watch --onSuccess \"npm run postbuild\" -p ./config",
"predeploy": "npm run build",
"deploy": "npm publish",
"deploy": "npm publish --tag beta",
"test": "npx jest --config ../../config/jest.config.js --testPathPattern packages/components",
"test:watch": "npx jest --config ../../config/jest.config.js --testPathPattern packages/components --watch",
"test:cjs": "npm run build && npx jest --config ../../config/jest.cjs.config.js --testPathPattern packages/components",
Expand Down
4 changes: 2 additions & 2 deletions packages/hoc/package.json
@@ -1,7 +1,7 @@
{
"name": "@apollo/react-hoc",
"description": "React Apollo `graphql` higher-order component.",
"version": "3.1.1",
"version": "3.1.2-beta.0",
"author": "opensource@apollographql.com",
"keywords": [
"apollo",
Expand Down Expand Up @@ -29,7 +29,7 @@
"postbuild": "npx rollup -c ./config/rollup.config.js",
"watch": "npx tsc-watch --onSuccess \"npm run postbuild\" -p ./config",
"predeploy": "npm run build",
"deploy": "npm publish",
"deploy": "npm publish --tag beta",
"test": "npx jest --config ../../config/jest.config.js --testPathPattern packages/hoc",
"test:watch": "npx jest --config ../../config/jest.config.js --testPathPattern packages/hoc --watch",
"test:cjs": "npm run build && npx jest --config ../../config/jest.cjs.config.js --testPathPattern packages/hoc",
Expand Down
4 changes: 2 additions & 2 deletions packages/hooks/package.json
@@ -1,7 +1,7 @@
{
"name": "@apollo/react-hooks",
"description": "React Apollo Hooks.",
"version": "3.1.1",
"version": "3.1.2-beta.0",
"author": "opensource@apollographql.com",
"keywords": [
"apollo",
Expand Down Expand Up @@ -29,7 +29,7 @@
"postbuild": "npx rollup -c ./config/rollup.config.js",
"watch": "npx tsc-watch --onSuccess \"npm run postbuild\" -p ./config",
"predeploy": "npm run build",
"deploy": "npm publish",
"deploy": "npm publish --tag beta",
"test": "npx jest --config ../../config/jest.config.js --testPathPattern packages/hooks",
"test:watch": "npx jest --config ../../config/jest.config.js --testPathPattern packages/hooks --watch",
"test:cjs": "npm run build && npx jest --config ../../config/jest.cjs.config.js --testPathPattern packages/hooks",
Expand Down
2 changes: 1 addition & 1 deletion packages/hooks/src/__tests__/useLazyQuery.test.tsx
Expand Up @@ -415,7 +415,7 @@ describe('useLazyQuery Hook', () => {
expect(loading).toEqual(false);
expect(data).toEqual(CAR_RESULT_DATA);
setTimeout(() => {
execute();
execute({ variables: { someProp: 'someValue' } });
});
break;
case 3:
Expand Down
7 changes: 5 additions & 2 deletions packages/hooks/src/data/OperationData.ts
Expand Up @@ -30,8 +30,11 @@ export abstract class OperationData<TOptions = any> {
return this.options;
}

public setOptions(newOptions: CommonOptions<TOptions>) {
if (!isEqual(this.options, newOptions)) {
public setOptions(
newOptions: CommonOptions<TOptions>,
storePrevious: boolean = false
) {
if (storePrevious && !isEqual(this.options, newOptions)) {
this.previousOptions = this.options;
}
this.options = newOptions;
Expand Down
88 changes: 44 additions & 44 deletions packages/hooks/src/data/QueryData.ts
Expand Up @@ -48,7 +48,7 @@ export class QueryData<TData, TVariables> extends OperationData {
public execute(): QueryResult<TData, TVariables> {
this.refreshClient();

const { skip, query, ssr } = this.getOptions();
const { skip, query } = this.getOptions();
if (skip || query !== this.previousData.query) {
this.removeQuerySubscription();
this.previousData.query = query;
Expand All @@ -58,9 +58,7 @@ export class QueryData<TData, TVariables> extends OperationData {

if (this.isMounted) this.startQuerySubscription();

const ssrDisabled = ssr === false;

return this.getExecuteSsrResult(ssrDisabled) || this.getExecuteResult();
return this.getExecuteSsrResult() || this.getExecuteResult();
}

public executeLazy(): QueryTuple<TData, TVariables> {
Expand Down Expand Up @@ -90,6 +88,7 @@ export class QueryData<TData, TVariables> extends OperationData {

public afterExecute({ lazy = false }: { lazy?: boolean } = {}) {
this.isMounted = true;

if (!lazy || this.runLazy) {
this.handleErrorOrCompleted();

Expand All @@ -103,6 +102,7 @@ export class QueryData<TData, TVariables> extends OperationData {
});
}

this.previousOptions = this.getOptions();
return this.unmount.bind(this);
}

Expand All @@ -114,25 +114,24 @@ export class QueryData<TData, TVariables> extends OperationData {

public getOptions() {
const options = super.getOptions();
const lazyOptions = this.lazyOptions || {};
const updatedOptions = {
...options,
variables: {

if (this.lazyOptions) {
options.variables = {
...options.variables,
...lazyOptions.variables
},
context: {
...this.lazyOptions.variables
};
options.context = {
...options.context,
...lazyOptions.context
}
};
...this.lazyOptions.context
};
}

// skip is not supported when using lazy query execution.
if (this.runLazy) {
delete updatedOptions.skip;
delete options.skip;
}

return updatedOptions;
return options;
}

private runLazyQuery = (options?: QueryLazyOptions<TVariables>) => {
Expand All @@ -149,29 +148,31 @@ export class QueryData<TData, TVariables> extends OperationData {
return result;
};

private getExecuteSsrResult(ssrDisabled: boolean) {
let result;

if (this.context && this.context.renderPromises) {
const ssrLoading = {
loading: true,
networkStatus: NetworkStatus.loading,
called: true,
data: undefined
};

// SSR is disabled, so just return the loading event and leave it in that state.
if (ssrDisabled) {
return ssrLoading;
}
private getExecuteSsrResult() {
const treeRenderingInitiated = this.context && this.context.renderPromises;
const ssrDisabled = this.getOptions().ssr === false;
const fetchDisabled = this.refreshClient().client.disableNetworkFetches;

const ssrLoading = {
loading: true,
networkStatus: NetworkStatus.loading,
called: true,
data: undefined
} as QueryResult<TData, TVariables>;

// If SSR has been explicitly disabled, and this function has been called
// on the server side, return the default loading state.
if (ssrDisabled && (treeRenderingInitiated || fetchDisabled)) {
return ssrLoading;
}

result = this.context.renderPromises.addQueryPromise(
this,
this.getExecuteResult
);
if (!result) {
result = ssrLoading as QueryResult<TData, TVariables>;
}
let result;
if (treeRenderingInitiated) {
result =
this.context.renderPromises!.addQueryPromise(
this,
this.getExecuteResult
) || ssrLoading;
}

return result;
Expand All @@ -196,7 +197,7 @@ export class QueryData<TData, TVariables> extends OperationData {
return {
...options,
displayName,
context: options.context || {},
context: options.context,
metadata: { reactComponent: { displayName } }
};
}
Expand All @@ -213,13 +214,14 @@ export class QueryData<TData, TVariables> extends OperationData {

if (!this.currentObservable.query) {
const observableQueryOptions = this.prepareObservableQueryOptions();

this.previousData.observableQueryOptions = {
...observableQueryOptions,
children: null
};
this.currentObservable.query = this.refreshClient().client.watchQuery(
observableQueryOptions
);
this.currentObservable.query = this.refreshClient().client.watchQuery({
...observableQueryOptions
});

if (this.context && this.context.renderPromises) {
this.context.renderPromises.registerSSRObservable(
Expand Down Expand Up @@ -266,7 +268,6 @@ export class QueryData<TData, TVariables> extends OperationData {
this.currentObservable.subscription = obsQuery.subscribe({
next: ({ loading, networkStatus, data }) => {
const previousResult = this.previousData.result;

if (previousResult) {
// Calls to `ObservableQuery.fetchMore` return a result before the
// `updateQuery` function fully finishes. This can lead to an
Expand All @@ -282,7 +283,6 @@ export class QueryData<TData, TVariables> extends OperationData {
) {
return;
}

// Make sure we're not attempting to re-render similar results
if (
previousResult.loading === loading &&
Expand Down
2 changes: 1 addition & 1 deletion packages/hooks/src/useSubscription.ts
Expand Up @@ -32,7 +32,7 @@ export function useSubscription<TData = any, TVariables = OperationVariables>(
}

const subscriptionData = getSubscriptionDataRef();
subscriptionData.setOptions(updatedOptions);
subscriptionData.setOptions(updatedOptions, true);
subscriptionData.context = context;

useEffect(() => subscriptionData.afterExecute());
Expand Down
4 changes: 2 additions & 2 deletions packages/ssr/package.json
@@ -1,7 +1,7 @@
{
"name": "@apollo/react-ssr",
"description": "React Apollo server-side rendering utilities",
"version": "3.1.1",
"version": "3.1.2-beta.0",
"author": "opensource@apollographql.com",
"keywords": [
"apollo",
Expand Down Expand Up @@ -29,7 +29,7 @@
"postbuild": "npx rollup -c ./config/rollup.config.js",
"watch": "npx tsc-watch --onSuccess \"npm run postbuild\" -p ./config",
"predeploy": "npm run build",
"deploy": "npm publish",
"deploy": "npm publish --tag beta",
"test": "npx jest --config ../../config/jest.config.js --testPathPattern packages/ssr",
"test:watch": "npx jest --config ../../config/jest.config.js --testPathPattern packages/ssr --watch",
"test:cjs": "npm run build && npx jest --config ../../config/jest.cjs.config.js --testPathPattern packages/ssr",
Expand Down

0 comments on commit 7b4fc9f

Please sign in to comment.