Next.js Discord

Discord Forum

Fetch request url trouble

Answered
Capple posted this in #help-forum
Open in Discord
Hello,

I was previously using Axios in my previous Nextjs13 projects, but I'm trying to migrate to fetch.

With Axios I could just do this and send a request to /products but I'm unable to get the same functionality with fetch.

const axiosConfig = {
  baseURL: `/api`,
  timeout: 10000,
  withCredentials: true,
  headers: {
    "Content-Type": "application/json",
  },
};

Right now I'm currently using this, but this doesn't work when deployed to Vercel due to obvious reasons and I can't seem to just fetch /api/products either. Would I need to use something like ${process.env.NEXT_PUBLIC_API_URL}/api/products or is there a better way I'm missing?
export default async function Home() {
    const res = await fetch('http://localhost:3000/api/products');
    const products = await res.json();
}
Answered by Giant panda
Don't fetch your own endpoints from server-side code: https://nextjs-faq.com/fetch-api-in-rsc
View full answer

2 Replies

bump
Giant panda
Don't fetch your own endpoints from server-side code: https://nextjs-faq.com/fetch-api-in-rsc
Answer