How to setup api routes urls for production? #48793
-
SummaryHello everyone, app builds without problem locally, but when uploading to Vercel the build fails with
I added a dynamic base url like this process.env.NODE_ENV === "production" ? "" : "http://localhost:3000 but problem continues. What the base url should be for production? Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
This sounds like you are trying to reach to an From the docs:
Although that was written for GetServerSideProps, it applies to Server Components too.
For most developers, since
However, on a Node.js environment, there's no document (and no baseURL), since a server is typically agnostic to their external identity. The real solution is to, while on the server, just fetch whatever data you need from the source, and skip doing For example, I found this on your GitHub account: https://github.com/thanosoncode/next-shop/blob/efed3362d0f60c9256a36f04b9f533df64fc86fc/app/products/page.tsx#L7-L18 const getProducts = async () => {
const response = await fetch(`${baseUrl}/api/products`);
if (!response.ok) {
throw new Error("error fetching products");
}
return response.json();
};
const ProductsPage = async () => {
const products = await getProducts();
return ( That's the kind of issue I am talking about. You don't need to call You should do this instead: import { getAllProducts } from "@/lib/products"
import Container from "../components/Container";
import FooterCTA from "../components/FooterCTA";
import ProductsHeader from "./components/ProductsHeader";
import ProductList from "./components/ProductList";
const ProductsPage = async () => {
const products = await getAllProducts();
return ( As simple as that. The whole point of Server Side Rendering, is that you can just get the data you need, on spot, no need for extra fuzz. |
Beta Was this translation helpful? Give feedback.
This sounds like you are trying to reach to an
api
route from within the server. That's a bad idea, it means, even if you fixed it, you'll do an extra jump out to the internet and back to your server.From the docs:
Although that was written for GetServerSideProps, it applies to Server Components too.