英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论