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

Migrate next/router to use React.createContext #6030

Merged
merged 11 commits into from
Jan 11, 2019

Conversation

alexandernanberg
Copy link
Contributor

Fixes parts of #5716. I had some issues with the test suite but I'm fairly certain that I got it working correctly.

@alexandernanberg
Copy link
Contributor Author

alexandernanberg commented Jan 11, 2019

Got any tips on how to troubleshoot if the tests are failing? The test suite keeps spawning Chrome instances 😅

Edit: Looks like RouterContext exported from next/router is undefined 🤔

@timneutkens
Copy link
Member

TypeError: Cannot read property 'Consumer' of undefined
    at WithRouteWrapper (/home/circleci/repo/test/integration/basic/.next/server/static/development/pages/nav/with-hoc.js:551:129)
    at processChild (/home/circleci/repo/node_modules/react-dom/cjs/react-dom-server.node.development.js:2790:14)
    at resolve (/home/circleci/repo/node_modules/react-dom/cjs/react-dom-server.node.development.js:2714:5)
    at ReactDOMServerRenderer.render (/home/circleci/repo/node_modules/react-dom/cjs/react-dom-server.node.development.js:3098:22)
    at ReactDOMServerRenderer.read (/home/circleci/repo/node_modules/react-dom/cjs/react-dom-server.node.development.js:3057:29)
    at renderToString (/home/circleci/repo/node_modules/react-dom/cjs/react-dom-server.node.development.js:3524:27)
    at render (/home/circleci/repo/packages/next-server/dist/server/render.js:39:16)
    at renderPage (/home/circleci/repo/packages/next-server/dist/server/render.js:96:16)
    at Function.getInitialProps (/home/circleci/repo/test/integration/basic/.next/server/static/development/pages/_document.js:1262:25)
    at Object.loadGetInitialProps (/home/circleci/repo/packages/next-server/dist/lib/utils.js:44:35)
    at Object.renderToHTML (/home/circleci/repo/packages/next-server/dist/server/render.js:99:36)
    at <anonymous>

Looks like an issue with the Consumer

@timneutkens
Copy link
Member

You can run it locally:

./node_modules/next/dist/bin/next ./test/integration/basic

@alexandernanberg
Copy link
Contributor Author

Ah nice thanks! Okay so I think the problem is that the Provider with the context isn't rendered on the server. I guess the client/index isn't rendered during SSR? Where would be the best place for the provider to be located?

@timneutkens
Copy link
Member

Oh yeah I see, you didn't add it to packages/next-server/server/render.tsx 🕵️

@alexandernanberg
Copy link
Contributor Author

Looks like the Azure build error is unrelated to this PR, right? 🤔

@timneutkens
Copy link
Member

@alexandernanberg yep it fails inconsistently sometimes.

@timneutkens timneutkens merged commit 25fb3f9 into vercel:canary Jan 11, 2019
@timneutkens
Copy link
Member

Great PR @alexandernanberg 🙏

@timneutkens
Copy link
Member

Could you send me a DM on https://spectrum.chat/users/timneutkens

@alexandernanberg
Copy link
Contributor Author

Done ✔️

@jaydenseric
Copy link
Contributor

I'm trying to use RouterContext with next@8.0.0-canary.6, but the router is always undefined:

import { RouterContext } from 'next/router'

const Foo = () => (
  <RouterContext.Consumer>
    {router => {
      console.log(router) // undefined
      return null
    }}
  </RouterContext.Consumer>
)

Can you think why?

@@ -180,7 +180,9 @@ async function doRender ({ App, Component, props, err, emitter: emitterProp = em
// In development runtime errors are caught by react-error-overlay.
if (process.env.NODE_ENV === 'development') {
renderReactElement((
<App {...appProps} />
<RouterContext.Provider value={makePublicRouterInstance(router)}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so I think this is a big breaking change for next-graphql-react, and probably the Apollo examples too:

https://github.com/jaydenseric/next-graphql-react/blob/v1.0.1/src/index.mjs#L92

Copy link
Contributor

@jaydenseric jaydenseric Jan 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure this new approach of <RouterContext.Provider> wrapping <App> (as opposed to doing it inside <App> via a router prop, like before) is here to stay? If so, I can work it in to next-graphql-react and cut a new release.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I fully understand, the router props is still passed to the <App /> if that's what you are referring to 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way @alexandernanberg is exactly how I would have implemented it, outside of _app.js.

@alexandernanberg
Copy link
Contributor Author

alexandernanberg commented Jan 12, 2019

@jaydenseric That's interesting, I can't seem to reproduce it myself. Can you provide reproducible sample repo?

@alexandernanberg alexandernanberg deleted the use-new-context-in-router branch January 12, 2019 11:53
@jaydenseric
Copy link
Contributor

jaydenseric commented Jan 12, 2019

@alexandernanberg in the with-graphql-react example, 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

@alexandernanberg alexandernanberg restored the use-new-context-in-router branch January 14, 2019 12:31
alexandernanberg pushed a commit to alexandernanberg/next.js that referenced this pull request Jan 14, 2019
timneutkens pushed a commit that referenced this pull request Jan 14, 2019
* Revert #6030

* Fix _app childContextTypes
@lock lock bot locked as resolved and limited conversation to collaborators Jan 14, 2020
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 this pull request may close these issues.

3 participants