Skip to content

Using auth() or currentUser() inside a server action, in nextjs 14.1.0, produces build errors. #2710

@aasmal97

Description

@aasmal97

Preliminary Checks

Reproduction / Replay Link

https://github.com/aasmal97/Clerk-Authentication-Dynamic-Server-Usage-Error

Publishable key

pk_test_bGVhZGluZy1kb2Jlcm1hbi00MC5jbGVyay5hY2NvdW50cy5kZXYk

Description

Issue:

Using auth() or currentUser() inside a server action, that is imported into a page.tsx file, results in the following build error:

[Error]: Dynamic server usage: Page couldn't be rendered statically because it used `cookies`. See more info here: https://nextjs.org/docs/messages/dynamic-server-error

This would cause/force the page to be client-rendered, potentially defeating the purpose of using nextjs. Though this goes away when the entire server action is defined within the page itself, this creates duplicate code when the same actions need to be re-used.

Steps to reproduce:

  1. Create a page.tsx file
  2. Create a create a action.ts file, and create a server action, that calls auth() or currentUser() within it.
  3. Import server action into page.tsx
  4. Run npm run build
  5. View log error in command line

Expected behavior:

  • No build errors that indicate dynamic rendering

Actual behavior:

The following build error arises.

[Error]: Dynamic server usage: Page couldn't be rendered statically because it used `cookies`. See more info here: https://nextjs.org/docs/messages/dynamic-server-error

Potential Patch
Have an optional cookies param, that allows cookies to be passed directly into auth(), so that we can define the context. This would allow us to call cookies inside the server action, similar to this supabase issue.

Environment

System:
    OS: Windows 11 10.0.22621
    CPU: (12) x64 AMD Ryzen 5 3600X 6-Core Processor
    Memory: 22.67 GB / 47.95 GB
  Binaries:
    Node: 18.18.0 - C:\nodejs\node.EXE
    npm: 8.3.0 - C:\nodejs\npm.CMD
  Browsers:
    Chrome: 121.0.6167.139
    Edge: Chromium (121.0.2277.83)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    @clerk/nextjs: ^4.29.5 => 4.29.5
    @types/node: ^20 => 20.11.16
    @types/react: ^18 => 18.2.51
    @types/react-dom: ^18 => 18.2.18
    autoprefixer: ^10.0.1 => 10.4.17
    eslint: ^8 => 8.56.0
    eslint-config-next: 14.1.0 => 14.1.0
    next: 14.1.0 => 14.1.0
    postcss: ^8 => 8.4.33
    react: ^18 => 18.2.0
    react-dom: ^18 => 18.2.0
    tailwindcss: ^3.3.0 => 3.4.1
    typescript: ^5 => 5.3.3

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions