Skip to content

Commit 739d283

Browse files
committed
[dev-overlay] Only warn once per invalid sourcemap
1 parent 50a4698 commit 739d283

File tree

6 files changed

+38
-11
lines changed

6 files changed

+38
-11
lines changed

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ bench/nested-deps/components/**/*
4848
**/.tina/__generated__/**
4949
test/lib/amp-validator-wasm.js
5050
test/production/pages-dir/production/fixture/amp-validator-wasm.js
51+
test/e2e/app-dir/server-source-maps/fixtures/default/app/bad-sourcemap/page.js
5152
test/e2e/app-dir/server-source-maps/fixtures/default/internal-pkg/ignored.js
5253
test/e2e/app-dir/server-source-maps/fixtures/default/internal-pkg/sourcemapped.js
5354
test/e2e/app-dir/server-source-maps/fixtures/default/external-pkg/sourcemapped.js

packages/next/src/server/patch-error-inspect.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ interface IgnoreableStackFrame extends StackFrame {
5757

5858
type SourceMapCache = Map<
5959
string,
60-
{ map: SyncSourceMapConsumer; payload: ModernSourceMapPayload }
60+
null | { map: SyncSourceMapConsumer; payload: ModernSourceMapPayload }
6161
>
6262

6363
function frameToString(frame: StackFrame): string {
@@ -208,6 +208,9 @@ function getSourcemappedFrameIfPossible(
208208
console.error(
209209
`${sourceURL}: Invalid source map. Only conformant source maps can be used to find the original code. Cause: ${cause}`
210210
)
211+
// If loading fails once, it'lll fail every time.
212+
// So set the cache to avoid duplicate errors.
213+
sourceMapCache.set(frame.file, null)
211214
// Don't even fall back to the bundler because it might be not as strict
212215
// with regards to parsing and then we fail later once we consume the
213216
// source map payload.
@@ -235,12 +238,20 @@ function getSourcemappedFrameIfPossible(
235238
console.error(
236239
`${sourceURL}: Invalid source map. Only conformant source maps can be used to find the original code. Cause: ${cause}`
237240
)
241+
// If creating the consumer fails once, it'll fail every time.
242+
// So set the cache to avoid duplicate errors.
243+
sourceMapCache.set(frame.file, null)
238244
return createUnsourcemappedFrame(frame)
239245
}
240246
sourceMapCache.set(frame.file, {
241247
map: sourceMapConsumer,
242248
payload: sourceMapPayload,
243249
})
250+
} else if (sourceMapCacheEntry === null) {
251+
// We failed earlier getting the payload or consumer.
252+
// Just return an unsourcemapped frame.
253+
// Errors will already be logged.
254+
return createUnsourcemappedFrame(frame)
244255
} else {
245256
sourceMapConsumer = sourceMapCacheEntry.map
246257
sourceMapPayload = sourceMapCacheEntry.payload

test/e2e/app-dir/server-source-maps/fixtures/default/app/bad-sourcemap/page.js

+8-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/e2e/app-dir/server-source-maps/fixtures/default/app/bad-sourcemap/page.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/e2e/app-dir/server-source-maps/fixtures/default/bad-sourcemap/page.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44

55
import { connection } from 'next/server'
66

7+
function logError() {
8+
console.error(new Error('Boom!'))
9+
}
10+
711
export default async function Page() {
812
await connection()
9-
console.error(new Error('Boom!'))
13+
logError()
1014
return <p>Hello, Dave!</p>
1115
}

test/e2e/app-dir/server-source-maps/server-source-maps.test.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -262,15 +262,16 @@ describe('app-dir - server source maps', () => {
262262
expect(normalizeCliOutput(next.cliOutput.slice(outputIndex))).toContain(
263263
'Error: Boom!'
264264
)
265-
})
265+
}, 60000)
266266

267267
if (isNextDev) {
268268
if (isTurbopack) {
269269
expect(normalizeCliOutput(next.cliOutput.slice(outputIndex))).toContain(
270270
// Node.js is fine with invalid URLs in index maps apparently.
271271
'' +
272272
'\nError: Boom!' +
273-
'\n at Page (custom://[badhost]/app/bad-sourcemap/page.js:9:15)' +
273+
'\n at logError (custom://[badhost]/app/bad-sourcemap/page.js:14:19)' +
274+
'\n at Page (custom://[badhost]/app/bad-sourcemap/page.js:18:5)' +
274275
// TODO: Remove blank line
275276
'\n'
276277
)
@@ -282,8 +283,15 @@ describe('app-dir - server source maps', () => {
282283
'' +
283284
`\nwebpack-internal:///(rsc)/./app/bad-sourcemap/page.js: Invalid source map. Only conformant source maps can be used to find the original code. Cause: TypeError [ERR_INVALID_ARG_TYPE]: The "payload" argument must be of type object. Received null` +
284285
'\nError: Boom!' +
285-
'\n at Page (webpack-internal:///(rsc)/./app/bad-sourcemap/page.js:15:19)'
286+
'\n at logError (webpack-internal:///(rsc)/./app/bad-sourcemap/page.js:14:19)' +
287+
'\n at Page (webpack-internal:///(rsc)/./app/bad-sourcemap/page.js:18:5)'
286288
)
289+
// Expect the invalid sourcemap warning only once.
290+
expect(
291+
normalizeCliOutput(next.cliOutput.slice(outputIndex)).split(
292+
'Invalid source map.'
293+
).length - 1
294+
).toEqual(1)
287295
}
288296
} else {
289297
// TODO: test `next start` with `--enable-source-maps`

0 commit comments

Comments
 (0)