Skip to content

[NEXT-644] cookies broke on 13.2.1 Invariant: Method expects to have requestAsyncStorage, none availableΒ #46356

Closed

Description

@cipriancaba

Verify canary release

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

Provide environment information

Operating System:
Platform: linux
Arch: x64
Version: Ubuntu 20.04.0 LTS Fri Feb 24 2023 12:23:18 GMT+0200 (Eastern European Standard Time)
Binaries:
Node: 16.14.2
npm: 7.17.0
Yarn: 1.22.19
pnpm: 7.13.6
Relevant packages:
next: 13.2.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://stackblitz.com/edit/vercel-next-js-gqeapg?file=package.json,app%2F[variant]%2F(shop)%2Fp%2F[[...id]]%2Fpage.tsx,next.config.js

To Reproduce

Try to import cookies from next/headers
Try to access a dynamic page, eg /test/p

Describe the Bug

Error: Invariant: Method expects to have requestAsyncStorage, none available

This happens if I do an import of cookies

import { cookies } from 'next/headers';

const DynamicPage = async () => {
  const nextCookies = cookies();
  console.log(nextCookies.get('random'));
  const data = await loadRandomData();

  return <div>{data.title}</div>;
};

Screenshot 2023-02-24 at 12 26 31

Expected Behavior

I can access the cookies object on a dynamic page

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

NEXT-644

