UNPKG

@sitecore-jss/sitecore-jss-rendering-host

Version:

This module is provided as a part of Sitecore JavaScript Rendering SDK (JSS). It contains the rendering host implementation.

50 lines (49 loc) 2.63 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDefaultAppInvocationInfoResolver = getDefaultAppInvocationInfoResolver; const import_fresh_1 = __importDefault(require("import-fresh")); const path_1 = __importDefault(require("path")); /** * Returns the default AppInvocationInfoResolver, which is responsible for resolving the function, within your app bundle, * that should be invoked for rendering your app. * * By default, the resolver assumes a folder structure of: * `./{baseAppPath}/{JSSAppName}/{serverBundleName}.js` * * `JSSAppName` is the `id` property of the JSON request body that is POSTed to the rendering host by Sitecore. * * `serverBundleName` is the name of the JavaScript file (typically a bundle) that contains the function for rendering your app. * @param {string} [baseAppPath] The base path to your JSS app(s), defaults to `./dist` * @returns {AppInvocationInfoResolver} resolver */ function getDefaultAppInvocationInfoResolver({ appPathResolver = (requestJson) => { // eslint-disable-next-line @typescript-eslint/no-use-before-define return path_1.default.resolve(baseAppPath, requestJson.id, serverBundleName); }, baseAppPath = './dist', serverBundleName = 'server.bundle', }) { const resolver = (requestJson) => { // default resolution assumes folder structure of: // ./dist/{JSSAppName}/{ServerBundleName}.js const modulePath = appPathResolver(requestJson); const resolvedModule = (0, import_fresh_1.default)(modulePath); const resolvedRenderFunctionName = requestJson.functionName || 'renderView'; const renderFunction = resolvedModule[resolvedRenderFunctionName]; if (!renderFunction) { throw new Error(`The module "${modulePath}" has no export named "${resolvedRenderFunctionName}". Ensure that your server bundle is transpiled to CommonJS (or equivalent) format that can be resolved by a Node.js 'require' statement. And ensure that your server bundle exports a function named "${resolvedRenderFunctionName}".`); } const renderFunctionArgs = requestJson.args; return { renderFunction: (...args) => { console.log(`[SSR] rendering app at ${modulePath} via render function named ${resolvedRenderFunctionName}`); return renderFunction(...args); }, renderFunctionArgs, }; }; return resolver; }