-
Notifications
You must be signed in to change notification settings - Fork 50k
[Flight] Serialize Date #26622
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
[Flight] Serialize Date #26622
Conversation
| key: string, | ||
| value: ReactClientValue, | ||
| ): ReactJSONValue { | ||
| if (typeof value === 'string' && value[value.length - 1] === 'Z') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's see if we can find a way not to duplicate this check. We have a string check further below, that can maybe unify with this. Since that is also going to check the first character both operations are free.
However, this technique doesn't seem very proof for other types of values. We don't have immediate plans to support URL but it's often requested - and it has a toJSON too.
Another thing we could do is always operate on the original value. Might make it more future proof. E.g. FormData doesn't have toJSON today but it might in the future. This might be bad for perf since it's a mega-morphic key look up for every value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How's this? ¯\_(ツ)_/¯ LMK if you prefer always reading the original value. (At that point it might be faster to reimplement JSON.stringify?)
This is kind of annoying because Date implements toJSON so JSON.stringify turns it into a string before calling our replacer function.
This is kind of annoying because Date implements toJSON so JSON.stringify turns it into a string before calling our replacer function.
This is kind of annoying because Date implements toJSON so JSON.stringify turns it into a string before calling our replacer function.
This is kind of annoying because Date implements toJSON so JSON.stringify turns it into a string before calling our replacer function. DiffTrain build for commit c6db19f.
Using a similar approach as facebook#26622 for Date, we now allow URL instances to be transported via Flight. To make room for serializing URLs, which are prefixed with `'$u'`, we now use `'$_'` as the serialization for `undefined`, instead of `'$undefined'`. This also reduces the (uncompressed) RSC payload size a bit when lots of `undefined` values are transported.
Using a similar approach as facebook#26622 for Date, we now allow URL instances to be transported via Flight. To make room for serializing URLs, which are prefixed with `'$u'`, we now use `'$_'` as the serialization for `undefined`, instead of `'$undefined'`. This also reduces the (uncompressed) RSC payload size a bit when lots of `undefined` values are transported.
Previously, `URL` objects were serialized as plain strings via `JSON.stringify`, so they lost their class identity on the receiving side. This change extends Flight to transport `URL` instances directly, using an approach similar to facebook#26622 for `Date`. To make room for URL serialization (prefixed with `'$u'`), we now represent undefined as `'$_'` instead of `'$undefined'`. This also slightly reduces the uncompressed RSC payload size when many `undefined` values are transported. If Flight were the only concern, we could have used `'$U'` for URLs and kept the existing encoding for `undefined`. However, this would conflict with how `Uint8ClampedArray` is serialized in the Flight Reply Client.
This is kind of annoying because Date implements toJSON so JSON.stringify turns it into a string before calling our replacer function.