-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_document.tsx
80 lines (68 loc) · 2.77 KB
/
_document.tsx
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
import React from 'react';
import Document, { DocumentContext, Head, Html, Main, NextScript } from 'next/document';
import { DocumentInitialProps } from 'next/dist/pages/_document';
import { DecoratorComponents } from '@navikt/nav-dekoratoren-moduler/ssr';
import { Language } from 'translations';
import { DocumentParameter } from 'components/_common/metatags/DocumentParameterMetatags';
import { getDecoratorComponents } from 'srcCommon/decorator-utils-serverside';
type DocumentProps = {
language: Language;
Decorator: DecoratorComponents;
isLegacyContentType: boolean;
};
// The 'head'-field of the document initialProps contains data from <head> (meta-tags etc)
// We use this to pass certain data from our page content via meta tags from the
// DocumentParameterMetatags component
const getDocumentParameter = (initialProps: DocumentInitialProps, name: DocumentParameter) => {
return initialProps.head?.find((element) => element?.props?.name === name)?.props?.content;
};
class MyDocument extends Document<DocumentProps> {
static async getInitialProps(ctx: DocumentContext) {
const initialProps = await Document.getInitialProps(ctx);
const decoratorParams = getDocumentParameter(
initialProps,
DocumentParameter.DecoratorParams
);
const language = getDocumentParameter(initialProps, DocumentParameter.HtmlLang);
const decoratorDisabled = getDocumentParameter(
initialProps,
DocumentParameter.DecoratorDisabled
);
const isLegacyContentType =
getDocumentParameter(initialProps, DocumentParameter.LegacyContentType) === 'true';
const Decorator =
!decoratorDisabled &&
(await getDecoratorComponents(
decoratorParams ? JSON.parse(decoratorParams) : undefined
));
return {
...initialProps,
Decorator,
language,
isLegacyContentType,
};
}
render() {
const { Decorator, language, isLegacyContentType } = this.props;
return (
<Html lang={language || 'no'}>
<Head>
{Decorator && (
<>
<Decorator.Styles />
<Decorator.HeadAssets />
</>
)}
</Head>
<body className={isLegacyContentType ? 'legacyContentType' : undefined}>
{Decorator && <Decorator.Header />}
<Main />
{Decorator && <Decorator.Footer />}
{Decorator && <Decorator.Scripts />}
<NextScript />
</body>
</Html>
);
}
}
export default MyDocument;