Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Suspense } from 'react';
import { headers } from 'next/headers';
import * as Sentry from '@sentry/nextjs';

async function CachedContent() {
const getTodos = async () => {
return Sentry.startSpan({ name: 'getTodos', op: 'get.todos' }, async () => {
'use cache';
await new Promise(resolve => setTimeout(resolve, 100));
return [1, 2, 3, 4, 5];
});
};

const todos = await getTodos();

return <div id="todos-fetched">Todos fetched: {todos.length}</div>;
}

async function DynamicContent() {
await headers();
return (
<>
<CachedContent />
</>
);
}

export default function Page() {
return (
<>
<h1>Cache Pageload Tracing</h1>
<Suspense fallback={<div>Loading...</div>}>
<DynamicContent />
</Suspense>
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@sentry/nextjs": "file:../../packed/sentry-nextjs-packed.tgz",
"@sentry/core": "file:../../packed/sentry-core-packed.tgz",
"import-in-the-middle": "^1",
"next": "16.2.3",
"next": "^16",
"react": "19.1.0",
"react-dom": "19.1.0",
"require-in-the-middle": "^7",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,38 @@ test('Should generate metadata async', async ({ page }) => {
await expect(page).toHaveTitle('Product: 1');
});

test('Prerendered shell does not stitch the pageload onto a stale trace', async ({ page }) => {
const serverTxPromise = waitForTransaction('nextjs-16-cacheComponents', async transactionEvent => {
return (
transactionEvent.contexts?.trace?.op === 'http.server' && transactionEvent.transaction === 'GET /pageload-tracing'
);
});

const pageloadTxPromise = waitForTransaction('nextjs-16-cacheComponents', async transactionEvent => {
return transactionEvent.contexts?.trace?.op === 'pageload' && transactionEvent.transaction === '/pageload-tracing';
});

await page.goto('/pageload-tracing');

await expect(page.locator('#todos-fetched')).toHaveText('Todos fetched: 5');

const [serverTx, pageloadTx] = await Promise.all([serverTxPromise, pageloadTxPromise]);

const serverTraceId = serverTx.contexts?.trace?.trace_id;
const pageloadTraceId = pageloadTx.contexts?.trace?.trace_id;

// Under Cache Components the shell is prerendered and rendered in a context detached from the
// runtime server request, so a `sentry-trace` meta tag would carry a stale/unrelated trace. The
// SDK therefore does not enable the trace meta tags, and the browser pageload starts a fresh trace
// instead of stitching onto a trace that doesn't match the server request.
expect(pageloadTraceId).toBeTruthy();
expect(serverTraceId).not.toBe(pageloadTraceId);

// No trace meta tags should be injected when Cache Components is enabled.
expect(await page.locator('meta[name="sentry-trace"]').count()).toBe(0);
expect(await page.locator('meta[name="baggage"]').count()).toBe(0);
});

test('Should prerender a page that captures an exception in generateMetadata', async ({ page }) => {
await page.goto('/capture-metadata');

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/versions

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*

# env files (can opt-in for committing if needed)
.env*

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts

# Sentry Config File
.env.sentry-build-plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Suspense } from 'react';
import * as Sentry from '@sentry/nextjs';

export default function Page() {
return (
<>
<h1>This will be pre-rendered</h1>
<DynamicContent />
</>
);
}

async function DynamicContent() {
const getTodos = async () => {
return Sentry.startSpan({ name: 'getTodos', op: 'get.todos' }, async () => {
'use cache';
await new Promise(resolve => setTimeout(resolve, 100));
return [1, 2, 3, 4, 5];
});
};

const todos = await getTodos();

return <div id="todos-fetched">Todos fetched: {todos.length}</div>;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use client';

import * as Sentry from '@sentry/nextjs';
import NextError from 'next/error';
import { useEffect } from 'react';

export default function GlobalError({ error }: { error: Error & { digest?: string } }) {
useEffect(() => {
Sentry.captureException(error);
}, [error]);

return (
<html>
<body>
{/* `NextError` is the default Next.js error page component. Its type
definition requires a `statusCode` prop. However, since the App Router
does not expose status codes for errors, we simply pass 0 to render a
generic error message. */}
<NextError statusCode={0} />
</body>
</html>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as Sentry from '@sentry/nextjs';

function fetchPost() {
return Promise.resolve({ id: '1', title: 'Post 1' });
}

export async function generateMetadata() {
const { id } = await fetchPost();
const product = `Product: ${id}`;

return {
title: product,
};
}

export default function Page() {
return (
<>
<h1>This will be pre-rendered</h1>
<DynamicContent />
</>
);
}

async function DynamicContent() {
const getTodos = async () => {
return Sentry.startSpan({ name: 'getTodos', op: 'get.todos' }, async () => {
'use cache';
await new Promise(resolve => setTimeout(resolve, 100));
return [1, 2, 3, 4, 5];
});
};

const todos = await getTodos();

return <div id="todos-fetched">Todos fetched: {todos.length}</div>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as Sentry from '@sentry/nextjs';

/**
* Tests generateMetadata function with cache components, this calls the propagation context to be set
* Which will generate and set a trace id in the propagation context, which should trigger the random API error if unpatched
* See: https://github.com/getsentry/sentry-javascript/issues/18392
*/
export function generateMetadata() {
return {
title: 'Cache Components Metadata Test',
};
}

export default function Page() {
return (
<>
<h1>This will be pre-rendered</h1>
<DynamicContent />
</>
);
}

async function DynamicContent() {
const getTodos = async () => {
return Sentry.startSpan({ name: 'getTodos', op: 'get.todos' }, async () => {
'use cache';
await new Promise(resolve => setTimeout(resolve, 100));
return [1, 2, 3, 4, 5];
});
};

const todos = await getTodos();

return <div id="todos-fetched">Todos fetched: {todos.length}</div>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <p>Next 16 CacheComponents test app</p>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Suspense } from 'react';
import { headers } from 'next/headers';
import * as Sentry from '@sentry/nextjs';

async function CachedContent() {
const getTodos = async () => {
return Sentry.startSpan({ name: 'getTodos', op: 'get.todos' }, async () => {
'use cache';
await new Promise(resolve => setTimeout(resolve, 100));
return [1, 2, 3, 4, 5];
});
};

const todos = await getTodos();

return <div id="todos-fetched">Todos fetched: {todos.length}</div>;
}

async function DynamicContent() {
await headers();
return (
<>
<CachedContent />
</>
);
}

export default function Page() {
return (
<>
<h1>Cache Pageload Tracing</h1>
<Suspense fallback={<div>Loading...</div>}>
<DynamicContent />
</Suspense>
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Suspense } from 'react';
import * as Sentry from '@sentry/nextjs';

export default function Page() {
return (
<>
<h1>This will be pre-rendered</h1>
<Suspense fallback={<div>Loading...</div>}>
<DynamicContent />
</Suspense>
</>
);
}

async function DynamicContent() {
const getTodos = async () => {
return Sentry.startSpan({ name: 'getTodos', op: 'get.todos' }, async () => {
await new Promise(resolve => setTimeout(resolve, 100));
return [1, 2, 3, 4, 5];
});
};

const todos = await getTodos();

return <div id="todos-fetched">Todos fetched: {todos.length}</div>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import { FlatCompat } from '@eslint/eslintrc';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const compat = new FlatCompat({
baseDirectory: __dirname,
});

const eslintConfig = [
...compat.extends('next/core-web-vitals', 'next/typescript'),
{
ignores: ['node_modules/**', '.next/**', 'out/**', 'build/**', 'next-env.d.ts'],
},
];

export default eslintConfig;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as Sentry from '@sentry/nextjs';

Sentry.init({
environment: 'qa', // dynamic sampling bias to keep transactions
dsn: process.env.NEXT_PUBLIC_E2E_TEST_DSN,
tunnel: `http://localhost:3031/`, // proxy server
tracesSampleRate: 1.0,
sendDefaultPii: true,
integrations: [Sentry.spanStreamingIntegration()],
});

export const onRouterTransitionStart = Sentry.captureRouterTransitionStart;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as Sentry from '@sentry/nextjs';

export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
await import('./sentry.server.config');
}

if (process.env.NEXT_RUNTIME === 'edge') {
await import('./sentry.edge.config');
}
}

export const onRequestError = Sentry.captureRequestError;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { withSentryConfig } from '@sentry/nextjs';
import type { NextConfig } from 'next';

const nextConfig: NextConfig = {
cacheComponents: true,
};

export default withSentryConfig(nextConfig, {
silent: true,
});
Loading
Loading