-
Notifications
You must be signed in to change notification settings - Fork 11
/
_document.js
59 lines (53 loc) · 1.41 KB
/
_document.js
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
import Document, { Head, Html, Main, NextScript } from 'next/document';
import Script from 'next/script';
import { ServerStyleSheet } from 'styled-components';
import PropTypes from 'prop-types';
const MyDocument = ({ localeDataScript, locale, styles }) => {
// Polyfill Intl API for older browsers
const polyfill = `https://cdn.polyfill.io/v3/polyfill.min.js?features=Intl.~locale.${locale}`;
return (
<Html>
<Head />
<body>
<Main />
<Script src={polyfill} />
<Script
id="locale-data"
dangerouslySetInnerHTML={{
__html: localeDataScript
}}
/>
<NextScript />
{styles}
</body>
</Html>
);
};
MyDocument.propTypes = {
localeDataScript: PropTypes.string.isRequired,
locale: PropTypes.string.isRequired,
styles: PropTypes.node.isRequired
};
MyDocument.getInitialProps = async (ctx) => {
const sheet = new ServerStyleSheet();
const originalRenderPage = ctx.renderPage;
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) => sheet.collectStyles(<App {...props} />)
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
)
};
} finally {
sheet.seal();
}
};
export default MyDocument;