UNPKG

vike

Version:

The Framework *You* Control - Next.js & Nuxt alternative for unprecedented flexibility and dependability.

59 lines (58 loc) 2.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getGlobalContext = getGlobalContext; exports.getGlobalContextSync = getGlobalContextSync; exports.createGetGlobalContextClient = createGetGlobalContextClient; const createGlobalContextShared_js_1 = require("../../shared/createGlobalContextShared.js"); const getJsonSerializedInHtml_js_1 = require("./getJsonSerializedInHtml.js"); const utils_js_1 = require("./utils.js"); const globalObject = (0, utils_js_1.getGlobalObject)('createGetGlobalContextClient.ts', (() => { const { promise: globalContextPromise, resolve: globalContextPromiseResolve } = (0, utils_js_1.genPromise)(); return { globalContextPromise, globalContextPromiseResolve, }; })()); function createGetGlobalContextClient(virtualFileExports, isClientRouting, addGlobalContext) { (0, utils_js_1.assert)(globalObject.isClientRouting === undefined || globalObject.isClientRouting === isClientRouting); globalObject.isClientRouting = isClientRouting; // Eagerly call onCreateGlobalContext() hook getGlobalContext(); return getGlobalContext; async function getGlobalContext() { // Cache if (globalObject.globalContext && // Don't break HMR globalObject.globalContext._virtualFileExports !== virtualFileExports) { return globalObject.globalContext; } // Create const globalContext = await (0, createGlobalContextShared_js_1.createGlobalContextShared)(virtualFileExports, globalObject, async (globalContext) => { const globalContextAddendum = { /** * Whether the environment is client-side or server-side / pre-rendering. * * We recommend using `import.meta.env.SSR` instead, see https://vike.dev/globalContext */ isClientSide: true, }; (0, utils_js_1.objectAssign)(globalContextAddendum, (0, getJsonSerializedInHtml_js_1.getGlobalContextSerializedInHtml)()); (0, utils_js_1.objectAssign)(globalContextAddendum, await addGlobalContext?.(globalContext)); return globalContextAddendum; }); (0, utils_js_1.assert)(globalObject.globalContext); globalObject.globalContextPromiseResolve(globalObject.globalContext); // Return return globalContext; } } // The type is never used: it's the type of the server-side getGlobalContext() that is used. async function getGlobalContext() { const globalContext = await globalObject.globalContextPromise; return globalContext; } function getGlobalContextSync() { const { globalContext } = globalObject; (0, utils_js_1.assertUsage)(globalContext, createGlobalContextShared_js_1.getGlobalContextSyncErrMsg); return globalContext; }