Next.js Discord

Discord Forum

getURL() function that I always used stopped working since Next.js 16

Answered
Masai Lion posted this in #help-forum
Open in Discord
Masai LionOP
I have a getURL() function that always used to work in prod and locally, that I would pass to robots.ts, sitemap.ts and things like better-auth and Polar.sh but since upgrading to Next.js 16 few hours ago I noticed that both on Vercel and self hosted apps the resolved URL is always http://localhost:3000 and the env variables get skipped on all of those mentioned prod environments:
export const getURL = () => {
  let url =
    process?.env?.NEXT_PUBLIC_VERCEL_PROJECT_PRODUCTION_URL ?? // Automatically set by Vercel.
    process?.env?.NEXT_PUBLIC_VERCEL_BRANCH_URL ?? // Automatically set by Vercel.
    process?.env?.NEXT_PUBLIC_VERCEL_URL ?? // Automatically set by Vercel.
    'http://localhost:3000';
  // Make sure to include `https://` when not localhost.
  url = url.includes('http') ? url : `https://${url}`;
  // Make sure to exclude trailing `/`.
  url = url.charAt(url.length - 1) === '/' ? url.slice(0, -1) : url;
  return url;
};

I went over the changelogs and can't figure out what changed that would cause this to stop working in all of the places in the codebase.
Answered by Masai Lion
Looks like it is a turbopack bug/issue, using webpack via next build --webpack solved it on Vercel and selfhosted.
View full answer

1 Reply

Masai LionOP
Looks like it is a turbopack bug/issue, using webpack via next build --webpack solved it on Vercel and selfhosted.
Answer