next.js: Next build breaks third-party packages (Shaka Player) since 15.2.1
Link to the code that reproduces this issue
https://github.com/AndreyKovanov/reproduction-app-nextjs-shaka
To Reproduce
- Run
yarn build(next build) - Check production build (
.next/static/chunks/...) - Find
originalMcapfield (chunk with Shaka Player)
Current vs. Expected behavior
Original Shaka Player package contains the construction
Expected (Next.js before 15.2.1): build keeps this construction as is
Actual (Next.js since 15.2.1, including canary): build transforms this construction to another one
This transformation breaks work of Shaka Player in some cases and causes floating bugs in Safari
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 24.4.0: Fri Apr 11 18:33:47 PDT 2025; root:xnu-11417.101.15~117/RELEASE_ARM64_T6030
Available memory (MB): 36864
Available CPU cores: 12
Binaries:
Node: 22.13.1
npm: 10.9.2
Yarn: 1.22.22
pnpm: N/A
Relevant Packages:
next: 15.4.0-canary.3
eslint-config-next: N/A
react: 19.1.0
react-dom: 19.1.0
typescript: 5.8.3
Next.js Config:
output: N/A
Which area(s) are affected? (Select all that apply)
Not sure
Which stage(s) are affected? (Select all that apply)
next build (local)
Additional context
The issue started from 15.2.1 and still exists in 15.4.0-canary.3
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 9
- Comments: 26 (13 by maintainers)
Hi all, just summarizing the above and my 2cents on the matter.
Shaka Player has a polyfill that is installed on the
navigator.mediaCapabilitiesobject. The problem is, that safari tends to garbage collect this object, which removes the polyfill that shaka installs on it. Shaka has an internal workaround for this and this is where the issue comes from. I managed to confirm everything that has been written in the comments in this issue so far, but just summarizing it for the sake of clarity.Shaka Player roughly does the following workaround:
This seems like a workable solution and actually solves the garbage collection issue. The problem comes in when the above code is compiled using the Google Closure Compiler, used by Shaka Player. It comes down to this:
This still works, because the
tempvariable is assigned to theoriginalMcapproperty (which is exported) so Safari doesn’t do GC on it. But the actual logic is broken by this, because the class propertyoriginalMcapdoesn’t get assigned to thenavigator.mediaCapabilitiesobject, but instead install method assigns it to thetempvariable. The broken logic is never noticed because the logic is never used, it is only a workaround for the garbage collection issue, which still works.Now onto the real problem:
SWC@16+ now sees:
And optimizes it to:
From SWC’s perspective this is totally fine, since the
tempvariable cannot change in-between the declaration and assignment tonulland the following assignment topolyfill.roginalMcap. This however breaks the GC workaround, becausetempis not exported, onlypolyfill.originalMcapis, therefore seen as unused and thus subject to GC again.Now, how to get this resolved:
Simply do the preservation of the
navigator.mediaCapabilitiesin the consuming nextjs application in a manner that does not get stripped. This is a quick workaround that we’ve implemented that get’s us unblocked and we are able to update NextJS while coming up with a more permanent fix (which obviously has to be done on shaka, not here):nullto theoriginalMcapproperty, and only declare it without assigning anything to it, the bug is gone.shaka.polyfill.MediaCapabilities['originalMcap'] = navigator.mediaCapabilitieson the class property with the bracket string notation, the bug is also gone. This forces the Closure Compiler to keep theoriginalMcapproperty on the class, instead of collapsing it to a separate variable.With the latest Shaka-player update, the issue looks fixed ✅
@vincent-derks huge thanks for the fix 🙇♂
I took a look at the values of the minified variables, and I think it’s a bug in the Shaka player.
I did local patching to verify if the lack of the assignment is the cause, and it was not.
I can still reproduce with this change. I also debugged it a bit as I can read minified/transpiled code, but I couldn’t find any problem in the minified code.
🤔 I feel like, this issue has gotten to the point where each input we give has to be precise, to reduce noise… that being said:
I found this looking at the diffs between
15.2.1-canary.0and15.2.1-canary.1, so that’s already different.15.2.1-canary.1removes that assignment intoml-@AndreyKovanov beat me to it
True, now that I look more carefully, post 15.2.1-canary.1, we have:
but before, it was:
That does look like a normal transformation.
I think, we need more input from OP as to what really happens in Safari… @AndreyKovanov would you mind providing screenshots/video, error logs from console?
How did this ever work in shaka-player? This is their transpiled code.
YBis only assigned when another function (XB) is invoked but that clearly can’t happen in between declaration ofYBandWB.originalMcap = YB;so it seems safe to me that SWC just collapses these lines intoWB.originalMcap = null;