Skip to content
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

App router migration new relic configuration #1086

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
440bcb0
Add basic Root Layout
boodland Aug 19, 2024
78f94ae
Add metadata
boodland Aug 19, 2024
e4bffac
Add reference included by next
boodland Aug 19, 2024
844f412
Add comment to explain the default meta tags
boodland Aug 19, 2024
d857356
Add public testing page to check the migration
boodland Aug 19, 2024
c7148d3
Add basic Root Layout
boodland Aug 19, 2024
7f0deb9
Add metadata
boodland Aug 19, 2024
b6c1f22
Add reference included by next
boodland Aug 19, 2024
4f2d7c4
Add comment to explain the default meta tags
boodland Aug 19, 2024
a10db01
Add public testing page to check the migration
boodland Aug 19, 2024
7c20c78
Add new relic nextjs configuration
boodland Aug 19, 2024
2c6a7bc
Create new relic configuration file
boodland Aug 19, 2024
7e53126
Use new relic configuration
boodland Aug 19, 2024
fbc9686
Merge branch 'next-js-migration' into app-router-migration-root-layout
boodland Aug 30, 2024
b593d6f
Merge branch 'app-router-migration-root-layout' into app-router-migra…
boodland Aug 30, 2024
86241af
Remove non required file
boodland Aug 30, 2024
ceaa88e
Merge branch 'app-router-migration-root-layout' into app-router-migra…
boodland Aug 30, 2024
7753da2
Add basic Root Layout
boodland Aug 19, 2024
4955f46
Add metadata
boodland Aug 19, 2024
9e2bb39
Add reference included by next
boodland Aug 19, 2024
297f5c9
Add comment to explain the default meta tags
boodland Aug 19, 2024
23afe00
Add public testing page to check the migration
boodland Aug 19, 2024
39d2c87
Merge branch 'next-js-migration' into app-router-migration-new-relic-…
boodland Sep 12, 2024
5fd9223
App router migration google tag manager script (#1117)
boodland Sep 17, 2024
c076abd
Merge branch 'next-js-migration' into app-router-migration-new-relic-…
boodland Sep 18, 2024
4effeae
App router migration rollbar script (#1118)
boodland Sep 20, 2024
40496ab
App router migration opengraph metadata (#1120)
boodland Sep 20, 2024
24fc604
Merge branch 'next-js-migration' into app-router-migration-new-relic-…
boodland Sep 20, 2024
5d9708b
App router migration error boundary component (#1121)
boodland Sep 20, 2024
bf7c899
App router migration material UI configuration (#1122)
boodland Sep 20, 2024
db8dd34
App router migration analytics (#1123)
boodland Sep 20, 2024
a673cd7
Merge branch 'next-js-migration' into app-router-migration-new-relic-…
boodland Sep 20, 2024
163adca
Merge branch 'next-js-migration' into app-router-migration-new-relic-…
eleanorreem Oct 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import Analytics from '../components/head/Analytics';
import GoogleTagManagerScript from '../components/head/GoogleTagManagerScript';
import RollbarScript from '../components/head/RollbarScript';
import ErrorBoundary from '../components/layout/ErrorBoundary';
import { newRelicInit } from '../config/newRelic';
import rootMetadata from './rootMetadata';
import RollbarScript from '../components/head/RollbarScript';
import ThemeRegistry from './ThemeRegistry';

export const metadata = rootMetadata;

export default function RootLayout({
export default async function RootLayout({
// Layouts must accept a children prop.
// This will be populated with nested layouts or pages
children,
}: {
children: React.ReactNode;
}) {
const NewRelicScript = await newRelicInit();
return (
<html lang="en">
<body>
Expand All @@ -26,6 +28,7 @@ export default function RootLayout({
<ErrorBoundary>
<ThemeRegistry>{children}</ThemeRegistry>
</ErrorBoundary>
{NewRelicScript}
<Analytics />
</body>
</html>
Expand Down
46 changes: 46 additions & 0 deletions config/newRelic.tsx
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 }}
/>
);
};
18 changes: 18 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,26 @@ const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
});

// Configuration according to Newrelic app router example
// See https://github.com/newrelic/newrelic-node-nextjs?tab=readme-ov-file#example-projects
const nrExternals = require('@newrelic/next/load-externals');

module.exports = withBundleAnalyzer(
withPWA({
experimental: {
// Without this setting, the Next.js compilation step will routinely
// try to import files such as `LICENSE` from the `newrelic` module.
// See https://nextjs.org/docs/app/api-reference/next-config-js/serverComponentsExternalPackages.
serverComponentsExternalPackages: ['newrelic'],
},

// In order for newrelic to effectively instrument a Next.js application,
// the modules that newrelic supports should not be mangled by webpack. Thus,
// we need to "externalize" all of the modules that newrelic supports.
webpack: (config) => {
nrExternals(config);
return config;
},
reactStrictMode: true,
publicRuntimeConfig: {
NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL,
Expand Down
Loading