Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router context breaking change in v8.0.0-canary.6 #6042

Closed
jaydenseric opened this issue Jan 12, 2019 · 12 comments · Fixed by #7732
Closed

Router context breaking change in v8.0.0-canary.6 #6042

jaydenseric opened this issue Jan 12, 2019 · 12 comments · Fixed by #7732

Comments

@jaydenseric
Copy link
Contributor

Bug report

Describe the bug

The next/router migration to React.createContext in #6030 (Next.js v8.0.0-canary.6) is a breaking change for the GraphQL SSR related examples such as with-graphql-react and probably with-apollo.

To Reproduce

In the with-graphql-react example, update the next dependency to 8.0.0-canary.6 and replace the contents of pages/index.js with:

import { Query } from 'graphql-react'
import { RouterContext } from 'next/router'

export default () => (
  <RouterContext.Consumer>
    {router => {
      console.log(router)
      return null
    }}
  </RouterContext.Consumer>
)

With npm run dev, in terminal you will see first undefined (bad) for the GraphQL preload render and then a router instance when the final SSR happens:

screen shot 2019-01-13 at 9 50 03 am

I imagine this reproduction would work for the with-apollo examples too:

https://github.com/zeit/next.js/blob/v8.0.0-canary.6/examples/with-apollo/lib/with-apollo-client.js#L27

Expected behavior

Perhaps the API would be non-breaking from Next.js v7 if <RouterContext.Consumer> was used inside of <App> instead of immediately outside.

System information

  • Version of Next.js: v8.0.0-canary.6

Additional context

Is this breaking change intentional? If so, I can work it in to next-graphql-react and cut a new release for Next.js v8 compatibility.

The Apollo examples will also need updating.

@timneutkens
Copy link
Member

timneutkens commented Jan 13, 2019

Right so the issue is the non-standard rendering of the React tree in getInitialProps, I guess the best solution would be to wrap Component in the provider before Component is actually passed to _app, so that when it's rendered the value is provided automatically. This would require no user-side changes.

@todesstoss
Copy link

todesstoss commented Apr 25, 2019

@timneutkens Hello, i'm currently using Next.js v8.1.0, and as I understand after implementation of useRouter, Provider implemented with a same issue described here, legacy context witRouter works just fine, but useRouter breaks my getDataFromTree in with-apollo example, as useRouter return null in this method, is it smth that will be fixed or it is expected behaviour? Thanks

@timneutkens
Copy link
Member

See #7075 (comment)

@todesstoss
Copy link

Yes, sure, but i think it's smth you should be aware of

@adamsoffer
Copy link
Contributor

Hey @jaydenseric 👋 I'm encountering this as well. Did you by any chance figure out a workaround?

@adamsoffer
Copy link
Contributor

Can confirm Next router is not working when using Apollo as well.

@jaydenseric
Copy link
Contributor Author

jaydenseric commented Jun 2, 2019

@adamsoffer perhaps you are trying to use the useRouter hook, or using one of the broken Next.js versions?

The original issue was resolved when modern context for router was reverted before v8 landed. Modern context would be fine for us, if implemented in a different way so the <App> API doesn't break:

Perhaps the API would be non-breaking from Next.js v7 if <RouterContext.Consumer> was used inside of instead of immediately outside.

I'm looking forward to being able to use the useRouter hook once it's implemented properly.

@timneutkens
Copy link
Member

I'm looking forward to being able to use the useRouter hook once it's implemented properly.

Note that it is implemented properly and it's Apollo that causes this for you, not Next.js.

@adamsoffer
Copy link
Contributor

@timneutkens Would you mind explaining what's causing this bug so I can open up an issue in react-apollo?

@timneutkens
Copy link
Member

Well getDataFromTree does a full server-render (one for every single query component) so it needs the full tree as otherwise it doesn't have certain values in context (this was already the case).

So basically I guess we'll have to provide some kind of way to provide the tree, even though we really wouldn't want to as it can lead to misusage.

@adamsoffer
Copy link
Contributor

@timneutkens @jaydenseric I updated my next-apollo-example to use @apollo/react-hooks and useRouter 😀

For now, I get around this issue by wrapping router in a conditional: https://github.com/adamsoffer/next-apollo-example/blob/master/components/Header/index.js#L9

Not ideal, so if anyone has any ideas here let me know!

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
6 participants