fix(nextjs): Prevent sourceMappingURL stripping from truncating minified chunks - #24022
Open
oesnuj wants to merge 2 commits into
Open
fix(nextjs): Prevent sourceMappingURL stripping from truncating minified chunks#24022oesnuj wants to merge 2 commits into
oesnuj wants to merge 2 commits into
Conversation
…ied chunks The regex in stripSourceMappingURLComments did not require the comment to start at a line start, so sourceMappingURL-shaped text inside a string literal of a minified (single-line) chunk matched, and everything from the marker to EOF was deleted, leaving the chunk unparseable. Anchor the match to a line start and exclude whitespace/quote characters from the URL, matching how debug-id-upload.ts already reads the comment.
oesnuj
requested review from
mydea and
s1gr1d
and removed request for
a team
September 3, 2026 15:15
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
While deploying our app (Next.js 16.2.10 turbopack build,
@sentry/nextjs10.68.0,deleteSourcemapsAfterUpload: true), we noticed some JS chunks were shipped cut off mid-file and threwSyntaxError: Invalid or unexpected tokenon every page load. We traced it to the sourceMappingURL stripping step, and this PR addresses it with a small regex change.The regex in
stripSourceMappingURLCommentsdoesn't check that the comment starts at the beginning of a line:/\n?\/\/[#@] sourceMappingURL=[^\n]+$/So it also matches comment-shaped text inside a string literal — and since a minified chunk is a single line, everything from the match to EOF gets deleted:
In our case the text came from rrweb (the recording engine behind Amplitude Session Replay), which inlines its worker code as a string. The regex is unchanged in 10.73.0 and on current develop.
We fixed it by requiring the match to start at a line start — we've been running this in production via
pnpm patch, upstreaming it in case it's useful:/(?:^|\n)\/\/[#@] sourceMappingURL=[^\s'"`]+$/While digging around we noticed the repo already does this when reading the comment (
debug-id-upload.tsuses/^\s*\/\/# sourceMappingURL=(.*)$/m), so this aligns the stripping side with it. Genuine comments sit on their own line and are stripped exactly as before; string-embedded text no longer matches. Excluding whitespace/quotes from the URL matches how browsers read the value (data:URIs still work) and blocks the template-literal variant of the same issue. The CSS regex gets the same line-start condition.Added 4 tests — two reproduce the truncation and fail on the old regex, two pin existing behavior.
Would love to hear whether this approach makes sense. If you'd prefer a more minimal change, the URL charset restriction can be dropped — the line-start condition alone fixes the truncation.