Deployed Next.js App Adds Extra Path to Backend API URL #81958
Replies: 1 comment 4 replies
-
This issue often happens because of how the API base URL is constructed or accessed in your frontend code. The key problem is that in production, the base URL for your backend API is either undefined or incorrectly resolved, causing URLs like:
instead of the correct backend URL. Why does it happen? Relative URL usage: If you use relative paths for API calls (e.g., /api/users/login) without a proper absolute base URL, the browser will prepend the frontend origin, causing the frontend URL to be used instead of the backend. How to fix Make sure you define your backend API URL in .env.local and in Vercel environment variables like this:
Reference the API URL correctly in frontend code:
Check Vercel environment variables: Ensure that NEXT_PUBLIC_API_URL is set in your Vercel project dashboard (under Settings > Environment Variables) for both production and preview environments. Avoid hardcoding or relying on server-only env vars: Variables without NEXT_PUBLIC_ prefix are only accessible in server-side code, not in client bundles. Additional debugging tips Test API calls in production browser DevTools network tab to verify URLs. If you use custom hooks or wrappers for API calls, check they correctly use the base URL. If you apply these, your deployed frontend should correctly call the backend API without extra or undefined paths. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
I'm facing an issue after deploying my Next.js frontend to Vercel. When I try to send data to my deployed backend, an extra path gets added to the API URL automatically. However, the same code works perfectly on my local machine (both frontend and backend).


Additional information
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions