# All files / src/server render.tsx

0% Statements 0/19
0% Branches 0/2
0% Functions 0/1
0% Lines 0/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64

     import { Request, Response } from 'express';
    import { renderToString } from 'react-dom/server';
    import { ChunkExtractor } from '@loadable/server';
    import { HttpStatusCode, isProduction } from '@cutting/util';
    import { StaticRouter, StaticRouterContext } from 'react-router';
    import { Routes } from '../routes';
    import path from 'path';
    import { Helmet } from 'react-helmet';
    import { GATagManager } from 'src/components/ga-tagmanager';

    const gaId = 'UA-146837410-1';

    export interface RendererOptions {
      req: Request;
      res: Response;
    }

    const statsFile = path.resolve(process.cwd(), 'dist/loadable-stats.json');

    export async function render({ req, res }: RendererOptions): Promise<void> {
      const extractor = new ChunkExtractor({
        entrypoints: ['client'],
        statsFile,
      });

      const context: StaticRouterContext = {};

      const helmet = Helmet.renderStatic();

      const app = (
        <html lang="en" {...helmet.htmlAttributes.toComponent()}>
          <head lang="en">
            <meta httpEquiv="X-UA-Compatible" content="IE=edge" />
            <meta httpEquiv="Content-Type" content="text/html; charset=UTF-8" />
            <meta charSet="utf-8" />
            <meta
              name="viewport"
              content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=5.0, minimum-scale=1.0"
            />
            {helmet.meta.toComponent()}
            {helmet.link.toComponent()}
            {helmet.title.toComponent()}
            {extractor.getStyleElements()}
            {isProduction && <GATagManager gaId={gaId} />}
          </head>
          <body {...helmet.bodyAttributes.toComponent()}>
            <div id="root">
              <StaticRouter location={req.url} context={context}>
                <Routes />
              </StaticRouter>
            </div>
          </body>
          {extractor.getScriptElements()}
        </html>
      );

      const appString = renderToString(extractor.collectChunks(app));

      res.status(HttpStatusCode.Ok).send(`
        <!doctype html>
        ${appString}
      `);
    }