UNPKG

vike

Version:

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

33 lines (32 loc) 1.43 kB
export { preparePageContextForUserConsumption }; import { assert, assertWarning, compareString } from './utils.js'; import { addIs404ToPageProps } from './addIs404ToPageProps.js'; function preparePageContextForUserConsumption(pageContext) { assert(pageContext.pageId); assert('config' in pageContext); assert('configEntries' in pageContext); addIs404ToPageProps(pageContext); // TODO/next-major-release: remove if (!('_pageId' in pageContext)) { Object.defineProperty(pageContext, '_pageId', { get() { assertWarning(false, 'pageContext._pageId has been renamed to pageContext.pageId', { showStackTrace: true, onlyOnce: true }); return pageContext.pageId; }, enumerable: false }); } // For a more readable `console.log(pageContext)` output sortPageContext(pageContext); } // Sort `pageContext` keys alphabetically, in order to make reading the `console.log(pageContext)` output easier function sortPageContext(pageContext) { let descriptors = Object.getOwnPropertyDescriptors(pageContext); for (const key of Object.keys(pageContext)) delete pageContext[key]; descriptors = Object.fromEntries(Object.entries(descriptors).sort(([key1], [key2]) => compareString(key1, key2))); Object.defineProperties(pageContext, descriptors); }