Hostname for fetch in serverside component with NextJS13 on Vercel

huangapple go评论59阅读模式
英文:

Hostname for fetch in serverside component with NextJS13 on Vercel

问题

When using fetch in a server-side component in Next.js 13, the code is written as follows:

fetch("http://localhost:3000/api/test")

此端点(例如,http..../test)根据部署环境而异。例如,此示例在本地部署,但在部署到某个分支时,主机名应该类似于 your-pj-and-git-branch.verce.app

因此,问题在于根据部署环境的不同,上述代码应该进行重写。

如何轻松切换 fetch 的主机?

我希望 Vercel 有默认的环境变量,其中包含有关部署环境或主机名的信息,但似乎它没有这样的功能,所以我现在陷入困境。

谢谢!

英文:

When using fetch in an server side component on NextJS13, code is written as follows:

fetch("http://localhost:3000/api/test")

This endpoint (e.g., http..../test) varies on deployed environment. For example, this example is deployed on locally, but when deployed to certain branch, the host name should be something like your-pj-and-git-branch.verce.app.

So, the problem is that the code above should be rewritten depending on which environment it is deployed.

How can I switch the host for fetch easily?

I wish vercel has default env var which has information on deployed environment or hostname but it seems that it does not have such a functionality so I am now stuck.

Thanks!

答案1

得分: 1

我可以获取此环境变量(VERCL_URL)。文档在这里:

https://vercel.com/docs/concepts/projects/environment-variables/system-environment-variables

英文:

I could get env var for this ( VERCL_URL ). The document is here:

https://vercel.com/docs/concepts/projects/environment-variables/system-environment-variables

huangapple
  • 本文由 发表于 2023年4月17日 19:01:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定