vike
Version:
The Framework *You* Control - Next.js & Nuxt alternative for unprecedented flexibility and dependability.
45 lines (44 loc) • 2.1 kB
JavaScript
export { preparePageContextForUserConsumptionClientSide };
import { assert, objectAssign } from '../server-routing-runtime/utils.js';
import { getPageContextProxyForUser } from './getPageContextProxyForUser.js';
import { preparePageContextForUserConsumption } from '../../shared/preparePageContextForUserConsumption.js';
function preparePageContextForUserConsumptionClientSide(pageContext, isClientRouting) {
if (isClientRouting) {
const pageContextTyped = pageContext;
assert([true, false].includes(pageContextTyped.isHydration));
assert([true, false, null].includes(pageContextTyped.isBackwardNavigation));
}
else {
const pageContextTyped = pageContext;
assert(pageContextTyped.isHydration === true);
assert(pageContextTyped.isBackwardNavigation === null);
}
const Page = pageContext.config.Page ||
// TODO/next-major-release: remove
pageContext.exports.Page;
objectAssign(pageContext, { Page });
// TODO/next-major-release: remove
// - Requires https://github.com/vikejs/vike-vue/issues/198
// - Last time I tried to remove it (https://github.com/vikejs/vike/commit/705fd23598d9d69bf46a52c8550216cd7117ce71) the tests were failing as expected: only the Vue integrations that used shallowReactive() failed.
supportVueReactiviy(pageContext);
preparePageContextForUserConsumption(pageContext);
const pageContextProxy = getPageContextProxyForUser(pageContext);
return pageContextProxy;
}
// With Vue + Cient Routing, the `pageContext` is made reactive:
// ```js
// import { reactive } from 'vue'
// // See /examples/vue-full/renderer/createVueApp.ts
// const pageContextReactive = reactive(pageContext)
// ```
function supportVueReactiviy(pageContext) {
resolveGetters(pageContext);
}
// Remove propery descriptor getters because they break Vue's reactivity.
// E.g. resolve the `pageContext.urlPathname` getter.
function resolveGetters(pageContext) {
Object.entries(pageContext).forEach(([key, val]) => {
delete pageContext[key];
pageContext[key] = val;
});
}