feat: allow serialization/deserialization of custom data types#13125
feat: allow serialization/deserialization of custom data types#13125
Conversation
|
|
preview: https://svelte-dev-git-preview-kit-13125-svelte.vercel.app/ this is an automated message |
|
I'd love an api where They have a strict relation to one another and changing one without the other can lead to bugs. It would also simplify implementing tests that ensure serialize(deserialize(x)) equals x and deserialize(serialize(y)) equals y Users can possibly do that in userland by importing them from such a file, but just lik param matchers i think we could provide that from a known location. |
So I tried to do something like that, but I gave up. I couldn't figure out how how we could safely take the function on the server and use it on the client, given the function references imports, and generate client code with the same logic without leaking server stuff. This is my first Kit PR, so maybe I just need someone to help me understand how that sort of thing can be done. |
|
its a pair of pure functions tied together by a name, so shouldn't it work in a similar way as //src/serializers/foo.js
export const is(foo: any): boolean;
export const serialize(foo: Foo): string;
export const deserialize(str: string): Foo;due to treeshaking only one of serialize and deserialize should make it to the output of client or server. |
The body of the functions will require importing the class though. So when it runs on the client, the import will be missing? |
It's not done like that because currently we're not using the serialize function on the client / the deserialize function on the server. |
dummdidumm
left a comment
There was a problem hiding this comment.
Looks good so far! I think this doesn't handle the client side navigation case yet though, i.e. when the data loading happens via __data.json. E.g. if you start on /foo and that contains a link to /serialization-basic, so it (de)serialize the data correctly?
| else fulfil(data); | ||
| const try_to_resolve = () => { | ||
| if (!deferred.has(id)) { | ||
| setTimeout(try_to_resolve, 0); |
There was a problem hiding this comment.
was is necessary after all due to the dynamic imports? If so we should add a comment here stating why this is necessary.
There was a problem hiding this comment.
this feels suuuuuper hacky, surely we can find another approach?
|
@dummdidumm Thanks for the feedback. Fixed! |
Co-authored-by: Rich Harris <richard.a.harris@gmail.com>
|
Opened an alternative PR that puts the encoding/decoding logic in a single hook: #13149 |
|
Closing in favor of #13149 |
Enables serialization and deserialization of custom data types in the form of hooks.
Closes #9401.