Open adamsullovey opened 2 months ago
You didn't provide the right link to that codesandbox, it is linking to the template starter.
I think the issue here is that Next.js stores its own state into the history, since The state object can be anything that can be serialized.
:
{
"__NA": true,
"__PRIVATE_NEXTJS_INTERNALS_TREE": [
"",
{
"children": [
"__PAGE__",
{}
]
},
null,
null,
true
]
}
That's also valid from their side... 🤔
While:
if (data == null || typeof data === 'string') data = {}
And the link kind of checks, would most likely stop the issue, I wonder what'll happen to the rest of the app. We need a core maintainer up in here.
Link to the code that reproduces this issue
https://codesandbox.io/p/sandbox/github/vercel/next.js/tree/canary/examples/reproduction-template (or https://codesandbox.io/p/devbox/confident-pateu-go8s7s)
To Reproduce
window.history.pushState("", null, "/help")
in the browser consoleCurrent vs. Expected behavior
Current behaviour
An error is thrown:
Expected behaviour
The URL in the navigation bar should change to include the path
/help
Provide environment information
Which area(s) are affected? (Select all that apply)
Navigation
Which stage(s) are affected? (Select all that apply)
next dev (local), next start (local), Other (Deployed)
Additional context
I have received a not open-source 3rd party library to integrate with a Next.js site which updates URLs with calls like
window.history.pushState("", null, "/some-path")
. Passing""
as the first argument should be ok according MDN since""
is serializable. It does not trigger an error outside of Next.js. Unfortunately calling Next.js' version of pushState with a blank string as a first argument causes a crash and the 3rd party library stops working.The crash originates here: https://github.com/vercel/next.js/blob/e0ed599f43d3a0114e19f7ba6522270e5384e190/packages/next/src/client/components/app-router.tsx#L170
If
data
is""
,data
will not be reassigned to{}
. Laterdata.__NA = __NA
will try to create a new property on""
which is not allowed in strict mode.I'm happy to put up a PR that changes this if I can get some direction on what the right fix is. I don't know if changing to
if (!data) data = {}
orif (data == null || data == "") data = {}
or something else would preserve more intended behaviours.