-
Notifications
You must be signed in to change notification settings - Fork 786
SSR error "cannot read property 'store' of undefined" #155
Comments
I did some digging, and I have might found the problem. I saw that GraphQL was instantiated twice, despite I use Looking at the function Now the second call of That component has no context set which means the result of Is it possible that getDataFromTree expects My fix is to change the Root.js: const Root = ({
history: _history,
routes,
routerState,
client,
store
}) => {
let component;
if (__SERVER__)
component = <RouterContext {...routerState} />;
else
component = <Router history={_history} routes={routes} render={scrollHandlingMiddleware} />;
return (component);
}; The function that renders server side: const Root = require("../client/containers/Root").default;
const client = createClient();
const store = createStore({ client });
const routes = getRoutes();
match({ routes, location: req.originalUrl }, (err, redirectLocation, routerState) => {
if (err) return next(err);
else if (redirectLocation)
return res.redirect(302, redirectLocation.pathname + redirectLocation.search);
else if (!routerState)
return res.status(404).send("Not Found");
const component = (
<ApolloProvider client={client} store={store}>
<Root routerState={routerState} routes={routes} />
</ApolloProvider>
), rootContainer);
return renderToStringWithData(component)
.then(markup => {
hydrateOnClient({
...htmlRenderProps,
markup
});
})
.catch(err => {
console.error("Error server side rendering", err);
return next(err);
});
}); This fixed my app. In my opinion, this behavior should be improved. It is common in React community to use a Root component that renders all the providers. And it should not be a worry which provider should be rendered as root. I don't know the rationale behind |
@rewop thank you so much for such excellent debugging! It shouldn't be required that the parent container is the |
@jbaxleyiii I could give it a try for sure. But I read that a fix could be already on its way from #157. If not I am happy to contribute. |
@jbaxleyiii I think I overlooked a bit the description, and I see now that #157 is a different problem. I will give a first go to this later on as I have some time. |
This should be fixed in the next version (set to release today) If it is not, please reopen! |
@jbaxleyiii Great! I am also working on a SSR demonstration with react-apollo based on a fork from https://github.com/apollostack/GitHunt. |
@rewop heads up - GitHunt has SSR now |
Perfect then. I missed the open PR yesterday when I forked. |
Also, another heads up, I just split up the API server and the React frontend into separate repos, since we want to share the server between all of the example frontends. |
Hi,
I am using
react-apollo
to perform server side rendering and I am receiving the errorThis error is only present server side, and not client side. When I disable SSR client side works perfectly.
I looked into the code and I found out that the error is thrown from the function
getDataFromTree
in theserver.ts
file.Basically when the function
getQueriesFromTree
tries to get the queries from the tree, when encounter theGraphQL
component it throws the error.I see that the
GraphQL
component is created by the decoratorgraphql
that handles the call to the serevr. It looks like that when the component is initialised, it cannot find the client neither from the context nor from the props (despite I have the root wrapped inApolloProvider
).Also I found another small bug. When this kind of situation arises, a warning should be shown, instead of a uncaught exception. The problem is that this line should be after the invariant check against the client prop in the line soon after.
I haven't managed to find out yet why the client is not in the context. The following is how my application is set up.
In my application I am using
react-router
,redux
andreact-apollo
. My root component looks like this:My client is created with this function, and passed to the
Root
component:The only container I am using with
react-apollo
at the moment looks like this:Any idea what the problem could be? Am I doing something wrong?
I am willing to contribute on this one if we find the problem.
The text was updated successfully, but these errors were encountered: