Next.js: Shallow routing example throws TypeError: Cannot read property 'auth' of null

Created on 31 Mar 2020  路  4Comments  路  Source: vercel/next.js

Bug report

I'm not sure this is a bug, but it throws an error and it probably shouldn't.

Describe the bug

Shallow routing following the docs throws TypeError:

Unhandled Rejection (TypeError): Cannot read property 'auth' of null

To Reproduce

Clone repo and run:

https://github.com/tgdn/nextjs-shallow-typeerror/

Expected behavior

Error should not be thrown.

System information

  • OS: macOS
  • Version of Next.js: tried both 9.3.2 and 9.3.3
good first issue

All 4 comments

This might be a documentation issue. In the example in this repo the second parameter is the same as the first (the url).

So I was able to get shallow routing to work via router.push('/?count=1', '/?count=1', { shallow: true })

I believe this line in the Next codebase would prevent null from being a second valid parameter. typeof null === 'Object'so formatWithValidation(_as) runs.

I ended up using the same url as the first two arguments to push as you just said.

It probably is a documentation issue indeed.

@tgdn @jamesmosier I came across the same issue while following the docs. I think the problem comes from here where as is initiated to url (typeof null return always object), and because passing null is different from undefined for the behaviour of default parameter value(see here) , as parameter doesn't get url value when initiated to null.

@timneutkens I can see 2 possibilities here:

  • update the docs to use undefined instead of null: router.push('/?counter=10', undefined, { shallow: true })
  • add null check for as parameter in push function (maybe also in replace function), something like this in router.ts:
push(url: Url, as: Url = url, options = {}) {
    return this.change('pushState', url, as === null ? url : as, options)
  }

I would be happy to submit a PR with either of these solutions 馃槉

I came across the same problem in the docs and I found this issue, so I created a small PR to update the docs 馃槂

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DvirSh picture DvirSh  路  3Comments

renatorib picture renatorib  路  3Comments

flybayer picture flybayer  路  3Comments

knipferrc picture knipferrc  路  3Comments

sospedra picture sospedra  路  3Comments