-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update injecting browser agent example #62
Comments
Hi @bizob2828, in your comment, you said that we could use the Thanks |
Notes here for whoever takes this The way to deal with it currently is by creating a react component which returns a <script> tag with the agent injected, see the component below:
The odd thing here (it’s a Nextjs thing) is that you are interested in running newrelic.getBrowserTimingHeader method only on the node side of things thus the typeof window == "undefined" condition.
|
Hi @siuvdlec I just provided a better example. As you can see we are not using the Script component due to the caveats you mentioned above before and after interactions |
@bizob2828 thanks for clarifying. This new snippet brought me two doubts. |
Hi @siuvdlec, Could you elaborate on the use cases you're interested in, there are multiple ways of integrating the NR Browser Agent with NextJs depending on the needs.
|
Answering your question no 1 @siuvdlec, |
Hi @matewilk, we use SSR, but sometimes it is possible that some projects use SSG at least for the error pages. In my previous comment, when I used With this last approach (script in If we don't have to call |
Hi @siuvdlec I double checked that for you and you're absolutely right, you can inject the NR Browser agent into the const newrelic = require("newrelic");
export default class MyDocument extends Document {
static async getIntialProps() {
const browserTimingHeader = newrelic.getBrowserTimingHeader({
hasToRemoveScriptWrapper: true,
});
return {
browserTimingHeader,
};
}
render() {
return (
<Html>
<Head>
<script
type="text/javascript"
dangerouslySetInnerHTML={{ __html: this.props.browserTimingHeader }}
/>
</Head>
</Html>
);
}
} Using
So all in all, |
Hi @siuvdlec I've also looked into the const newrelic = require("newrelic");
import Document, {
DocumentContext,
DocumentInitialProps,
Html,
Head,
Main,
NextScript,
} from "next/document";
import Script from "next/script";
class MyDocument extends Document {
static async getInitialProps(
ctx: DocumentContext
): Promise<DocumentInitialProps> {
const initialProps = await Document.getInitialProps(ctx);
const browserTimingHeader = newrelic.getBrowserTimingHeader({
hasToRemoveScriptWrapper: true,
});
return {
...initialProps,
// @ts-ignore
browserTimingHeader,
};
}
render() {
return (
<Html>
<Head>{/* whatever you need here */}</Head>
<body>
<Main />
<NextScript />
<Script
// @ts-ignore
dangerouslySetInnerHTML={{ __html: this.props.browserTimingHeader }}
strategy="beforeInteractive"
></Script>
</body>
</Html>
);
}
}
export default MyDocument; So both, injecting directly into The |
Hi @matewilk Thank you your code! btw your code include ts-ignore. I hope my code will be as helpful to others as yours.🦄
|
Is your feature request related to a problem? Please describe.
In 8.12.0 we introduced a flag(hasToRemoveScriptWrapper) to api.getBrowserTimingHeader that does not wrap the browser agent in a script tag. We can update the example to show how to properly put in the head of the html and reuse in a custom component.
The text was updated successfully, but these errors were encountered: