Skip to content

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

@BjornHMS

Description

@BjornHMS

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.
vercel/turborepo#3319 (comment)

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions