Skip to content

unstable_cache fails during build process with "Invariant: incrementalCache missing" error #35

@Lionel-Dutrieux

Description

@Lionel-Dutrieux

Plugin version

0.8.2

Payload version

3.37.0

Auth.js version

5.0.0-beta.28

Describe the bug

When using unstable_cache from Next.js in combination with the payload-authjs plugin, the application fails during the build process (pnpm build && pnpm start) with the following error:

Error: Invariant: incrementalCache missing in unstable_cache async function s(){return await new Promise(e=>setTimeout(e,100)),Math.floor(1e3*Math.random())+1}
    at <unknown> (C:\repos\Win\test-cache\.next\server\chunks\1906.js:711:4902)
    at Object.onInit (C:\repos\Win\test-cache\.next\server\app\(payload)\api\[...slug]\route.js:1:1800)
    at tJ.init (C:\repos\Win\test-cache\.next\server\chunks\1906.js:588:18842)
    at async tQ (C:\repos\Win\test-cache\.next\server\chunks\1906.js:588:20621)
    at async (C:\repos\Win\test-cache\.next\server\chunks\1906.js:710:16006)

The error occurs specifically when calling a cached function within the onInit hook of the Payload configuration. This works perfectly fine in a vanilla PayloadCMS setup without the payload-authjs plugin.

To Reproduce

Steps to reproduce the behavior:

  1. Create a Next.js 15.3.2 project with PayloadCMS 3.37.0

  2. Install and configure payload-authjs@0.8.2 plugin

  3. Create a cached function using unstable_cache:

    import { unstable_cache } from 'next/cache'
    
    async function generateRandomNumber(): Promise<number> {
      await new Promise((resolve) => setTimeout(resolve, 100))
      return Math.floor(Math.random() * 1000) + 1
    }
    
    export const getCachedRandomNumber = unstable_cache(generateRandomNumber, ['random-number'], {
      revalidate: 60,
      tags: ['random-number'],
    })
  4. Use the cached function in the onInit hook:

    export default buildConfig({
      // ... other config
      onInit: async (payload) => {
        const randomNumber = await getCachedRandomNumber()
        console.log(randomNumber)
        payload.logger.info(`Random number: ${randomNumber}`)
      },
    })
  5. Run pnpm build && pnpm start

  6. The error occurs during the initialization process

Additional context / Screenshots

Expected behavior

The cached function should work normally during the build process, just as it does in development mode and in vanilla PayloadCMS setups.

Environment

  • Node.js: 18.20.2+ / 20.9.0+
  • Package Manager: pnpm
  • Next.js: 15.3.2
  • PayloadCMS: 3.37.0
  • payload-authjs: 0.8.2
  • next-auth: 5.0.0-beta.28

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingstatus: verifiedIf an issue has been reproduced

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions