From 0f9e31d9b804b83e9f6980268ccdc13bf4f6bba0 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 22 Sep 2021 20:46:58 +0100 Subject: [PATCH 1/3] feat: add beforeSerialize Co-authored-by: Sebastien Chopin --- .../en/docs/2.concepts/2.context-helpers.md | 2 +- .../en/docs/6.internals-glossary/1.context.md | 31 ++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/content/en/docs/2.concepts/2.context-helpers.md b/content/en/docs/2.concepts/2.context-helpers.md index 4b5938d0cc..af317af4bb 100644 --- a/content/en/docs/2.concepts/2.context-helpers.md +++ b/content/en/docs/2.concepts/2.context-helpers.md @@ -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 diff --git a/content/en/docs/6.internals-glossary/1.context.md b/content/en/docs/6.internals-glossary/1.context.md index 974948d001..17c4902203 100644 --- a/content/en/docs/6.internals-glossary/1.context.md +++ b/content/en/docs/6.internals-glossary/1.context.md @@ -29,7 +29,7 @@ function (context) { } = context // Server-side if (process.server) { - const { req, res, beforeNuxtRender } = context + const { req, res, beforeSerialize } = context } // Client-side if (process.client) { @@ -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. + +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. From fd16bf815fd75f61e248fefb5e7db50cdc08ccbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Tue, 21 Feb 2023 17:59:07 +0100 Subject: [PATCH 2/3] Update content/en/docs/6.internals-glossary/1.context.md --- content/en/docs/6.internals-glossary/1.context.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/en/docs/6.internals-glossary/1.context.md b/content/en/docs/6.internals-glossary/1.context.md index 17c4902203..6f2c7b3450 100644 --- a/content/en/docs/6.internals-glossary/1.context.md +++ b/content/en/docs/6.internals-glossary/1.context.md @@ -29,7 +29,7 @@ function (context) { } = context // Server-side if (process.server) { - const { req, res, beforeSerialize } = context + const { req, res, beforeNuxtRender } = context } // Client-side if (process.client) { From 58a027cce7eb05b7fdcf723d03b3575aee60a666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Tue, 21 Feb 2023 17:59:18 +0100 Subject: [PATCH 3/3] Update content/en/docs/6.internals-glossary/1.context.md Co-authored-by: Daniel Roe --- content/en/docs/6.internals-glossary/1.context.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/en/docs/6.internals-glossary/1.context.md b/content/en/docs/6.internals-glossary/1.context.md index 6f2c7b3450..e7893160d0 100644 --- a/content/en/docs/6.internals-glossary/1.context.md +++ b/content/en/docs/6.internals-glossary/1.context.md @@ -152,7 +152,7 @@ Use this method to update `__NUXT__` variable rendered on client-side, the `fn` 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. +**Note**: this usage is advanced and mostly used for Nuxt modules. ```js export default {