getPersistedSnapshot should have null fields instead of undefined? #4716
-
getPersistedSnapshot() serves the purpose of serializing data to restore state (for example, across network requests - see blog reference here: https://stately.ai/blog/2023-10-02-persisting-state#persisting-and-restoring-state). However, the resulting object returned by the function sets output and error properties to undefined by default. This causes issues in cases where you might try to persist state from getServerSideProps to the client side in Next.js page router, for example, with errors like: Error: Error serializing .machine.snapshot.output returned from getServerSideProps in "/page/[slug]" I imagine there are other use cases where trying to serialize undefined can cause issues. I suggest the output and error properties are instead initialized to null for compatibility with serialization. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
This would be a breaking change at this point. This value can be post-processed to remove/convert what affects you. JSON is just one of the serialization formats that can be used (a very popular one though). I'm not saying that we won't change this but there are some extra things to consider here. |
Beta Was this translation helpful? Give feedback.
A simpler workaround:
More info on the issue here: vercel/next.js#11209
It's unfortunate that NextJS doesn't play nicely with
undefined
but it's something to consider when using any 3rd-party library with NextJS, not just XState. The safest thing to do is to process the data regardless.