Skip to content

Running an effect in NextJS 16.0.1 with cacheComponents causes: Cannot access Date.now(), Date(), or new Date() before other uncached data or Request data in a Server Component #5706

@bbreunig

Description

@bbreunig

What version of Effect is running?

[email protected]

What steps can reproduce the bug?

  1. Install NextJS v16.0.1
  2. Set cacheComponents: true in next.config.ts
  3. Use Effect.runPromise or Effect.runPromiseExit or a custum runtime.runPromise, etc, in any React Server Component on a page
  4. Run pnpm dev
  5. Go to the page
  6. See the error.

What is the expected behavior?

There should be no error and the build shouldn't fail.

What do you see instead?

Route "/my_page" used Date.now() before accessing either uncached data (e.g. fetch()) or Request data (e.g. cookies(), headers(), connection(), and searchParams). Accessing the current time in a Server Component requires reading one of these data sources first. Alternatively, consider moving this expression into a Client Component or Cache Component. See more info here: https://nextjs.org/docs/messages/next-prerender-current-time

Additional information

Responsible file:
node_modules/.pnpm/[email protected]/node_modules/effect/src/internal/fiberId.ts

Responisble function:

/** @internal */
export const unsafeMake = (): FiberId.Runtime => {
  const id = MutableRef.get(_fiberCounter)
  pipe(_fiberCounter, MutableRef.set(id + 1))
  return new Runtime(id, Date.now()) // <- Date.now() most likely causes the issue
}

This only happens if you activate cacheComponents. When cacheComponents are not active, you don't get an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions