Skip to content

shallowEquals throws when using custom server and not passing an object for query to app.render #2943

@migueloller

Description

@migueloller
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

Calling app.render(req, res, pathname, query) should not cause any issues.

Current Behavior

If app.render(req, res, pathname, query) is called and query is not an object, there will be a nasty error that will happen when calling Router.push (or any other router method that internally calls change).

Steps to Reproduce (for bugs)

  1. Set up a custom Next.js server
  2. Call app.render while passing an undefined value for query.
  3. Call Router.push with the same pathname and a query.

Context

I was trying to move from a dynamic server to using static exports and had to deal with this bug for about two hours. I've gone deep into Next.js's source code so I can help pinpoint the exact path the bug follows. Basically, it's something like app.render -> renderToHTML -> doRender -> new Router.

To solve this issue it's as simple as modifying the shallow comparison function to be a bit more safe. It should check that both inputs are objects and if not return false. For now, I'm just doing the following:

server.get('*', (req, res, n) => {
  const { pathname } = parse(req.url, true)
  const route = routes[pathname]
  if (route) return app.render(req, res, route.page, route.query || {})
  n()
})

This example in the wiki should be updated.

Your Environment

Tech Version
next 3.2.2
node 8.4.0
OS macOS 10.12.6
browser Chrome 60.0.3112.113

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueEasy to fix issues, good for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions