Skip to content
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

Merged
merged 5 commits into from Dec 12, 2023
Merged

docs: add more info about custom serialize/revive #24680

merged 5 commits into from Dec 12, 2023

Conversation

Jamie4224
Copy link
Contributor

πŸ”— Linked issue

#21832

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to 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

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

Copy link

stackblitz bot commented Dec 11, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link

nuxt-studio bot commented Dec 11, 2023

βœ… Live Preview ready!

Name Edit Preview Latest Commit
Nuxt Docs Edit on Studio β†—οΈŽ View Live Preview 147c668

@github-actions github-actions bot added the 3.x label Dec 11, 2023
@Jamie4224 Jamie4224 changed the title docs: Added information about adding a type to the Nuxt (de)serializer docs: added information about adding a type to the Nuxt (de)serializer Dec 11, 2023
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) => {
Copy link
Member

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.

Copy link
Member

@danielroe danielroe left a 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.

@Jamie4224
Copy link
Contributor Author

@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:

However, since serialization is limited to only JavaScript primitive types, Nuxt does its best to convert the return type of $fetch and useFetch to match the actual value.

@danielroe
Copy link
Member

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 $fetch('/that/endpoint') correctly shows that date is a number. (It's not about 'rich' payloads.)

@Jamie4224
Copy link
Contributor Author

@danielroe I implemented the discussed changes. Let me know what you think!

@danielroe danielroe changed the title docs: added information about adding a type to the Nuxt (de)serializer docs: add more info about custom serialize/revive Dec 12, 2023
@danielroe danielroe merged commit 3991a10 into nuxt:main Dec 12, 2023
4 checks passed
@github-actions github-actions bot mentioned this pull request Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants