Open romeovs opened 3 weeks ago
in this case, 'use cache' isn't working in development mode, but it works well when you run 'next build' and 'next start.' It might be helpful if this were mentioned in the documentation as well.
@TheCodby I don't get that behaviour. If you run next build
in my repro repo, I get the following error:
Error: Filling a cache during prerender timed out, likely because request-specific arguments such as params, searchParams, cookies() or dynamic data were used inside "use cache".
at Timeout._onTimeout (/Users/romeo/code/romeovs/test/next-repro/.next/server/app/page.js:1:12894)
Error while saving cache key: 1:04e:["IByHVx-xX3_umBI9lT0k4","803dd5b6bde48097ee8b05827a035b1b11ca2ab099",["$@1"]] Error: Filling a cache during prerender timed out, likely because request-specific arguments such as params, searchParams, cookies() or dynamic data were used inside "use cache".
at Timeout._onTimeout (/Users/romeo/code/romeovs/test/next-repro/.next/server/app/page.js:1:12894)
Error: Route "/": A component accessed data, headers, params, searchParams, or a short-lived cache without a Suspense boundary nor a "use cache" above it. We don't have the exact line number added to error messages yet but you can see which component in the stack below. See more info: https://nextjs.org/docs/messages/next-prerender-missing-suspense
at div (<anonymous>)
at P (/Users/romeo/code/romeovs/test/next-repro/.next/server/chunks/113.js:1:10178)
at l (/Users/romeo/code/romeovs/test/next-repro/.next/server/chunks/113.js:1:20990)
at c (/Users/romeo/code/romeovs/test/next-repro/.next/server/chunks/113.js:1:21072)
at l (/Users/romeo/code/romeovs/test/next-repro/.next/server/chunks/113.js:1:18907)
at c (/Users/romeo/code/romeovs/test/next-repro/.next/server/chunks/113.js:1:19018)
at O (/Users/romeo/code/romeovs/test/next-repro/.next/server/chunks/113.js:1:11446)
at h (/Users/romeo/code/romeovs/test/next-repro/.next/server/chunks/113.js:1:6592)
at E (/Users/romeo/code/romeovs/test/next-repro/.next/server/chunks/113.js:1:8941)
at S (/Users/romeo/code/romeovs/test/next-repro/.next/server/chunks/113.js:1:9930)
at s (/Users/romeo/code/romeovs/test/next-repro/.next/server/chunks/113.js:1:23852)
at R (/Users/romeo/code/romeovs/test/next-repro/.next/server/chunks/113.js:1:11673)
at body (<anonymous>)
at html (<anonymous>)
at nu (/Users/romeo/code/romeovs/test/next-repro/node_modules/.pnpm/next@15.0.3-canary.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:127:23030)
at nc (/Users/romeo/code/romeovs/test/next-repro/node_modules/.pnpm/next@15.0.3-canary.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:127:23113)
at n_ (/Users/romeo/code/romeovs/test/next-repro/node_modules/.pnpm/next@15.0.3-canary.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:127:24343)
at r7 (/Users/romeo/code/romeovs/test/next-repro/node_modules/.pnpm/next@15.0.3-canary.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:127:20037)
at ne (/Users/romeo/code/romeovs/test/next-repro/node_modules/.pnpm/next@15.0.3-canary.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:127:20187)
at nk (/Users/romeo/code/romeovs/test/next-repro/node_modules/.pnpm/next@15.0.3-canary.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:127:29289)
at ServerInsertedHTMLProvider (/Users/romeo/code/romeovs/test/next-repro/node_modules/.pnpm/next@15.0.3-canary.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:127:3034)
at at (/Users/romeo/code/romeovs/test/next-repro/node_modules/.pnpm/next@15.0.3-canary.7_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:131:6010)
Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
Export encountered an error on /page: /, exiting the build.
⨯ Static worker exited with code: 1 and signal: null
Link to the code that reproduces this issue
https://github.com/romeovs/use-cached-closures-repro
To Reproduce
next dev
There will be the error:
app/page.tsx
to use../api/direct
instead of../api/closure
Current vs. Expected behavior
"use cache"
should work inside functions closures, and should be able to use closed over values. This will help with wrapping third-party clients with a Next.js cache.If that's not possible for some reason, I would like the Next.js compiler to make it clear in the error message and this should be documented somewhere. It would also seriously reduce the usefulness of the
"use cache"
pattern.Provide environment information
Which area(s) are affected? (Select all that apply)
dynamicIO
Which stage(s) are affected? (Select all that apply)
next dev (local), next start (local), Vercel (Deployed), Other (Deployed)
Additional context
No response