Skip to content

Commit

Permalink
feat: add beforeSerialize
Browse files Browse the repository at this point in the history
Related to nuxt/nuxt#9332
  • Loading branch information
Atinux committed Jun 4, 2021
1 parent 93af215 commit bbe3c05
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion content/en/guides/concepts/context-helpers.md
Expand Up @@ -73,7 +73,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
31 changes: 30 additions & 1 deletion content/en/guides/internals-glossary/context.md
Expand Up @@ -28,7 +28,7 @@ function (context) {
} = context
// Server-side
if (process.server) {
const { req, res, beforeNuxtRender } = context
const { req, res, beforeSerialize } = context
}
// Client-side
if (process.client) {
Expand Down Expand Up @@ -147,6 +147,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_)

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.

Not 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

0 comments on commit bbe3c05

Please sign in to comment.