-
Notifications
You must be signed in to change notification settings - Fork 22
/
_document.tsx
69 lines (60 loc) · 1.57 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
import newrelic from "newrelic";
import Document, {
DocumentContext,
DocumentInitialProps,
Html,
Head,
Main,
NextScript,
} from "next/document";
import { logger } from "../components/Logger";
type NewRelicProps = {
browserTimingHeader: string;
};
class MyDocument extends Document<NewRelicProps> {
static async getInitialProps(
ctx: DocumentContext
): Promise<DocumentInitialProps & NewRelicProps> {
const initialProps = await Document.getInitialProps(ctx);
/**
* For SSG pages the build is faster than the agent connect cycle
* In those cases, let's wait for the agent to connect before getting
* the browser agent script.
*/
if (!newrelic.agent.collector.isConnected()) {
await new Promise((resolve) => {
newrelic.agent.on("connected", resolve);
});
}
const browserTimingHeader = newrelic.getBrowserTimingHeader({
hasToRemoveScriptWrapper: true,
allowTransactionlessInjection: true,
});
logger.info("NextJs New Relic redirecting to a page", {
application: "NextJs NewRelic app logging",
test: "Testing logging with Winston",
pathname: ctx.pathname,
});
return {
...initialProps,
browserTimingHeader,
};
}
render() {
return (
<Html>
<Head>
<script
type="text/javascript"
dangerouslySetInnerHTML={{ __html: this.props.browserTimingHeader }}
/>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
export default MyDocument;