vercel / next.js

The React Framework
https://nextjs.org
MIT License
127.28k stars 27.02k forks source link

"use cache" with vercel otel - used `Math.random()` outside of `"use cache"` #72024

Open JamesRobertWiseman opened 3 weeks ago

JamesRobertWiseman commented 3 weeks ago

Link to the code that reproduces this issue

https://github.com/JamesRobertWiseman/nextjs-otel-use-cache

To Reproduce

  1. Start the application in development (next dev)
  2. Visit the home page of the application
  3. Error will be present. Error:
Screenshot 2024-10-29 at 14 54 02
[ Server ] Error: Route "/" used `Math.random()` outside of `"use cache"` and without explicitly calling `await connection()` beforehand. See more info here: https://nextjs.org/docs/messages/next-prerender-random
------------------
resolveErrorDev
./node_modules/.pnpm/next@15.0.2-canary.10_@opentelemetry+api@1.9.0_react-dom@19.0.0-rc-02c0e824-20241028_react@19_5el7z43pxvlkdtwozw72p3sfgu/node_modules/next/dist/compiled/react-server-dom-webpack/cjs/react-server-dom-webpack-client.browser.development.js
getOutlinedModel
./node_modules/.pnpm/next@15.0.2-canary.10_@opentelemetry+api@1.9.0_react-dom@19.0.0-rc-02c0e824-20241028_react@19_5el7z43pxvlkdtwozw72p3sfgu/node_modules/next/dist/compiled/react-server-dom-webpack/cjs/react-server-dom-webpack-client.browser.development.js
parseModelString
./node_modules/.pnpm/next@15.0.2-canary.10_@opentelemetry+api@1.9.0_react-dom@19.0.0-rc-02c0e824-20241028_react@19_5el7z43pxvlkdtwozw72p3sfgu/node_modules/next/dist/compiled/react-server-dom-webpack/cjs/react-server-dom-webpack-client.browser.development.js
Array.eval
./node_modules/.pnpm/next@15.0.2-canary.10_@opentelemetry+api@1.9.0_react-dom@19.0.0-rc-02c0e824-20241028_react@19_5el7z43pxvlkdtwozw72p3sfgu/node_modules/next/dist/compiled/react-server-dom-webpack/cjs/react-server-dom-webpack-client.browser.development.js
------------------
resolveConsoleEntry
./node_modules/.pnpm/next@15.0.2-canary.10_@opentelemetry+api@1.9.0_react-dom@19.0.0-rc-02c0e824-20241028_react@19_5el7z43pxvlkdtwozw72p3sfgu/node_modules/next/dist/compiled/react-server-dom-webpack/cjs/react-server-dom-webpack-client.browser.development.js
processFullStringRow
./node_modules/.pnpm/next@15.0.2-canary.10_@opentelemetry+api@1.9.0_react-dom@19.0.0-rc-02c0e824-20241028_react@19_5el7z43pxvlkdtwozw72p3sfgu/node_modules/next/dist/compiled/react-server-dom-webpack/cjs/react-server-dom-webpack-client.browser.development.js
processFullBinaryRow
./node_modules/.pnpm/next@15.0.2-canary.10_@opentelemetry+api@1.9.0_react-dom@19.0.0-rc-02c0e824-20241028_react@19_5el7z43pxvlkdtwozw72p3sfgu/node_modules/next/dist/compiled/react-server-dom-webpack/cjs/react-server-dom-webpack-client.browser.development.js
progress
./node_modules/.pnpm/next@15.0.2-canary.10_@opentelemetry+api@1.9.0_react-dom@19.0.0-rc-02c0e824-20241028_react@19_5el7z43pxvlkdtwozw72p3sfgu/node_modules/next/dist/compiled/react-server-dom-webpack/cjs/react-server-dom-webpack-client.browser.development.js

Current vs. Expected behavior

Current:

Using dynamicIO and @vercel/otel results in math.random outside of "use cache"; error.

Expected:

No error should appear, compiler should ignore any math.random calls in instrumentation.

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #26~22.04.1-Ubuntu SMP Thu Jul 11 22:33:04 UTC 2024
  Available memory (MB): 7930
  Available CPU cores: 2
Binaries:
  Node: 20.17.0
  npm: 10.8.2
  Yarn: 1.22.22
  pnpm: 9.11.0
Relevant Packages:
  next: 15.0.2-canary.10 // Latest available version is detected (15.0.2-canary.10).
  eslint-config-next: N/A
  react: 19.0.0-rc-02c0e824-20241028
  react-dom: 19.0.0-rc-02c0e824-20241028
  typescript: 5.3.3
Next.js Config:
  output: N/A

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

Instrumentation, Performance

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

next dev (local)

Additional context

No response

JamesRobertWiseman commented 3 weeks ago

@unstubbable appreciate your assistance in looking into this. Let me know if I can assist in any way.

dualdetail commented 2 weeks ago

I've gotten the above error, but I also get something similar when I use Date.now() - 'Route "/route" used Date.now() instead of using performance or without explicitly calling await connection() beforehand'. Would the PR address this as well?

unstubbable commented 2 weeks ago

I've gotten the above error, but I also get something similar when I use Date.now() - 'Route "/route" used Date.now() instead of using performance or without explicitly calling await connection() beforehand'. Would the PR address this as well?

No, this issue is specifically for the internal Math.random() usage in @vercel/otel.

When you use Date.now() in your application code with dynamicIO enabled, you need to express how you intend to use the date. I.e. is it supposed to be used in a static or dynamic context? You can find more info at https://nextjs.org/docs/messages/next-prerender-current-time.

ellemedit commented 1 week ago

@unstubbable // is there any escape hatch for custom open telemetry(instead of @vercel/otel)? especially, random id generator depends on Math.random() things.