All files / src/server/Document index.jsx

100% Statements 7/7
100% Branches 0/0
100% Functions 1/1
100% Lines 7/7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41              1x               1x   1x           1x 1x 1x                     1x        
import React from 'react';
import { renderToString, renderToStaticMarkup } from 'react-dom/server';
import { ServerApp } from '../../app';
import { getStyleTag } from '../styles';
import getAssetsArray from '../assets';
import DocumentComponent from './component';
 
const renderDocument = async (
  url,
  data,
  routes,
  ResourceHints,
  ServerStyleSheet, // pass in the ServerStyleSheet as it needs to remain a singleton - revist in https://github.com/bbc/SPArtacus/issues/41
  Helmet,
) => {
  const sheet = new ServerStyleSheet();
 
  const app = renderToString(
    sheet.collectStyles(
      <ServerApp location={url} routes={routes} data={data} context={{}} />,
    ),
  );
 
  const headHelmet = Helmet.renderStatic();
  const assets = getAssetsArray();
  const doc = renderToStaticMarkup(
    <DocumentComponent
      assets={assets}
      app={app}
      data={data}
      styleTags={getStyleTag(sheet, data.isAmp)}
      helmet={headHelmet}
      ResourceHints={ResourceHints}
    />,
  );
 
  return `<!doctype html>${doc}`;
};
 
export default renderDocument;