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

SSR should check that instance belongs to Apollo before calling fetchData #1571

Closed
asmockler opened this issue Jan 23, 2018 · 1 comment
Closed

Comments

@asmockler
Copy link

Intended outcome:

fetchData should not be called on components that do not belong to react-apollo during server render.

Actual outcome:

If the component is not expecting fetchData to be called, SSR will fail. Right now, when walking the tree, react-apollo only checks if an instance exists and that it has a function called fetchData:

walkTree(rootElement, rootContext, (element, instance, context) => {
const skipRoot = !fetchRoot && element === rootElement;
if (instance && typeof instance.fetchData === 'function' && !skipRoot) {
const query = instance.fetchData();
if (query) {
queries.push({ query, element, context });
// Tell walkTree to not recurse inside this component; we will
// wait for the query to execute before attempting it.
return false;
}
}
});
return queries;
}

That is problematic if the instance has a function called fetchData that should not be called during server rendering.

How to reproduce the issue:

Add something along the lines of the following to a component being server rendered:

class MyComponent extends React.Component {
  fetchData() {
    throw new Error('fetchData should not be called');
  }

  render() { return null; }
}

Version

@benjamn
Copy link
Member

benjamn commented Nov 15, 2018

The implementation of getDataFromTree has changed substantially in [email protected] (the latest version), thanks to #2533. In particular, we no longer attempt to detect whether component instances have a fetchData method. Please try updating, and feel free to reopen this issue if the problem persists.

@benjamn benjamn closed this as completed Nov 15, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants