Skip to content

Commit 59310f2

Browse files
chore(digital-data): moved declaration to _document.js (#66)
### Related Ticket(s) #47 ### Description This PR moves the digitalData declaration from `_app.js` to `_document.js` ### Changelog **Changed** - moved `digitalData` declaration to `_document.js`
1 parent 2c5bde4 commit 59310f2

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed

pages/_app.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import "../styles/global.scss";
22

33
import altlangs from "./data/altlang.json";
44
import App from "next/app";
5-
import DDO from "./data/DDO.json";
65
import { DotcomShell } from "@carbon/ibmdotcom-react";
76
import Head from "next/head";
87
import packageJson from "../package.json";
@@ -31,7 +30,6 @@ export default class CarbonForIBMDotcom extends App {
3130
const { Component, pageProps } = this.props;
3231
const reactVersion = packageJson.dependencies["@carbon/ibmdotcom-react"];
3332
const stylesVersion = packageJson.dependencies["@carbon/ibmdotcom-styles"];
34-
const digitalData = `digitalData=${JSON.stringify(DDO)};`;
3533

3634
const items = altlangs.map((alt, i) => (
3735
<link
@@ -58,23 +56,6 @@ export default class CarbonForIBMDotcom extends App {
5856
<meta name="geo.country" content="US" />
5957
<meta name="robots" content="index,follow" />
6058

61-
<script dangerouslySetInnerHTML={{ __html: digitalData }} />
62-
63-
<script
64-
dangerouslySetInnerHTML={{
65-
__html: `
66-
var params = new URLSearchParams(window.location.search);
67-
68-
if(params.has('lc') && params.has('cc')) {
69-
var lang = params.get('lc') + '-' + params.get('cc').toUpperCase();
70-
document.getElementsByTagName("html")[0].setAttribute("lang", lang);
71-
digitalData.page.pageInfo.language = lang;
72-
digitalData.page.pageInfo.ibm.country = params.get('cc').toUpperCase();
73-
}
74-
`,
75-
}}
76-
/>
77-
7859
{items}
7960

8061
{process.env.ENABLE_RTL === "true" && (

pages/_document.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Document, { Html, Head, Main, NextScript } from "next/document";
2+
import DDO from "./data/DDO.json";
3+
4+
/**
5+
* Class CarbonForIBMDotcomDocument
6+
*/
7+
class CarbonForIBMDotcomDocument extends Document {
8+
/**
9+
* Declares the digitalData object
10+
*
11+
* @returns {*} Page wrapper JSX
12+
*/
13+
render() {
14+
const digitalData = `digitalData=${JSON.stringify(DDO)};`;
15+
return (
16+
<Html>
17+
<Head>
18+
<script dangerouslySetInnerHTML={{ __html: digitalData }} />
19+
20+
<script
21+
dangerouslySetInnerHTML={{
22+
__html: `
23+
var params = new URLSearchParams(window.location.search);
24+
25+
if(params.has('lc') && params.has('cc')) {
26+
var lang = params.get('lc') + '-' + params.get('cc').toUpperCase();
27+
document.getElementsByTagName("html")[0].setAttribute("lang", lang);
28+
digitalData.page.pageInfo.language = lang;
29+
digitalData.page.pageInfo.ibm.country = params.get('cc').toUpperCase();
30+
}
31+
`,
32+
}}
33+
/>
34+
</Head>
35+
<body>
36+
<Main />
37+
<NextScript />
38+
</body>
39+
</Html>
40+
);
41+
}
42+
}
43+
44+
export default CarbonForIBMDotcomDocument;

0 commit comments

Comments
 (0)