adamsoffer / next-apollo

React higher-order component for integrating Apollo Client with Next.js
MIT License
481 stars 64 forks source link

Cannot read property 'pathname' of undefined #31

Closed AndrewRayCode closed 5 years ago

AndrewRayCode commented 5 years ago

I can't use this library because of the dependency issues, but when I copied most of the source code into my program, I started getting

Cannot read property 'pathname' of undefined

I believe it's because you need to pass the router object as a prop, and I'm not sure if you need it in context. I'm referring to your line: https://github.com/adamsoffer/next-apollo/blob/master/src/withData.js#L43

VS in https://github.com/zeit/next.js/blob/canary/examples/with-apollo/lib/with-apollo-client.js#L27 they pass it as a prop

adamsoffer commented 5 years ago

Interesting. If I recall I needed router in context to support prefetching for some reason. I'll make sure to pass it as a prop as well in a hotfix.

adamsoffer commented 5 years ago

So passing router as a prop doesn't fix the issue. Hmm. Not sure what's goin on here...

amitschandillia commented 5 years ago

Hey Adam, were you able to resolve this one? I've been struggling with the same issue for a while now and have even posted to StackOverflow (here: https://stackoverflow.com/questions/57094132/mysterious-typeerror-while-running-next-apollo-react-apollo) with zero answers so far. My code is pretty short:

import { HttpLink } from 'apollo-link-http';
import { withData } from 'next-apollo';
import { InMemoryCache } from 'apollo-cache-inmemory';

// Set up cache.
const cache = new InMemoryCache();

// Configure Apollo.
const config = {
  link: new HttpLink({
    uri: 'https://dev.schandillia.com/graphql', // Server URL
  }),
  cache,
};
export default withData(config);

So passing router as a prop doesn't fix the issue. Hmm. Not sure what's goin on here...

amitschandillia commented 5 years ago

I can't use this library because of the dependency issues, but when I copied most of the source code into my program, I started getting

Cannot read property 'pathname' of undefined

I believe it's because you need to pass the router object as a prop, and I'm not sure if you need it in context. I'm referring to your line: https://github.com/adamsoffer/next-apollo/blob/master/src/withData.js#L43

VS in https://github.com/zeit/next.js/blob/canary/examples/with-apollo/lib/with-apollo-client.js#L27 they pass it as a prop

Did you find a solution yet, Andrew?

adamsoffer commented 5 years ago

Hey @amitschandillia - turns out this is actually an issue with next. There’s an open issue for it here: https://github.com/zeit/next.js/issues/6042

amitschandillia commented 5 years ago

Ahh...at least that's some pointer in the right direction. Thanks! :)

yzhang-indeed commented 5 years ago

Hey @adamsoffer, I'm wondering if you are still looking into this issue? Currently I'm unable to SSR pages with dynamic url because the render method is called twice. The first time the router object is undefined.

adamsoffer commented 5 years ago

Hey guys, this was resolved with v3.1.2.

amitschandillia commented 5 years ago

Hey Adam, sorry for resurrecting a closed issue but I just updated to v3.1.2 and got hit by this error:

Module not found: Can't resolve '@apollo/react-ssr' in '/home/ubuntu/proost/web/node_modules/next-apollo/dist'

Am I missing something?

adamsoffer commented 5 years ago

Yeah I forgot to add @apollo/react-ssr as a peer dep to the package. My bad.

Install v3.1.4 and then install @apollo/react-ssr as a peer dep. I just updated the README to reflect this.

amitschandillia commented 5 years ago

Ugh...now there's an invariant violation:

Error while running getDataFromTree { Invariant Violation: Minified React error #130; visit https://reactjs.org/docs/error-decoder.html?invariant=130&args[]=undefined&args[]= for the full message or use the non-minified dev environment for full errors and additional helpful warnings.

adamsoffer commented 5 years ago

@amitschandillia - check out the next-apollo-example repo. It's using next-apollo v3.1.4 and working on my end. Can you try cross-referencing your dependencies with that project and see if you're missing anything?