-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Enforce that endpoint responses can be serialized #4944
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
Comments
Hmm. Since no serialization is happening, I wonder if we should just enforce (albeit only at a type level) that the Is it important that you're able to rely on |
in my use case, i'm using special objects that have a custom Since regular endpoints will automatically handles |
opened a potential implementation PR (mainly wanted to learn how to contribute something small), but of course it should be discussed it should be implemented as a feature, or fixed by removing support entirely. |
I'm slightly inclined to require a POJO as well. It'd be an unfortunate asymmetric quirk, but maybe if we provided your |
I think that either the function should be automatically used or the user should (dev mode only) get warnings about having toJSON functions. I personally like the automatic side but I understand that it's a small use case that may not be worth adding and slowing down everyone's code. I also want to remind that one important case that people may run into accidentally is returning |
If anything, the The difference with standalone endpoints is that you always explicitly treat them as |
that makes sense, you've convinced me it should be the dev warning due to the confusion of automatic type conversion, and how there's warning for returning other invalid structures like a I can go an attempt a new PR but I'd like someone else to agree on it before I go put the work into it. Also, I got two question regarding the implementation of a dev-warning:
|
FWIW, I recently bumped into this when using <script lang="ts">
import type { MyType } from '$models/my_type.type';
export let obj: MyType;
obj.date = new Date(obj.date); // typed-ness lost; reconstruct `Date`
</script> |
Closed by #5987 |
Describe the bug
When using page endpoints, the JSON output of the request isn't serialized before passing it to the component/load(), causing the SSR version of these pages to be different than the client rendered version. To extend this, you can pass any value through the endpoint, including functions and classes (these ones wont pass type checking), and it will be passed as props to the ssr component.
Reproduction
Repo: https://github.com./davecaruso/bug-sveltekit-tojson-endpoints
Logs
System Info
Severity
annoyance
Additional Information
I believe the part of the code that has to be changed is here
https://github.com./sveltejs/kit/blob/master/packages/kit/src/runtime/server/page/load_node.js#L520
as
body
is just passed through. it should either be passed throughJSON.stringify
or some helper function to flatten all objects with.toJSON
The text was updated successfully, but these errors were encountered: