-
Notifications
You must be signed in to change notification settings - Fork 228
/
component.jsx
102 lines (98 loc) · 2.9 KB
/
component.jsx
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import React from 'react';
import styled from 'styled-components';
import {
AMP_SCRIPT,
AMP_NO_SCRIPT,
AMP_JS,
AMP_GEO_JS,
AMP_CONSENT_JS,
AMP_ANALYTICS_JS,
} from '@bbc/psammead-assets/amp-boilerplate';
import ResourceHints from '#app/components/ResourceHints';
import IfAboveIE9 from '#app/components/IfAboveIE9Comment';
/* eslint-disable react/prop-types */
const Document = ({
assetOrigins,
app,
data,
styleTags,
helmet,
isAmp,
scripts,
}) => {
const htmlAttrs = helmet.htmlAttributes.toComponent();
const meta = helmet.meta.toComponent();
const title = helmet.title.toComponent();
const links = helmet.link.toComponent();
const headScript = helmet.script.toComponent();
const serialisedData = JSON.stringify(data);
const scriptsAllowed = !isAmp;
const StyledDiv = styled.div`
min-height: 100vh;
display: flex;
flex-direction: column;
`;
// The JS to remove the no-js class will not run on AMP, therefore only add it to canonical
const noJsHtmlAttrs = !isAmp && { className: 'no-js' };
const scriptTags = (
<>
<IfAboveIE9>{scripts}</IfAboveIE9>
</>
);
return (
<html lang="en-GB" {...noJsHtmlAttrs} {...htmlAttrs}>
<head>
{meta}
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<ResourceHints assetOrigins={assetOrigins} />
{title}
{links}
{styleTags}
{headScript}
{isAmp && (
<>
<style amp-boilerplate="">{AMP_SCRIPT}</style>
<noscript>
<style amp-boilerplate="">{AMP_NO_SCRIPT}</style>
</noscript>
</>
)}
{isAmp && (
<>
{AMP_JS}
{AMP_GEO_JS}
{AMP_CONSENT_JS}
{AMP_ANALYTICS_JS}
</>
)}
</head>
<body>
{/* disabling the rule that bans the use of dangerouslySetInnerHTML until a more appropriate implementation can be implemented */}
{/* eslint-disable-next-line react/no-danger */}
<StyledDiv id="root" dangerouslySetInnerHTML={{ __html: app }} />
{scriptsAllowed && (
<script
/* eslint-disable-next-line react/no-danger */
dangerouslySetInnerHTML={{
__html: `window.SIMORGH_DATA=${serialisedData}`,
}}
/>
)}
{scriptsAllowed && scriptTags}
{scriptsAllowed && (
<script
type="text/javascript"
// Justification:
// - we need this to be a blocking script that runs before the page first renders
// - the content is static text so there is no real XSS risk
/* eslint-disable-next-line react/no-danger */
dangerouslySetInnerHTML={{
__html: `document.documentElement.classList.remove("no-js");`,
}}
/>
)}
</body>
</html>
);
};
export default Document;