Issue with BetterFetch on vps

When running the app on vercel everything seems to work But as soon as i switch over to my own vps i keep getting BetterFetch issues it seems..
Error fetching session: Error: fetch failed
at context.fetch (/app/node_modules/next/dist/server/web/sandbox/context.js:322:60)
at tN (/app/.next/server/src/middleware.js:13:45496)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async tB (/app/.next/server/src/middleware.js:13:50025)
at async tW (/app/.next/server/src/middleware.js:13:51099)
at async handler (/app/.next/server/src/middleware.js:13:51668)
at async /app/.next/server/src/middleware.js:13:37403
at async ti (/app/.next/server/src/middleware.js:13:33929)
at async /app/node_modules/next/dist/server/web/sandbox/sandbox.js:108:26
at async runWithTaggedErrors (/app/node_modules/next/dist/server/web/sandbox/sandbox.js:105:9) {
}
Error fetching session: Error: fetch failed
at context.fetch (/app/node_modules/next/dist/server/web/sandbox/context.js:322:60)
at tN (/app/.next/server/src/middleware.js:13:45496)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async tB (/app/.next/server/src/middleware.js:13:50025)
at async tW (/app/.next/server/src/middleware.js:13:51099)
at async handler (/app/.next/server/src/middleware.js:13:51668)
at async /app/.next/server/src/middleware.js:13:37403
at async ti (/app/.next/server/src/middleware.js:13:33929)
at async /app/node_modules/next/dist/server/web/sandbox/sandbox.js:108:26
at async runWithTaggedErrors (/app/node_modules/next/dist/server/web/sandbox/sandbox.js:105:9) {
}

if (!pathname.startsWith("/api")) {
try {
const { data } = await betterFetch<Session>("/api/auth/get-session", {
baseURL: req.nextUrl.origin,
headers: {
cookie: req.headers.get("cookie") || "",
},
});
session = data;
} catch (error) {
console.error("Error fetching session:", error);
}
}

if (!pathname.startsWith("/api")) {
try {
const { data } = await betterFetch<Session>("/api/auth/get-session", {
baseURL: req.nextUrl.origin,
headers: {
cookie: req.headers.get("cookie") || "",
},
});
session = data;
} catch (error) {
console.error("Error fetching session:", error);
}
}
I got no clue what the reason could be, any thoughts?
5 Replies
Erro Senn
Erro SennOP•3mo ago
Got any idea what might cause this issue @Ping @bekacru ?
Ping
Ping•3mo ago
I wonder if it could be an environment issue. Is your vps running on Node?
Erro Senn
Erro SennOP•3mo ago
yesss @Ping @bekacru Any other clues?
Ping
Ping•3mo ago
If you make a normal fetch() just to test, does it work?
Erro Senn
Erro SennOP•3mo ago
damn... doesn't work either! @Ping I'm sorry for bothering, I figured it out 😭 I feel so dumb but i was using req.nextUrl.origin and i just had to replace it to my baseurl... since im selfhosting it

Did you find this page helpful?