2021-08-11 20:28:38 +00:00
|
|
|
import { useNuxt } from '#app'
|
2021-01-18 12:46:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows full control of the hydration cycle to set and receive data from the server.
|
2021-04-15 18:49:29 +00:00
|
|
|
*
|
2021-01-18 12:46:19 +00:00
|
|
|
* @param key a unique key to identify the data in the Nuxt payload
|
|
|
|
* @param get a function that returns the value to set the initial data
|
|
|
|
* @param set a function that will receive the data on the client-side
|
|
|
|
*/
|
|
|
|
export const useHydration = <T>(key: string, get: () => T, set: (value: T) => void) => {
|
|
|
|
const nuxt = useNuxt()
|
|
|
|
|
|
|
|
if (process.server) {
|
|
|
|
nuxt.hooks.hook('app:rendered', () => {
|
|
|
|
nuxt.payload[key] = get()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.client) {
|
|
|
|
nuxt.hooks.hook('app:created', () => {
|
|
|
|
set(nuxt.payload[key])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|