From 2a49c1a693ccdc34a3831fc1b6c3679375b63033 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 9 Sep 2024 11:36:53 +0000 Subject: [PATCH 1/2] fix(nextjs): Widen removal of 404 transactions --- packages/nextjs/src/server/index.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/nextjs/src/server/index.ts b/packages/nextjs/src/server/index.ts index 1132a6e1eed2..f39932c5cb28 100644 --- a/packages/nextjs/src/server/index.ts +++ b/packages/nextjs/src/server/index.ts @@ -219,8 +219,13 @@ export function init(options: NodeOptions): NodeClient | undefined { return null; } - // Filter out /404 transactions for pages-router which seem to be created excessively - if (event.transaction === '/404') { + // Filter out /404 transactions which seem to be created excessively + if ( + // Pages router + event.transaction === '/404' || + // App router (could be "GET /404", "POST /404", ...) + event.transaction?.match(/^(GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH) \/404$/) + ) { return null; } From bbd3f1d7f762c2caf12722d6e48c4f0e796c9e59 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 9 Sep 2024 13:27:51 +0000 Subject: [PATCH 2/2] Add test --- .../nextjs-13/tests/server/404.test.ts | 23 +++++++++++++++++++ packages/nextjs/src/server/index.ts | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 dev-packages/e2e-tests/test-applications/nextjs-13/tests/server/404.test.ts diff --git a/dev-packages/e2e-tests/test-applications/nextjs-13/tests/server/404.test.ts b/dev-packages/e2e-tests/test-applications/nextjs-13/tests/server/404.test.ts new file mode 100644 index 000000000000..4c09bce36b4a --- /dev/null +++ b/dev-packages/e2e-tests/test-applications/nextjs-13/tests/server/404.test.ts @@ -0,0 +1,23 @@ +import { test } from '@playwright/test'; +import { waitForTransaction } from '@sentry-internal/test-utils'; + +test('should create a transaction for a CJS pages router API endpoint', async ({ page }) => { + let received404Transaction = false; + waitForTransaction('nextjs-13', async transactionEvent => { + return transactionEvent.transaction === 'GET /404' || transactionEvent.transaction === 'GET /_not-found'; + }).then(() => { + received404Transaction = true; + }); + + await page.goto('/page-that-doesnt-exist'); + + await new Promise((resolve, reject) => { + setTimeout(() => { + if (received404Transaction) { + reject(new Error('received 404 transaction')); + } else { + resolve(); + } + }, 5_000); + }); +}); diff --git a/packages/nextjs/src/server/index.ts b/packages/nextjs/src/server/index.ts index f39932c5cb28..2348ba203021 100644 --- a/packages/nextjs/src/server/index.ts +++ b/packages/nextjs/src/server/index.ts @@ -224,7 +224,8 @@ export function init(options: NodeOptions): NodeClient | undefined { // Pages router event.transaction === '/404' || // App router (could be "GET /404", "POST /404", ...) - event.transaction?.match(/^(GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH) \/404$/) + event.transaction?.match(/^(GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH) \/404$/) || + event.transaction === 'GET /_not-found' ) { return null; }