fix: minimization issue with Next.js 15+#2059
Conversation
WalkthroughThe change updates the assignment of Changes
Poem
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
npm warn EBADENGINE Unsupported engine { 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (7)
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
| }; | ||
|
|
||
| xhr.onreadystatechange = function () { | ||
| xhr.onreadystatechange = () => { |
There was a problem hiding this comment.
also, if function expressions are a problem in SWC minimization, shouldn't we also change xhr.onerror and xhr.ontimeout assignments in this file to also use arrow functions just to be safe?
There was a problem hiding this comment.
I looked into minimized code and
a.onerror=function(e){u(e,"XHR error occurred",null,400)}and
a.ontimeout=function(e){u(e,"Request timed out",null,408)}it's nothing to inline here. On other hand, here is onreadystatechange:
a.onreadystatechange=function(){let i=a.readyState;i<3||0!==a.status&&(void 0===t&&(t=a.status,(()=>{var s;if(clearTimeout(r),n=t<400,204==t)return this.complete(null,null,null,null,t)...To fix this properly, we need to decide on a strategy for all function declarations across all SDKs. I think it would be good to release this fix first so SDK users don’t have to wait. This definitely doesn’t introduce any new problems but does fix some existing ones.
There was a problem hiding this comment.
I looked into minimized code and
a.onerror=function(e){u(e,"XHR error occurred",null,400)}
and
a.ontimeout=function(e){u(e,"Request timed out",null,408)}
huh, so there are multiple places which call errorHandler so it doesn't get inlined and as a result xhr.onerror and xhr.ontimeout don't break
There was a problem hiding this comment.
I mean, changing xhr.onerror and xhr.ontimeout to arrow functions is trivial but if you'd like to keep this PR specific and fix the immediate problem with xhr.onreadystatechange then sure. Will approve the PR as it fixes the problem
Starting with v15, minification cannot be customized using next.config.js. SWC that Next.js uses for minimization, inlines functions inside `XHRRequest` class, that results in losing `this` context and broken code
28e365d to
2af70ce
Compare
Related to #2057
SWC that Next.js uses for minimization, inlines functions inside
XHRRequestclass, that results in losingthiscontext and broken code.Unfortunately there is no easy way to fix this on the Next.js 15+ side:
This PR fixes a single immediate issue. Another problem with other minimizers may appear soon. I’ll create a RFC with code practices we should adopt to avoid this in the future.
Summary by CodeRabbit