hafffe
hafffe10mo ago

Dynamic links url dependent on next headers?

Hi, We're currently explore if we can use trpc on a multi tenant next.js 13 application. But we can not find a satisfying answer how to set the url for the links used in createTRPCProxyClient Our thinking is that we use headers from next to get the hostname. Here is an non working example just give you an idea what we want to archive
const getUrl = () => {
const headersList = headers();
const domain = headersList.get("host");


return `http://${domain}/api/trpc`;
};

export const api = createTRPCProxyClient<AppRouter>({
transformer,
links: [
unstable_httpBatchStreamLink({
url: getUrl(),
headers() {
const heads = new Map(headers());
heads.set("x-trpc-source", "server");

return Object.fromEntries(heads);
},
}),
],
});
const getUrl = () => {
const headersList = headers();
const domain = headersList.get("host");


return `http://${domain}/api/trpc`;
};

export const api = createTRPCProxyClient<AppRouter>({
transformer,
links: [
unstable_httpBatchStreamLink({
url: getUrl(),
headers() {
const heads = new Map(headers());
heads.set("x-trpc-source", "server");

return Object.fromEntries(heads);
},
}),
],
});
But it's ending up with the error "Error: Invariant: headers() expects to have requestAsyncStorage, none available." Any ideas how to solve this would be much appreciated
0 Replies
No replies yetBe the first to reply to this messageJoin