pvman
pvman
Explore posts from servers
TtRPC
Created by pvman on 4/11/2024 in #❓-help
> Error: Invariant: headers() expects to have requestAsyncStorage, none available
Hi, I use trpc v1045.1 in next 14.1 app router. I call it server side and have the following.
export async function createContext(opts: CreateNextContextOptions) {
const { session, user } = await getUserAuth();

return {
db,
session,
user,
...opts,
};
};

export const createServerApi = cache(async () => {
const heads = new Headers(headers());
heads.set("x-trpc-source", "rsc");

const context = await createTRPCContext({
headers: heads,
});

return createCaller(context);
});

export const api = await createServerApi();
export async function createContext(opts: CreateNextContextOptions) {
const { session, user } = await getUserAuth();

return {
db,
session,
user,
...opts,
};
};

export const createServerApi = cache(async () => {
const heads = new Headers(headers());
heads.set("x-trpc-source", "rsc");

const context = await createTRPCContext({
headers: heads,
});

return createCaller(context);
});

export const api = await createServerApi();
It is working on a static route but on a dynamic route /edit/[slug] I get the following error (coming from the headers() in createServerApi):
Error: Invariant: headers() expects to have requestAsyncStorage, none available
Any idea how I could solve this?
8 replies