How to use the react-union.PrescanContext.Provider function in react-union

To help you get started, we’ve selected a few react-union examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github lundegaard / react-union / packages / react-union-ssr-server / src / core.js View on Github external
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);
github lundegaard / react-union / packages / react-union-rendering-service / src / renderApplication.js View on Github external
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');

react-union

Assemble a single React application from distributed HTML segments.

MIT
Latest version published 4 years ago

Package Health Score

54 / 100
Full package analysis

Similar packages