-
Notifications
You must be signed in to change notification settings - Fork 49.4k
Description
The React Server Components payload is a custom protocol that extends what is serializable beyond just JSON. Beyond just JSON we also support all React primitives (React.lazy, ReactNode) and global named symbols (Symbol.for). We also already have plan to expand this support with these as well:
- Promises
- Typed Arrays / DataView
- BigInt
- undefined, Infinity, NaN, -0
We don't have plans to make this algorithm pluggable from the outside because we're concerned about the complexity this puts on the ecosystem and that components won't be reusable in different contexts where they're not configured or configurations are conflicting.
However, it might make sense to expand support to the types supported by the Structured clone algorithm which is already standardized and specified.
- Cyclic references: We already support references in the protocol. This is mostly just an implementation detail for the perf cost whether something should be inlined as JSON or defined as a separate row.
- ArrayBuffer: For Typed Arrays we might stick to using the underlying buffer coming from the stream instead of cloning the data. All values are considered immutable anyway. For ArrayBuffers we can't use that trick though so it would require a new clone of the data which might be a bit of a foot gun when switching between buffers and typed arrays.
- Error objects: We already support thrown errors and we could support more errors in the encoding. However, we intentionally don't pass them through with all information. We cover them up with digests since the error message and stack can sometimes include sensitive information that only the server should have access to. We would likely have to do the same here.
- Boolean/String objects: We don't currently support the object wrappers around primitives e.g.
new String()
. You're not really supposed to use these in modern JS so it's kind of annoying to have to add extra code to handle this case. - RegExp: These are pretty straightforward but can possibly have security implications.
- Date, Map, Set: These are fairly straightforward to serialize so it's mostly a matter of allowing these as special cases. Why are these special? Because Structured Clone says so.
- (Temporal: It seems appropriate that this would be added to structured clone but we need to confirm.)
We probably won't support Web specific APIs that don't necessarily have an equivalent on the Server or isn't directly transferrable such as if it has handles to local hardware or file system resources. The only one that might be easy to support:
- Blob: This would be a wrapper around a ReadableStream with a mime type.
- (File: This is just a Blob with a modified time and file name. I think we'll likely want to only support Blobs and not Files, meaning that File object would serialize as Blob, so the receiving type has to be Blob. Because file names and modified times could have security implications and it's too easy to accidentally leak this data.)