Repository: vercel/next.js
Pull request: Pass `filterStackFrame` everywhere

Also resolves https://github.com/vercel/next.js/pull/81344#pullrequestreview-3007204392



Changed files:
filename: packages/next/src/server/app-render/app-render.tsx
patch: @@ -253,6 +253,12 @@ const flightDataPathHeadKey = 'h'
 const getFlightViewportKey = (requestId: string) => requestId + 'v'
 const getFlightMetadataKey = (requestId: string) => requestId + 'm'
 
+const filterStackFrame =
+  process.env.NODE_ENV !== 'production'
+    ? (require('../lib/source-maps') as typeof import('../lib/source-maps'))
+        .filterStackFrameDEV
+    : undefined
+
 interface ParsedRequestHeaders {
   /**
    * Router state provided from the client-side router. Used to handle rendering
@@ -633,11 +639,6 @@ async function generateDynamicFlightRenderResult(
     )
   }
 
-  const filterStackFrame =
-    process.env.NODE_ENV !== 'production'
-      ? (require('../lib/source-maps') as typeof import('../lib/source-maps'))
-          .filterStackFrameDEV
-      : undefined
   // For app dir, use the bundled version of Flight server renderer (renderToReadableStream)
   // which contains the subset React.
   const flightReadableStream = workUnitAsyncStorage.run(
@@ -740,11 +741,6 @@ async function warmupDevRender(
     ctx
   )
 
-  const filterStackFrame =
-    process.env.NODE_ENV !== 'production'
-      ? (require('../lib/source-maps') as typeof import('../lib/source-maps'))
-          .filterStackFrameDEV
-      : undefined
   // For app dir, use the bundled version of Flight server renderer (renderToReadableStream)
   // which contains the subset React.
   workUnitAsyncStorage.run(
@@ -1877,11 +1873,6 @@ async function renderToStream(
   const setHeader = res.setHeader.bind(res)
   const appendHeader = res.appendHeader.bind(res)
 
-  const filterStackFrame =
-    process.env.NODE_ENV !== 'production'
-      ? (require('../lib/source-maps') as typeof import('../lib/source-maps'))
-          .filterStackFrameDEV
-      : undefined
   try {
     if (
       // We only want this behavior when running `next dev`
@@ -2368,11 +2359,6 @@ async function spawnDynamicValidationInDev(
     isNotFound
   )
 
-  const filterStackFrame =
-    process.env.NODE_ENV !== 'production'
-      ? (require('../lib/source-maps') as typeof import('../lib/source-maps'))
-          .filterStackFrameDEV
-      : undefined
   const pendingInitialServerResult = workUnitAsyncStorage.run(
     initialServerPrerenderStore,
     ComponentMod.prerender,
@@ -3004,12 +2990,6 @@ async function prerenderToStream(
         res.statusCode === 404
       )
 
-      const filterStackFrame =
-        process.env.NODE_ENV !== 'production'
-          ? (
-              require('../lib/source-maps') as typeof import('../lib/source-maps')
-            ).filterStackFrameDEV
-          : undefined
       const pendingInitialServerResult = workUnitAsyncStorage.run(
         initialServerPrerenderStore,
         ComponentMod.prerender,
@@ -3494,12 +3474,6 @@ async function prerenderToStream(
         ctx,
         res.statusCode === 404
       )
-      const filterStackFrame =
-        process.env.NODE_ENV !== 'production'
-          ? (
-              require('../lib/source-maps') as typeof import('../lib/source-maps')
-            ).filterStackFrameDEV
-          : undefined
       const reactServerResult = (reactServerPrerenderResult =
         await createReactServerPrerenderResultFromRender(
           workUnitAsyncStorage.run(
@@ -3730,12 +3704,6 @@ async function prerenderToStream(
         res.statusCode === 404
       )
 
-      const filterStackFrame =
-        process.env.NODE_ENV !== 'production'
-          ? (
-              require('../lib/source-maps') as typeof import('../lib/source-maps')
-            ).filterStackFrameDEV
-          : undefined
       const reactServerResult = (reactServerPrerenderResult =
         await createReactServerPrerenderResultFromRender(
           workUnitAsyncStorage.run(
@@ -3909,11 +3877,6 @@ async function prerenderToStream(
       errorType
     )
 
-    const filterStackFrame =
-      process.env.NODE_ENV !== 'production'
-        ? (require('../lib/source-maps') as typeof import('../lib/source-maps'))
-            .filterStackFrameDEV
-        : undefined
     const errorServerStream = workUnitAsyncStorage.run(
       prerenderLegacyStore,
       ComponentMod.renderToReadableStream,

filename: packages/next/src/server/app-render/collect-segment-data.tsx
patch: @@ -64,6 +64,12 @@ export type SegmentPrefetch = {
   isPartial: boolean
 }
 
+const filterStackFrame =
+  process.env.NODE_ENV !== 'production'
+    ? (require('../lib/source-maps') as typeof import('../lib/source-maps'))
+        .filterStackFrameDEV
+    : undefined
+
 function onSegmentPrerenderError(error: unknown) {
   const digest = getDigestForWellKnownError(error)
   if (digest) {
@@ -128,6 +134,7 @@ export async function collectSegmentData(
     />,
     clientModules,
     {
+      filterStackFrame,
       signal: abortController.signal,
       onError: onSegmentPrerenderError,
     }
@@ -349,6 +356,7 @@ async function renderSegmentPrefetch(
     segmentPrefetch,
     clientModules,
     {
+      filterStackFrame,
       signal: abortController.signal,
       onError: onSegmentPrerenderError,
     }
@@ -379,6 +387,7 @@ async function isPartialRSCData(
     abortController.abort()
   })
   await prerender(rsc, clientModules, {
+    filterStackFrame,
     signal: abortController.signal,
     onError() {},
     onPostpone() {

filename: packages/next/src/server/use-cache/use-cache-wrapper.ts
patch: @@ -80,6 +80,12 @@ const debug = process.env.NEXT_PRIVATE_DEBUG_CACHE
   ? console.debug.bind(console, 'use-cache:')
   : undefined
 
+const filterStackFrame =
+  process.env.NODE_ENV !== 'production'
+    ? (require('../lib/source-maps') as typeof import('../lib/source-maps'))
+        .filterStackFrameDEV
+    : undefined
+
 function generateCacheEntry(
   workStore: WorkStore,
   outerWorkUnitStore: WorkUnitStore | undefined,
@@ -446,6 +452,7 @@ async function generateCacheEntryImpl(
       clientReferenceManifest.clientModules,
       {
         environmentName: 'Cache',
+        filterStackFrame,
         signal: abortSignal,
         temporaryReferences,
         onError(error) {
@@ -490,12 +497,6 @@ async function generateCacheEntryImpl(
       stream = prelude
     }
   } else {
-    const filterStackFrame =
-      process.env.NODE_ENV !== 'production'
-        ? (require('../lib/source-maps') as typeof import('../lib/source-maps'))
-            .filterStackFrameDEV
-        : undefined
-
     stream = renderToReadableStream(
       resultPromise,
       clientReferenceManifest.clientModules,

filename: packages/next/types/$$compiled.internal.d.ts
patch: @@ -141,12 +141,17 @@ declare module 'react-server-dom-webpack/server.edge' {
     options?: {
       temporaryReferences?: TemporaryReferenceSet
       environmentName?: string | (() => string)
-      filterStackFrame?: (
-        url: string,
-        functionName: string,
-        lineNumber: number,
-        columnNumber: number
-      ) => boolean
+      // This is actually optional.
+      // But we want to not miss callsites accidentally and explicitly choose
+      // at each callsite which implementation to choose.
+      filterStackFrame:
+        | ((
+            url: string,
+            functionName: string,
+            lineNumber: number,
+            columnNumber: number
+          ) => boolean)
+        | undefined
       onError?: (error: unknown) => void
       onPostpone?: (reason: string) => void
       signal?: AbortSignal
@@ -267,12 +272,17 @@ declare module 'react-server-dom-webpack/static' {
     },
     options?: {
       environmentName?: string | (() => string)
-      filterStackFrame?: (
-        url: string,
-        functionName: string,
-        lineNumber: number,
-        columnNumber: number
-      ) => boolean
+      // This is actually optional.
+      // But we want to not miss callsites accidentally and explicitly choose
+      // at each callsite which implementation to choose.
+      filterStackFrame:
+        | ((
+            url: string,
+            functionName: string,
+            lineNumber: number,
+            columnNumber: number
+          ) => boolean)
+        | undefined
       identifierPrefix?: string
       signal?: AbortSignal
       temporaryReferences?: TemporaryReferenceSet