aymericzip / aymericzip/intlayer

Intlayer + NextJS custom server does not work. (AsyncLocalStorage issues)

Open
#318 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
TypeScript
Stars
833
Forks
126
Avg merge
1d 3h
Merged PRs (30d)
7

Description

I figured I'd post this here, incase anyone else has the same trouble I had.

I am using Next with a custom server. As soon as I added Intlayers to the project Next didn't want to start.

c:\repos\project\node_modules\next\dist\server\app-render\async-local-storage.js:44 throw sharedAsyncLocalStorageNotAvailableError; ^ Error: Invariant: AsyncLocalStorage accessed in runtime where it is not available at Object.<anonymous>

I found a solution through this comment for turborepo .. though unrelated .. I don't even use that package.
https://github.com/vercel/turborepo/issues/3319#issuecomment-2733804760

And then started my server somewhat like this.

const { asyncLocalStorage } = require('./asyncStorage.mjs');
import { createServer } from 'node:http';
import express from 'express';
const app = next({ dev, conf: nextConfig, customServer: true });
const handler = nextApp.getRequestHandler();

nextApp.prepare().then(() => {
  const app = express();

  app.all('*', (req, res) => {
    return asyncLocalStorage.run(new Map(), () => {
      return handler(req, res);
    });
  });

  const httpServer = createServer(app);

  httpServer
    .once('error', (err) => {
      console.error(err);
      process.exit(1);
    })
    .listen(3000);
});

Maybe you can fix it so this is unnecessary .. either way I hope this solution helps someone.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the startup failure with a Next.js custom server using Intlayer, then inspect the asyncStorage.mjs workaround and the custom server setup shown in the issue. Done means the application starts and handles requests without requiring the manual AsyncLocalStorage wrapper.

Written by the indexing model from the issue text.

Assessment

Tech stack
express, nextjs, node.js, typescript
Domain
backend, internationalization, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.