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
docs: add more info about custom serialize/revive #24680
Conversation
Run & review this pull request in StackBlitz Codeflow. |
β Live Preview ready!
|
In the example below, we define a serializer for the [Luxon](https://moment.github.io/luxon/#/) DateTime class. Note that the "DateTime" string in the first argument is the type identifier and must be the same on both ends. | ||
```ts [plugins/dateTimeType.server.ts] | ||
export default definePayloadPlugin(() => { | ||
definePayloadReducer("DateTime", (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.
These can/should be in the same plugin, by the way. Nuxt will strip out server/client code when compiling it.
See example in https://nuxt.com/docs/api/composables/use-nuxt-app#payload.
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.
This is nice - thank you!
I think we should consider moving the location of the docs, however, as here it might suggest that this is related to fetching from Nitro endpoint, whereas payload serialisation is about any data in the paylod transferred from server -> client.
Alternatively, we could add example to existing page https://nuxt.com/docs/api/composables/use-nuxt-app#payload and link to it from here instead.
@danielroe Thank you! I agree that this is not the best location for the docs since its main subject is data fetching. I just looked at the docs part that you linked and saw that documentation about adding a custom type is already present. In my search for adding types I also encountered the blink example but it wasn't very clear to me. Maybe we could replace the current example at https://nuxt.com/docs/api/composables/use-nuxt-app#payload with the new one and link there from the data fetching page? I think we could link to the useNuxtApp page in this section of data fetching:
|
That sounds good to me. Though note that the point being made in that section on data fetching is about the type of the return, e.g. if a server endpoint returns a date, which is serialised via JSON.stringify() to a number, we try to ensure that the inferred return type of |
@danielroe I implemented the discussed changes. Let me know what you think! |
π Linked issue
#21832
β Type of change
π Description
I have added information about adding a type to the Nuxt (devalue) serializer. This functionality is mostly undocumented and very hard to find, so I felt it was useful to add it to the docs.
π Checklist