Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"use cache" cannot call function from inside closure #72403

Open
romeovs opened this issue Nov 6, 2024 · 2 comments
Open

"use cache" cannot call function from inside closure #72403

romeovs opened this issue Nov 6, 2024 · 2 comments
Labels
bug Issue was opened via the bug report template. dynamicIO Related to dynamicIO.

Comments

@romeovs
Copy link

romeovs commented Nov 6, 2024

Link to the code that reproduces this issue

https://github.com/romeovs/use-cached-closures-repro

To Reproduce

  1. next dev
  2. open http://localhost:3000

There will be the error:

[ Server ] Error: generate is not defined
  1. edit app/page.tsx to use ../api/direct instead of ../api/closure
  2. open http://localhost:3000, there will be no errors

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.

function defineClient() {
  function handler() {
    // from a library
    return client.fetch()
  }

  async function get() {
    "use cache"
     // this should be able to call handler but it fails
     return await handler()
  }

  return { get }
}

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

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.6.0: Mon Jul 29 21:14:46 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T6031
  Available memory (MB): 36864
  Available CPU cores: 14
Binaries:
  Node: 20.18.0
  npm: 10.8.2
  Yarn: 1.22.21
  pnpm: 9.12.3
Relevant Packages:
  next: 15.0.3-canary.7 // Latest available version is detected (15.0.3-canary.7).
  eslint-config-next: N/A
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.6.3
Next.js Config:
  output: N/A

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

@romeovs romeovs added the bug Issue was opened via the bug report template. label Nov 6, 2024
@github-actions github-actions bot added the dynamicIO Related to dynamicIO. label Nov 6, 2024
@TheCodby
Copy link

TheCodby commented Nov 7, 2024

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.

@romeovs
Copy link
Author

romeovs commented Nov 8, 2024

@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/[email protected][email protected][email protected][email protected]/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/[email protected][email protected][email protected][email protected]/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/[email protected][email protected][email protected][email protected]/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/[email protected][email protected][email protected][email protected]/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/[email protected][email protected][email protected][email protected]/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/[email protected][email protected][email protected][email protected]/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/[email protected][email protected][email protected][email protected]/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/[email protected][email protected][email protected][email protected]/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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. dynamicIO Related to dynamicIO.
Projects
None yet
Development

No branches or pull requests

2 participants