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

  1. Run yarn build (next build)
  2. Check production build (.next/static/chunks/...)
  3. Find originalMcap field (chunk with Shaka Player)

Current vs. Expected behavior

Original Shaka Player package contains the construction Image Image

Expected (Next.js before 15.2.1): build keeps this construction as is Image Image

Actual (Next.js since 15.2.1, including canary): build transforms this construction to another one Image Image

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)

Most upvoted comments

Hi all, just summarizing the above and my 2cents on the matter.

Shaka Player has a polyfill that is installed on the navigator.mediaCapabilities object. 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:

var polyfill = class {
  static install() {
    polyfill.originalMcap = navigator.mediaCapabilities;
  }
};

// With @export to make sure the originalMcap property is not removed by the compiler
polyfill.originalMcap = null;

polyfill.install();

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:

var polyfill = function() {
    return {
          install: function() {
            temp = navigator.mediaCapabilities;
        }
    };
};

var temp = null;
// @export
polyfill.originalMcap = temp;

polyfill.install();

This still works, because the temp variable is assigned to the originalMcap property (which is exported) so Safari doesn’t do GC on it. But the actual logic is broken by this, because the class property originalMcap doesn’t get assigned to the navigator.mediaCapabilities object, but instead install method assigns it to the temp variable. 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:

var temp = null;
// @export
polyfill.originalMcap = temp;

And optimizes it to:

var temp = null;
// @export
polyfill.originalMcap = null;

From SWC’s perspective this is totally fine, since the temp variable cannot change in-between the declaration and assignment to null and the following assignment to polyfill.roginalMcap. This however breaks the GC workaround, because temp is not exported, only polyfill.originalMcap is, therefore seen as unused and thus subject to GC again.

Now, how to get this resolved:

Simply do the preservation of the navigator.mediaCapabilities in 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):

  1. If we remove the assignment of null to the originalMcap property, and only declare it without assigning anything to it, the bug is gone.
  2. Because of how Google’s Closure Compiler works, if we assign like this shaka.polyfill.MediaCapabilities['originalMcap'] = navigator.mediaCapabilities on the class property with the bracket string notation, the bug is also gone. This forces the Closure Compiler to keep the originalMcap property 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.

Image

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:

+   navigator.mediaCapabilities,
- (ml = navigator.mediaCapabilities),

I found this looking at the diffs between 15.2.1-canary.0 and 15.2.1-canary.1, so that’s already different. 15.2.1-canary.1 removes that assignment into ml -

@AndreyKovanov beat me to it

True, now that I look more carefully, post 15.2.1-canary.1, we have:

var ml=null;mo.originalMcap=null;

but before, it was:

var ml=null;mo.originalMcap=ml;

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.

var YB = null;
WB.originalMcap = YB;

YB is only assigned when another function (XB) is invoked but that clearly can’t happen in between declaration of YB and WB.originalMcap = YB; so it seems safe to me that SWC just collapses these lines into WB.originalMcap = null;