Does Nextjs automatically set baseUrl to host from a server component?
Answered
Large-billed Tern posted this in #help-forum
Large-billed TernOP
In the below code, will Next automaticall append "https://mywebsite" to the request? or it will fire blank "/users"?
export default async function UserDetails() {
const response = await fetch('/users');
const user = await response.json();
return (
<div>
<h1>{user.name}</h1>
</div>
);
}
Answered by Asian black bear
Don’t fetch your own API routes from server-side code: https://nextjs-faq.com/fetch-api-in-rsc
And to actually answer the question since I forgot to be explicit: no, server-side code has no concept of a base URL.
And to actually answer the question since I forgot to be explicit: no, server-side code has no concept of a base URL.
3 Replies
Asian black bear
Don’t fetch your own API routes from server-side code: https://nextjs-faq.com/fetch-api-in-rsc
And to actually answer the question since I forgot to be explicit: no, server-side code has no concept of a base URL.
And to actually answer the question since I forgot to be explicit: no, server-side code has no concept of a base URL.
Answer
Large-billed TernOP
Nicely written, super detailed. Thanks.
Large-billed TernOP
The beauty about this response is that it has helped me rethink my design. Not just for this app but going forward. 🫡