Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const render = async (reactElement, routes, applicationContext) => {
const scanResult = scan(original_$);
const { widgetConfigs } = route(routes, scanResult);
const context = { head, body, ...httpContext, ...applicationContext };
// NOTE: https://github.com/faceyspacey/react-universal-component#static-hoisting
// Without calling this function, `getInitialProps` statics will not be defined.
hoistComponentStatics(widgetConfigs);
initialProps = await resolveInitialProps(context, widgetConfigs);
const wrappedElement = React.createElement(
PrescanContext.Provider,
{ value: { initialProps, widgetConfigs } },
reactElement
);
// NOTE: https://github.com/faceyspacey/react-universal-component/issues/74
// We are doing this to make sure that the next `flushChunkNames()` call will only contain
// the universal components from `renderToString`, not from other asynchronous requests.
flushChunkNames();
const reactHTML = ReactDOMServer.renderToString(wrappedElement);
chunkNames = flushChunkNames();
const react_$ = cheerio.load(reactHTML);
// NOTE: Here we are populating the original HTML with the widget content.
react_$('[data-union-portal]').each((_, widget) => {
const $widget = react_$(widget);
return { widgetConfigs: [], chunkNames: [], initialProps: {} };
}
const { widgetConfigs } = route(routes, scanResult);
// NOTE: https://github.com/faceyspacey/react-universal-component#static-hoisting
// Without calling this function, `getInitialProps` statics will not be defined.
hoistComponentStatics(widgetConfigs);
const initialProps = await resolveInitialProps(
{ ...context, ...applicationContext },
widgetConfigs
);
const wrappedElement = React.createElement(
PrescanContext.Provider,
{ value: { initialProps, widgetConfigs } },
reactElement
);
// NOTE: https://github.com/faceyspacey/react-universal-component/issues/74
// We are doing this to make sure that the next `flushChunkNames()` call will only contain
// the universal components from `renderToString`, not from other asynchronous requests.
flushChunkNames();
const reactHTML = ReactDOMServer.renderToString(wrappedElement);
const chunkNames = flushChunkNames();
const react_$ = cheerio.load(reactHTML);
react_$('[data-union-portal]').each((_, widget) => {
const $widget = react_$(widget);
const id = $widget.data('union-portal');