next.js icon indicating copy to clipboard operation
next.js copied to clipboard

AsyncLocalStorage not passed within thenables when deployed to Vercel Edge runtime (upstream limitation with Cloudflare Workers)

Open andyjy opened this issue 1 year ago • 1 comments

Verify canary release

  • [X] 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: Thu Jun  8 22:22:19 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T8103
    Binaries:
      Node: 18.16.0
      npm: 9.5.1
      Yarn: 1.22.19
      pnpm: N/A
    Relevant Packages:
      next: 13.4.8
      eslint-config-next: 13.4.8
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.6
    Next.js Config:
      output: N/A

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

Middleware / Edge (API routes, runtime)

Link to the code that reproduces this issue or a replay of the bug

https://github.com/andyjy/nextjs-tests/tree/als-thenables-edge

To Reproduce

Reproduction repo: https://github.com/andyjy/nextjs-tests/tree/als-thenables-edge

Describe the Bug

Passing async context via AsyncLocalStorage.run() and then attempting to read it via AsyncLocalStorage.getStore() fails when deployed to Cloudflare Workers / thus also the Vercel Edge runtime if the call to getStore() is nested within a thenable (that isn't a native Promise).

The underlying issue is a limitation in the CloudFlare Workers implementation of support for AsyncLocalStorage: https://github.com/cloudflare/workerd/issues/870

My assumption is this will require an upstream fix by Cloudflare Workers and/or V8 - but worth knowing / documenting / tracking for Vercel Edge (this is where I looked first before tracing the issue upstream to CF workers).

Expected Behavior

AsyncLocalStorage context should be retrieved within a thenable when deployed to Vercel Edge functions, in the same way as works under Node.js and when emulating the Edge runtime locally under next dev.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

andyjy avatar Jul 17 '23 10:07 andyjy