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

importing server actions which use cookies or headers from client components causes Error: Invariant: Method expects to have requestAsyncStorage, none available #49410

Closed
1 task done
Fredkiss3 opened this issue May 7, 2023 · 3 comments
Labels
bug Issue was opened via the bug report template.

Comments

@Fredkiss3
Copy link
Contributor

Fredkiss3 commented May 7, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.5.0: Tue Apr 25 04:07:22 PDT 2023; root:xnu-8796.121.2~8/RELEASE_ARM64_T8103
    Binaries:
      Node: 18.15.0
      npm: 9.5.0
      Yarn: 1.22.18
      pnpm: 7.1.0
    Relevant packages:
      next: 13.4.1
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

App directory (appDir: true)

Link to the code that reproduces this issue

https://github.com/Fredkiss3/next-bug-server-actions-headers-cookies

To Reproduce

Run the app and click on one of the buttons in the forms

Describe the Bug

When importing server actions which use cookies or headers from a client component, when you try to call these functions you get an error :

- error Error: Invariant: Method expects to have requestAsyncStorage, none available
    at getCookie (./app/actions.ts:11:65)
null

It seems like next considers headers and cookies as client components ?

this error only happens if those actions are imported from a "use server" file into client components, if you instead pass those actions from a server component to a client component as props, everything works fine, but only if there is not any other client component who try to import the same server actions.

If you go to app/page.tsx and comment line 8, everything works normally :

import { getCookie, getHeader } from "./actions";
import { CookieGetter } from "./cookie-getter";
import { CookieGetterActionPassedDown } from "./cookie-getter-actions-passed-down";

export default function Home() {
  return (
    <main>
      {/* <CookieGetter /> */}
      <CookieGetterActionPassedDown
        getCookieAction={getCookie}
        getHeaderAction={getHeader}
      />
    </main>
  );
}

Expected Behavior

Importing server actions which use headers or cookies from a client component should not cause errors

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@Fredkiss3 Fredkiss3 added the bug Issue was opened via the bug report template. label May 7, 2023
@borispoehland
Copy link

I have the same, duplicate #49235

@Fredkiss3
Copy link
Contributor Author

Fredkiss3 commented May 7, 2023

closing in favor of #49235

@github-actions
Copy link
Contributor

github-actions bot commented Jun 7, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

2 participants