Files
koodiklinikka.fi/pages/_document.tsx
Olavi Haapala 0eba06f861 Inline the CSS for improved first paint performance (#81)
* Upgrade Next.js from 9.1.2 to 9.5.3

* Inline the CSS for improved first paint performance

* Change ENV to NODE_ENV

Possibly the CSS was not minified due to this
2020-09-30 15:12:47 +03:00

69 lines
1.6 KiB
TypeScript

import React from "react";
import Document, { Html, Head, Main, NextScript } from "next/document";
import { Footer } from "../components/Footer";
import Fader from "../components/Fader";
import ReactGA from "react-ga";
import fs from "fs";
import path from "path";
class CustomNextHead extends Head {
// TODO: This might not be needed if Next.js implements built-in support
// https://github.com/zeit/next-plugins/issues/364
getCssLinks({ allFiles }) {
return allFiles
.filter(file => file.endsWith(".css"))
.map(file => (
<style
key={file}
nonce={this.props.nonce}
dangerouslySetInnerHTML={{
__html: fs.readFileSync(path.join(".next", file), "utf-8"),
}}
/>
));
}
}
function trackPageView() {
if (location.hostname === "localhost" || location.hostname === "127.0.0.1") {
return;
}
if (!window.GA_INITIALIZED) {
ReactGA.initialize("UA-58806132-1");
window.GA_INITIALIZED = true;
}
ReactGA.set({ page: window.location.pathname });
ReactGA.pageview(window.location.pathname);
}
class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx);
return { ...initialProps };
}
componentDidMount() {
trackPageView();
}
render() {
return (
<Html lang="fi">
<CustomNextHead />
<body>
<div className="site">
<div className="container">
<Main />
</div>
<Footer />
</div>
<Fader />
<NextScript />
</body>
</Html>
);
}
}
export default MyDocument;