Repository navigation
Commit 344be6b
fix(nextjs): Prevent sourceMappingURL stripping from truncating minified chunks (#24022)
While deploying our app (Next.js 16.2.10 turbopack build,
`@sentry/nextjs` 10.68.0, `deleteSourcemapsAfterUpload: true`), we
noticed some JS chunks were shipped cut off mid-file and threw
`SyntaxError: Invalid or unexpected token` on every page load. We traced
it to the sourceMappingURL stripping step, and this PR addresses it with
a small regex change.
The regex in `stripSourceMappingURLComments` doesn't check that the
comment starts at the beginning of a line:
```js
/\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:
```js
// minified chunk — comment-shaped text inside a string
const a=1;const worker='self.onmessage=()=>{};\n//# sourceMappingURL=worker.js.map\n';use(worker);const b=2;
// after stripping — truncated mid-string, no longer parseable
const a=1;const worker='self.onmessage=()=>{};\n
```
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:
```js
/(?:^|\n)\/\/[#@] sourceMappingURL=[^\s'"`]+$/
```
While digging around we noticed the repo already does this when
*reading* the comment (`debug-id-upload.ts` uses `/^\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.
---------
Co-authored-by: Sigrid <32902192+s1gr1d@users.noreply.github.com>1 parent 7de3c72 commit 344be6b
2 files changed
Lines changed: 106 additions & 2 deletions
File tree
- packages/nextjs
- src/config
- test/config
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
137 | 137 | | |
138 | 138 | | |
139 | 139 | | |
140 | | - | |
141 | | - | |
| 140 | + | |
| 141 | + | |
142 | 142 | | |
143 | 143 | | |
144 | 144 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
740 | 740 | | |
741 | 741 | | |
742 | 742 | | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
743 | 847 | | |
0 commit comments