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

feat: add beforeSerialize #1475

Merged
merged 3 commits into from Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion content/en/docs/2.concepts/2.context-helpers.md
Expand Up @@ -38,7 +38,7 @@ function (context) { // Could be asyncData, nuxtServerInit, ...

// Only available on the Server-side
if (process.server) {
const { req, res, beforeNuxtRender } = context
const { req, res, beforeNuxtRender, beforeSerialize } = context
}

// Only available on the Client-side
Expand Down
29 changes: 29 additions & 0 deletions content/en/docs/6.internals-glossary/1.context.md
Expand Up @@ -146,6 +146,35 @@ Response from the Node.js server. If Nuxt is used as a middleware, the res objec

Use this method to update `__NUXT__` variable rendered on client-side, the `fn` (can be asynchronous) is called with `{ Components, nuxtState }`, see [example](https://github.com/nuxt/nuxt.js/blob/cf6b0df45f678c5ac35535d49710c606ab34787d/test/fixtures/basic/pages/special-state.vue).

### beforeSerialize

`beforeSerialize(fn)` (_Function_), available from Nuxt `2.16+`

Use this method to update `__NUXT__` variable rendered on client-side, the `fn` (must be synchronous) is called with `nuxtState` as argument. This method is called inside the `rendered` method of Vue SSR, allowing you to use it in components.

**Note**: this usage is advanced and mostly used for Nuxt modules.

```js
export default {
// Using asyncData
asyncData({ beforeSerialize }) {
if (process.server) {
beforeSerialize(nuxtState => {
nuxtState.hello = 'world'
})
}
},
// Using fetch
fetch() {
if (process.server) {
this.$root.context.beforeSerialize(nuxtState => {
nuxtState.hello = 'world'
})
}
}
}
```

## Client-side keys

These keys are available only on client-side.
Expand Down