Activity

  1. added
    bugIssue was opened via the bug report template.
    on Feb 24, 2023
  2. changed the title [-]cookies broke on 13.2.1 Invariant: Method expects to have requestAsyncStorage, none available[/-] [+][NEXT-644] cookies broke on 13.2.1 Invariant: Method expects to have requestAsyncStorage, none available[/+] on Feb 27, 2023
  3. ayhanap commented on Mar 1, 2023

    @ayhanap

    Not sure if its related but happens when I access cookies() outside of an async function.

    import { cookies } from 'next/headers';
    
    //gives same error when used here
    const hello = cookies().get('access_token')?.value;
    
    export default async function FeedbackPage(props: FeedbackPageProps) {
    
    //works ok inside here
    ....
    ...
    }
    
  4. wyattjoh commented on Mar 6, 2023

    @wyattjoh
    Contributor

    In order to access cookies(), it must be done inside a component or a route, you can't access it from the global scope @ayhanap.

  5. added a commit that references this issue on Mar 21, 2023
    c14c83a
  6. frankimhof commented on Mar 31, 2023

    @frankimhof

    I also got this error, but was able to fix it.

    I am using experimental App directory (appDir: true).

    I got the error when calling getServerSession(authOptions) (imported from next-auth)
    inside of an API Route handler function.

    Fixed it by also providing req and res (from the API Route handler) as arguments to getServerSession:
    getServerSession(req, res, authOptions)

  7. jgb-solutions commented on Apr 12, 2023

    @jgb-solutions

    @frankimhof hey thank you! This helps me get tRPC working with next 13 app dir. <3 I was having a hard time getting the session for the context.

  8. talbertherndon commented on Apr 13, 2023

    @talbertherndon

    I also got this error, but was able to fix it.

    I am using experimental App directory (appDir: true).

    I got the error when calling getServerSession(authOptions) (imported from next-auth) inside of an API Route handler function.

    Fixed it by also providing req and res (from the API Route handler) as arguments to getServerSession: getServerSession(req, res, authOptions)

    @frankimhof Where exactly is this file and how are you calling user session?

  9. jgb-solutions commented on Apr 13, 2023

    @jgb-solutions

    @talbertherndon import { ServerResponse } from "http"

  10. VyMajoris commented on Apr 15, 2023

    @VyMajoris

    I also got this error, but was able to fix it.

    I am using experimental App directory (appDir: true).

    I got the error when calling getServerSession(authOptions) (imported from next-auth) inside of an API Route handler function.

    Fixed it by also providing req and res (from the API Route handler) as arguments to getServerSession: getServerSession(req, res, authOptions)

    This would not work for me because I'm using getServerSession inside a multer fileFilter function, I don't have access to the response there. At least not that I'm aware.

  11. jgb-solutions commented on Apr 15, 2023

    @jgb-solutions

    You get the request out of the context passed to getServersideProps' context.

  12. 12 remaining items

  13. SvemirskiHod commented on May 9, 2023

    @SvemirskiHod

    Having the same issue with server actions on 13.4.1 with next-auth's getServerSession as well as with cookies and headers from "next/headers"

  14. vitalyliber commented on May 9, 2023

    @vitalyliber

    @lk9100 @salvinoto @SvemirskiHod just upgrade NextJS to the v13.4.2-canary.3 version.

  15. lk9100 commented on May 9, 2023

    @lk9100

    @lk9100 @salvinoto @SvemirskiHod just upgrade NextJS to the v13.4.2-canary.3 version.

    @vitalyliber

    have just installed v13.4.2-canary.3
    same error

    Screenshot 2023-05-09 at 20 54 26 Screenshot 2023-05-09 at 20 50 55
  16. tmns commented on May 10, 2023

    @tmns

    ^^ Also on 13.4.2-canary.3 and getting the same error when invoking getServerSession from a server action.

  17. hgogoi2012 commented on May 11, 2023

    @hgogoi2012

    I am also getting the same error.

  18. timneutkens commented on May 11, 2023

    @timneutkens
    Contributor

    Hey, could you open a new issue with a reproduction so that we can have a look?

  19. lk9100 commented on May 11, 2023

    @lk9100

    i can't reproduce this anymore after a clean npm install with latest canary.

    @timneutkens thanks for the heads up πŸ‘

  20. lukevers commented on May 11, 2023

    @lukevers

    Hey @timneutkens, let me see if I can get a reproduction for you, but I just got this same error while testing transitioning a server action passed down from the page to a client component to having the client component have direct access to it. I just upgraded my dependencies and see this exact issue.

  21. lukevers commented on May 11, 2023

    @lukevers

    @timneutkens https://github.com/lukevers/next-644

    OK so I initially had cookies().get('anything'); which did cause issues, but not on 13.4.2-canary.5. I do still get these errors, but not with cookies directly, I get them when I'm using getServerSession from import { getServerSession } from 'next-auth/next'; -- I'm not sure if this is a nextjs issue or a next-auth issue since cookies directly seem to work OK.

    I have two pages that define two situations:

    • /issue1 - "individual server components" - The main issue here is that when we access cookies through a server component imported from a client component, it does not work. It works if we import it in a server component (page layout here) and pass it to the client component as a prop.
    • /issue2 - "shared server component" - The issue here is that when we access cookies through a server component imported from a client component, it does not work. It also does not work even if it was passed through to the client component if the server component is the same component
  22. lukevers commented on May 11, 2023

    @lukevers

    @timneutkens I came to the conclusion this is related to next-auth after my related closed issue (here), but I wanted to debug it more so I copied the code locally and when I use the same code (but not imported from the library), grabbing my session information works.

    import { getServerSession } from 'next-auth/next';
    import { __internal__unstable__getSession } from './_internal_session';
    
    // uses from next-auth
    export async function getSession() {
      return await getServerSession({});
    }
    
    // uses a copied version locally
    export async function __unstable__getSession() {
      return await __internal__unstable__getSession({});
    }

    Is there anyway this could somehow be related to the bundler?

    lukevers/next-644@ca99b9e

    On /local in that repo, both of the buttons work now. The difference here between this one and the previous one is:

    local version:

    'use server';
    
    import { __unstable__getSession } from "@/util/session";
    
    export default async function ServerExample(message: string) {
      return await __unstable__getSession();
    }

    using the library's function:

    'use server';
    
    import { getSession } from '@/util/session';
    
    export default async function ServerExample(message: string) {
      return await getSession();
    }
  23. ammarrik commented on May 13, 2023

    @ammarrik

    i only get this error when any of the server actions from a single file is imported inside client component

  24. Hiieu commented on May 20, 2023

    @Hiieu

    Can we re-open the issue? This error is still happening in v13.4.4-canary.0.

  25. Hiieu commented on May 21, 2023

    @Hiieu

    What I did to solve the problem is replacing the entire cookies implementation from NextJS with the cookies-next library.

  26. timneutkens commented on May 22, 2023

    @timneutkens
    Contributor

    Please see my earlier comment, we can investigate if you provide a reproduction in a new issue, instead you're posting additional comments here without a reproduction 😒 Thank you very much πŸ™
    #46356 (comment)

  27. locked as off topic and limited conversation to collaborators on May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue was opened via the bug report template.linear: nextConfirmed issue that is tracked by the Next.js team.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions