Why should I add url for api route in nextjs #68026
Replies: 1 comment
-
|
You could never do that in a serve side context, even in Pages Router. The reason being, that the server is, let's say, agnostic, to the URL where the application is actually deployed. In the client it works, because fetch requests with relative URLs are assumed to be, relative to the page document. In a server environment, say Node.js, such assumption can't really be made. We might be in a build step, or as part of a sub task in another process, and not all data about the original request is likely to be available, etc etc. In your case, you have the wrong abstraction level. Your data fetching work, should be the thing that's common and to the serve interfaces. This really catches devs off guard. I personally don't see the point in creating an endpoint, so that your server can reach into it. It is an unnecessary round trip, if you were able to make it happen. The Discord community has written about this here: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I don't remember if it used to be, but I could do this very easily in nextjs, if I had my own local api route, I could easily pull it with fetch /api/*, but now I can't run it without specifying the url, why?
Beta Was this translation helpful? Give feedback.
All reactions