nextjs - 💡(How to fix) Fix Regression in mangling in v16 canary that breaks Sanity Studio [5 comments, 4 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#85107Fetched 2026-04-08 02:17:26
View on GitHub
Comments
5
Participants
4
Timeline
16
Reactions
0
Author
Timeline (top)
commented ×5labeled ×3subscribed ×2unsubscribed ×2

Error Message

ReferenceError: randomKey2 is not defined at TURBOPACK__module__evaluation (.next/server/chunks/ssr/[root-of-the-server]__e1e09896._.js:118:9919) at instantiateModule (.next/server/chunks/ssr/[turbopack]_runtime.js:715:9) at getOrInstantiateModuleFromParent (.next/server/chunks/ssr/[turbopack]_runtime.js:738:12) at Context.esmImport [as i] (.next/server/chunks/ssr/[turbopack]runtime.js:228:20) at TURBOPACK__module__evaluation (.next/server/chunks/ssr/[root-of-the-server]__12752159..js:1:12607) at instantiateModule (.next/server/chunks/ssr/[turbopack]_runtime.js:715:9) at getOrInstantiateModuleFromParent (.next/server/chunks/ssr/[turbopack]_runtime.js:738:12) at Context.commonJsRequire [as r] (.next/server/chunks/ssr/[turbopack]_runtime.js:249:12)

Code Example

ReferenceError: randomKey2 is not defined
    at __TURBOPACK__module__evaluation__ (.next/server/chunks/ssr/[root-of-the-server]__e1e09896._.js:118:9919)
    at instantiateModule (.next/server/chunks/ssr/[turbopack]_runtime.js:715:9)
    at getOrInstantiateModuleFromParent (.next/server/chunks/ssr/[turbopack]_runtime.js:738:12)
    at Context.esmImport [as i] (.next/server/chunks/ssr/[turbopack]_runtime.js:228:20)
    at __TURBOPACK__module__evaluation__ (.next/server/chunks/ssr/[root-of-the-server]__12752159._.js:1:12607)
    at instantiateModule (.next/server/chunks/ssr/[turbopack]_runtime.js:715:9)
    at getOrInstantiateModuleFromParent (.next/server/chunks/ssr/[turbopack]_runtime.js:738:12)
    at Context.commonJsRequire [as r] (.next/server/chunks/ssr/[turbopack]_runtime.js:249:12)

---

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.5.0: Tue Apr 22 19:53:27 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6041
  Available memory (MB): 131072
  Available CPU cores: 16
Binaries:
  Node: 24.6.0
  npm: 11.5.1
  Yarn: 1.22.22
  pnpm: 9.15.9
Relevant Packages:
  next: 16.0.0-canary.15 // Latest available version is detected (16.0.0-canary.15).
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: 5.9.3
Next.js Config:
  output: N/A

---

import { randomKey as randomKey$1 } from "@sanity/util/content";

---

function randomKey(length) {
  const table2 = getByteHexTable();
  return whatwgRNG(length).reduce((str, n) => str + table2[n], "").slice(0, length);
}
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/sanity-io/nextjs-mangle-reproduction

To Reproduce

  1. Run npm install
  2. Run next build or next build --webpack

Current vs. Expected behavior

Currently it crashes:

ReferenceError: randomKey2 is not defined
    at __TURBOPACK__module__evaluation__ (.next/server/chunks/ssr/[root-of-the-server]__e1e09896._.js:118:9919)
    at instantiateModule (.next/server/chunks/ssr/[turbopack]_runtime.js:715:9)
    at getOrInstantiateModuleFromParent (.next/server/chunks/ssr/[turbopack]_runtime.js:738:12)
    at Context.esmImport [as i] (.next/server/chunks/ssr/[turbopack]_runtime.js:228:20)
    at __TURBOPACK__module__evaluation__ (.next/server/chunks/ssr/[root-of-the-server]__12752159._.js:1:12607)
    at instantiateModule (.next/server/chunks/ssr/[turbopack]_runtime.js:715:9)
    at getOrInstantiateModuleFromParent (.next/server/chunks/ssr/[turbopack]_runtime.js:738:12)
    at Context.commonJsRequire [as r] (.next/server/chunks/ssr/[turbopack]_runtime.js:249:12)

Disabling mangling avoids the crash: next build --no-mangling. Downgrading to next v15 also avoids crash, without requiring --no-mangling.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.5.0: Tue Apr 22 19:53:27 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6041
  Available memory (MB): 131072
  Available CPU cores: 16
Binaries:
  Node: 24.6.0
  npm: 11.5.1
  Yarn: 1.22.22
  pnpm: 9.15.9
Relevant Packages:
  next: 16.0.0-canary.15 // Latest available version is detected (16.0.0-canary.15).
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: 5.9.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Output

Which stage(s) are affected? (Select all that apply)

next build (local)

Additional context

It happens with both --turbopack and --webpack, so it seems like the mangling/minify logic is shared in next.js regardless of bundler. It also happens regardless of what is being imported from the sanity package, even if it's just definePlugin, defineConfig or similar type asserters that in JS are just function defineConfig(config){return config}.

As for the error itself that references dist_randomKey, that specific identifier does not exist in the original ./node_modules/sanity/lib/index.mjs. But there are two cases of randomKey: On line 74 it's aliased

import { randomKey as randomKey$1 } from "@sanity/util/content";

Further down on lines 17733-17736 there's a local definition:

function randomKey(length) {
  const table2 = getByteHexTable();
  return whatwgRNG(length).reduce((str, n) => str + table2[n], "").slice(0, length);
}

extent analysis

TL;DR

  • Disabling mangling with --no-mangling or downgrading to next v15 may resolve the ReferenceError issue.

Guidance

  • The error seems to be related to the mangling/minify logic in next.js, which is shared regardless of the bundler used (--turbopack or --webpack).
  • Verify that the issue is indeed caused by mangling by running next build --no-mangling and checking if the error persists.
  • If downgrading to next v15 is a viable option, try running npm install next@15 and then next build to see if the issue is resolved.
  • Investigate the randomKey function and its usage in the sanity package to understand why it's being referenced as randomKey2 in the error message.

Example

  • No specific code snippet can be provided without further investigation, but checking the sanity package's index.mjs file for any references to randomKey or randomKey2 may be a good starting point.

Notes

  • The issue seems to be specific to next.js version 16.0.0-canary.15, so upgrading or downgrading may be necessary to resolve the issue.
  • The error message references a randomKey2 identifier that does not exist in the original ./node_modules/sanity/lib/index.mjs file, which may indicate a naming conflict or incorrect mangling.

Recommendation

  • Apply workaround: Disable mangling with --no-mangling until a more permanent fix is available, as downgrading to next v15 may not be a viable long-term solution.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

nextjs - 💡(How to fix) Fix Regression in mangling in v16 canary that breaks Sanity Studio [5 comments, 4 participants]