Server functions will falsely report an error in development when called with a MobX observable, but the value is (usually) correctly serialized.
As far as I can tell, this is because MobX observables are Proxies, and either add .toJSON() methods to wrapped values which return different values in JSON.stringify() than the "original value", or they add a special Symbol(mobx administration) to objects which are skipped over in serialization.
I created an issue in the React repo as well in case they are able to tweak the heuristic for these errors.
Intended outcome:
Ideally, observable values would be serializable without error in React server functions.
This came up in a real scenario when I tried invoking a server function in a Next.js codebase and passing data which happened to be a Proxy created by MobX. Because proxy values are meant to be indistinguishable, there was also no TypeScript warning that this error would occur.
Actual outcome:
In development, calling a server function with a MobX observable value will cause the function to report a confusing error, but not fail.
How to reproduce the issue:
https://codesandbox.io/p/devbox/2w4qqc
Server functions will falsely report an error in development when called with a MobX observable, but the value is (usually) correctly serialized.
As far as I can tell, this is because MobX observables are Proxies, and either add
.toJSON()methods to wrapped values which return different values inJSON.stringify()than the "original value", or they add a specialSymbol(mobx administration)to objects which are skipped over in serialization.I created an issue in the React repo as well in case they are able to tweak the heuristic for these errors.
Intended outcome:
Ideally, observable values would be serializable without error in React server functions.
This came up in a real scenario when I tried invoking a server function in a Next.js codebase and passing data which happened to be a
Proxycreated by MobX. Because proxy values are meant to be indistinguishable, there was also no TypeScript warning that this error would occur.Actual outcome:
In development, calling a server function with a MobX observable value will cause the function to report a confusing error, but not fail.
How to reproduce the issue:
https://codesandbox.io/p/devbox/2w4qqc