-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
unstable_serialize doesn't work with NextJS edge runtime #3030
Comments
Follow-upHaving done a bit more testing, the reason for using So my new suggested solution is to use Here are some examples: console.log({}.constructor == Object) // false
console.log([].constructor == Array) // false
console.log({} instanceof Object) // true
console.log([] instanceof Array) // true
console.log(new Set([1, 2]) instanceof Object) // true (we want this to be false so it's not serialized as an object)
console.log(Object.getPrototypeOf({}) == Object.prototype) // true
console.log(Object.getPrototypeOf([]) == Array.prototype) // true
console.log(Object.getPrototypeOf(new Set([1, 2])) == Object.prototype) // false (this won't be serialized as an object) |
Thanks for your awesome reproduction ❤️ The
the reason why the constructor of |
Co-authored-by: Toru Kobayashi <[email protected]>
Bug report
Description / Observed Behavior
When calling
unstable_serialize
from a server component that uses the NextJS edge runtime with an array or object key, the key is not serialized properly.As an example, the code below logs
1~
to the console:Expected Behavior
I would expect the behavior in edge runtime to match the NodeJS runtime, with
@"test",
logged to the console (example code below)Additional Context
Debugging
I've done some digging, and the problem seems to originate from the
stableHash
function in the file: https://github.com/vercel/swr/blob/v2.2.5/src/_internal/utils/hash.ts.if (constructor == Array) {
) and Line 48 (if (constructor == OBJECT) {
) don't return true for arrays/objects when using the edge runtime.Further testing reveals that although
key.constructor
doesn't equalArray
in the edge runtime,key instanceof Array
returns true (same applies to objects).A possible solution could be to change:
if (arg instanceof Array) {
if (arg instanceof OBJECT) {
This works in both the edge runtime and NodeJS runtime, but I'm unsure of the historical reasons for not using
instanceof
. Additionally, I'm not sure whyarg.constructor
doesn't equalArray
orOBJECT
in the edge runtime.Package Versions
[email protected]
├── @types/[email protected]
├── @types/[email protected]
├── @types/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]
Repro Steps / Code Example
I've built a test repository that demonstrates the issue. To reproduce:
npm i
), and then run the development server (npm run dev
).1~
(the output ofunstable_serialize
).true
(key instanceof Array
is true).false
(key.constructor == Array
is false).@"test",
(the output ofunstable_serialize
).true
(key instanceof Array
is true).true
(key.constructor == Array
is true).The text was updated successfully, but these errors were encountered: