-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import newrelic from 'newrelic'; | ||
import Script from 'next/script'; | ||
|
||
// Configuration according to Newrelic app router example | ||
// See https://github.com/newrelic/newrelic-node-nextjs?tab=readme-ov-file#example-projects | ||
export const newRelicInit = async () => { | ||
// @ts-ignore | ||
if (newrelic.agent.collector.isConnected() === false) { | ||
await new Promise((resolve) => { | ||
// @ts-ignore | ||
newrelic.agent.on('connected', resolve); | ||
}); | ||
} | ||
|
||
const browserTimingHeader = newrelic.getBrowserTimingHeader({ | ||
hasToRemoveScriptWrapper: true, | ||
// @ts-ignore | ||
allowTransactionlessInjection: true, | ||
}); | ||
|
||
return <NewRelicScript browserTimingHeader={browserTimingHeader} />; | ||
}; | ||
|
||
export type NewRelicScriptProps = { | ||
browserTimingHeader: string; | ||
}; | ||
|
||
const NewRelicScript = ({ browserTimingHeader }: NewRelicScriptProps) => { | ||
return ( | ||
// eslint-disable-next-line @next/next/no-before-interactive-script-outside-document | ||
<Script | ||
// We have to set an id for inline scripts. | ||
// See https://nextjs.org/docs/app/building-your-application/optimizing/scripts#inline-scripts | ||
id="nr-browser-agent" | ||
// By setting the strategy to "beforeInteractive" we guarantee that | ||
// the script will be added to the document's `head` element. | ||
strategy="beforeInteractive" | ||
// The body of the script element comes from the async evaluation | ||
// of `getInitialProps`. We use the special | ||
// `dangerouslySetInnerHTML` to provide that element body. Since | ||
// it requires an object with an `__html` property, we pass in an | ||
// object literal. | ||
dangerouslySetInnerHTML={{ __html: browserTimingHeader }} | ||
/> | ||
); | ||
}; |