Files
pedwfrontend/pages/_document.js
T
2021-11-23 20:20:43 +00:00

69 lines
2.3 KiB
JavaScript

import Document, { Html, Head, Main, NextScript } from "next/document";
import SkipLink from "../components/skiplink";
import { Tracking, TrackingNoScript } from "../components/tracking";
import basicAuthMiddleware from "nextjs-basic-auth-middleware";
class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx);
let useGATracking = false;
// remove this block when not need after softlaunch
// let hasBasicAuth =
// typeof process.env.BASIC_AUTH_CREDENTIALS != "undefined";
// console.log(hasBasicAuth);
// if (ctx.req && ctx.res) {
// hasBasicAuth &&
// (await basicAuthMiddleware(ctx.req, ctx.res, {
// users: process.env.BASIC_AUTH_CREDENTIALS,
// }));
// }
if (ctx.req && ctx.req.headers.cookie) {
typeof ctx.req.cookies.CookiePolicy != "undefined"
? JSON.parse(ctx.req.cookies.CookiePolicy).usage == "accepted"
? (useGATracking = true)
: (useGATracking = false)
: (useGATracking = true);
} else {
useGATracking = false;
}
return { ...initialProps, useGATracking };
}
render() {
const googleTagManagerID = process.env.GOOGLE_TAG_MANAGER || null;
const { useGATracking } = this.props;
return (
<Html lang="en">
<Head>
{useGATracking && (
<Tracking googleTagManagerID={googleTagManagerID} />
)}
</Head>
<body className="js-enabled">
{useGATracking && (
<TrackingNoScript
googleTagManagerID={googleTagManagerID}
/>
)}
{!process.browser && (
<script>
window.switchDomain = `{process.env.I18N_DOMAIN}`
</script>
)}
<SkipLink />
<Main />
<NextScript />
</body>
</Html>
);
}
}
export default MyDocument;