nuxt
Version:
21 lines (20 loc) • 718 B
JavaScript
import { useNuxtApp } from "../nuxt.js";
//#region src/app/composables/hydrate.ts
/**
* Allows full control of the hydration cycle to set and receive data from the server.
* @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
* @since 3.0.0
*/
const useHydration = (key, get, set) => {
const nuxtApp = useNuxtApp();
if (import.meta.server) nuxtApp.hooks.hook("app:rendered", () => {
nuxtApp.payload[key] = get();
});
if (import.meta.client) nuxtApp.hooks.hook("app:created", () => {
set(nuxtApp.payload[key]);
});
};
//#endregion
export { useHydration };