Skip to content

Commit

Permalink
Upgrade dependencies, only load GTM when cookie consent is given
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibowl committed Feb 26, 2022
1 parent 88adece commit cdc72bd
Show file tree
Hide file tree
Showing 5 changed files with 458 additions and 1,352 deletions.
4 changes: 2 additions & 2 deletions components/Footer.tsx
@@ -1,7 +1,7 @@
import FormattedLink from "./FormattedLink"

export default function Footer({ location }: { location: string }) {
return <footer className="flex flex-col items-center justify-center w-full border-t text-center">
export default function Footer({ location, marginBottom }: { location: string, marginBottom: number }) {
return <footer className="flex flex-col items-center justify-center w-full border-t text-center" style={({ marginBottom: `${marginBottom}px` })}>
<div className="flex items-center justify-center">
© All rights reserved by miHoYo. Other properties belong to their respective owners.
</div>
Expand Down
21 changes: 11 additions & 10 deletions package.json
Expand Up @@ -8,34 +8,35 @@
"lint": "next lint"
},
"dependencies": {
"chart.js": "^3.7.0",
"cookie": "^0.4.1",
"chart.js": "^3.7.1",
"cookie": "^0.4.2",
"git-raw-commits": "^2.0.11",
"jsonwebtoken": "^8.5.1",
"next": "12.0.7",
"next": "12.1.0",
"node-fetch": "2.6.6",
"react": "17.0.2",
"react-chartjs-2": "^4.0.1",
"react-cookie-consent": "^7.3.1",
"react-dom": "17.0.2",
"react-markdown": "^7.1.1",
"react-markdown": "^7.1.2",
"react-modal": "^3.14.4",
"react-toastify": "^8.1.0",
"react-toastify": "^8.2.0",
"sharp": "^0.29.3"
},
"devDependencies": {
"@types/cookie": "^0.4.1",
"@types/git-raw-commits": "^2.0.1",
"@types/jsonwebtoken": "^8.5.6",
"@types/jsonwebtoken": "^8.5.8",
"@types/node": "17.0.5",
"@types/node-fetch": "2",
"@types/node-fetch": "^2.6.1",
"@types/react": "17.0.38",
"@types/react-modal": "^3.13.1",
"autoprefixer": "^10.4.0",
"autoprefixer": "^10.4.2",
"babel-plugin-inline-react-svg": "^2.0.1",
"eslint": "8.5.0",
"eslint-config-next": "12.0.7",
"postcss": "^8.4.5",
"tailwindcss": "^3.0.7",
"postcss": "^8.4.7",
"tailwindcss": "^3.0.23",
"typescript": "4.5.4"
}
}
46 changes: 42 additions & 4 deletions pages/_app.tsx
@@ -1,37 +1,75 @@
import type { AppProps } from "next/app"
import Head from "next/head"
import { useEffect } from "react"
import { useEffect, useRef, useState } from "react"
import CookieConsent, { getCookieConsentValue } from "react-cookie-consent"
import "tailwindcss/tailwind.css"
import Footer from "../components/Footer"
import NavBar from "../components/NavBar"
import "../public/global.css"
import * as gtag from "../utils/gtag"
import Script from "next/script"

export default function MyApp({ Component, pageProps, router }: AppProps) {
const [consented, setConsented] = useState(null as boolean | undefined | null)

useEffect(() => {
const value = getCookieConsentValue()
if (value == "true")
setConsented(true)
else if (value == "true")
setConsented(false)
else
setConsented(undefined)
}, [])

useEffect(() => {
if (!consented)
return

const handleRouteChange = (url: string) => {
gtag.pageview(url)
}
router.events.on("routeChangeComplete", handleRouteChange)
return () => {
router.events.off("routeChangeComplete", handleRouteChange)
}
}, [router.events])
}, [router.events, consented])

return <div className="bg-slate-50 dark:bg-slate-700 min-h-screen flex flex-col items-center justify-between text-slate-900 dark:text-slate-100">
<Head>
<title>{router.pathname.substring(1).replace(/^\w/, w => w.toUpperCase())} | Hu Tao</title>
<link rel="icon" href="/favicon.ico" />
<meta httpEquiv="content-language" content="en-us"></meta>

{consented && <Script async
src={`https://www.googletagmanager.com/gtag/js?id=${gtag.GA_TRACKING_ID}`}
/>}
</Head>

<div className="w-full">
<div className="w-full flex-1">
<div className="absolute z-10">
<CookieConsent
buttonText="Accept all cookies"
declineButtonText="Refuse non-essential cookies"
enableDeclineButton
onAccept={() => {
setConsented(true)
}}
onDecline={() => {
setConsented(false)
}}
>
This website uses cookies to enhance the user experience.
<div style={{ fontSize: "12px" }}>Currently we only use these for analytic purposes, intended to figure out where to focus our efforts.</div>
</CookieConsent>
</div>

<NavBar location={router.asPath} />
<div className="p-4 flex flex-col w-full flex-1 px-1 lg:px-20 items-center justify-center">
<Component {...pageProps} location={router.asPath} />
</div>

<Footer location={router.asPath} marginBottom={consented || consented == null ? 3 : 80} />
</div>
<Footer location={router.asPath} />
</div>
}
11 changes: 3 additions & 8 deletions pages/_document.tsx
@@ -1,17 +1,12 @@
import Document, { Html, Head, Main, NextScript } from "next/document"

import Document, { Head, Html, Main, NextScript } from "next/document"
import { GA_TRACKING_ID } from "../utils/gtag"


export default class MyDocument extends Document {
render() {
return (
<Html>
<Head>
{/* Global Site Tag (gtag.js) - Google Analytics */}
<script
async
src={`https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}`}
/>
<Head lang="en">
<script
dangerouslySetInnerHTML={{
__html: `
Expand Down

0 comments on commit cdc72bd

Please sign in to comment.