Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as React from 'react';
- import { renderToString } from 'react-dom/server';
- import { HelmetData } from 'react-helmet';
- import { getBundles } from 'react-loadable/webpack';
- import { castError } from '../castError';
- export interface HtmlProps {
- helmet?: HelmetData;
- styleTags?: string;
- spriteContent?: string;
- content?: string;
- ssrError?: Error;
- apolloState?: object;
- reduxState?: object;
- bundles?: ReturnType<typeof getBundles>;
- }
- const GTMIds = {
- Analytics: 'GTM-TQX7VQ',
- Ads: 'GTM-TLLMLP',
- };
- const getGTMHeadScript = (GTMId: string) => `
- <!-- Google Tag Manager --> <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
- new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','${GTMId}');</script>
- <!-- End Google Tag Manager -->
- `;
- const getGTMBodyScript = (GTMId: string) => `
- <!-- Google Tag Manager (noscript) -->
- <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=${GTMId}" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
- <!-- End Google Tag Manager (noscript) -->
- `;
- export const Html: React.FC<HtmlProps> = ({
- helmet,
- styleTags,
- spriteContent,
- content,
- ssrError,
- apolloState,
- reduxState,
- bundles,
- }) => {
- return (
- <html {...(helmet ? helmet.htmlAttributes.toComponent() : {})}>
- <head
- dangerouslySetInnerHTML={{
- __html:
- (helmet
- ? helmet.base.toString() +
- helmet.link.toString() +
- helmet.meta.toString() +
- helmet.noscript.toString() +
- helmet.script.toString() +
- helmet.style.toString() +
- helmet.title.toString()
- : '') +
- (styleTags ? styleTags : '') +
- getGTMHeadScript(GTMIds.Analytics) +
- getGTMHeadScript(GTMIds.Ads),
- }}
- />
- <body
- {...(helmet ? helmet.bodyAttributes.toComponent() : {})}
- dangerouslySetInnerHTML={{
- __html: `${spriteContent ? spriteContent : ''}${renderToString(
- <React.Fragment>
- {ssrError ? <noscript>{castError(ssrError).userDisplayedMessage}</noscript> : null}
- <div id="root" dangerouslySetInnerHTML={{ __html: content ? content : '' }} />
- <script
- dangerouslySetInnerHTML={{
- __html: Object.entries({
- /** Config part */
- GRAPHQL_ENDPOINT: JSON.stringify(global.GRAPHQL_ENDPOINT),
- PUBLIC_PATH: JSON.stringify(global.PUBLIC_PATH),
- CANONICAL_ROBOTS_HOST: JSON.stringify(global.CANONICAL_ROBOTS_HOST),
- IS_OUTPUT_APP_INFO: JSON.stringify(global.IS_OUTPUT_APP_INFO),
- IS_SHOW_DEV_PAGES: JSON.stringify(global.IS_SHOW_DEV_PAGES),
- BACKGROUND_VIDEO_URL: JSON.stringify(global.BACKGROUND_VIDEO_URL),
- /** Dynamic server data part */
- APOLLO_STATE: JSON.stringify(apolloState),
- REDUX_STATE: JSON.stringify(reduxState),
- SSR_ERROR: JSON.stringify(ssrError, [
- ...Object.getOwnPropertyNames(ssrError || {}),
- 'name',
- ]),
- })
- .map(([key, value]) => `window.${key}=${value};`)
- .join(''),
- }}
- />
- {bundles
- ? bundles.map(bundle => (
- <script key={bundle.id} src={`${global.PUBLIC_PATH}${bundle.file}`} />
- ))
- : null}
- </React.Fragment>,
- )}${getGTMBodyScript(GTMIds.Analytics)}${getGTMBodyScript(GTMIds.Ads)}`,
- }}
- />
- </html>
- );
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement