【发布时间】:2020-06-12 03:39:14
【问题描述】:
这几乎直接来自 Next.js 网站上的一些示例。
function HomePage(props) {
return (
<div>
<p>Welcome to {props.data.name} Next.js!</p>
<img src="/conexon-logo-white.png" alt="my image" />
</div>
);
}
// This gets called on every request
export async function getServerSideProps(context) {
const res = await fetch('http://localhost:3000/api/test');
const data = await res.json();
// Pass data to the page via props
return { props: { data } };
}
export default HomePage;
该代码当然在本地工作得很好。但是如何根据站点的运行位置将http://localhost:3000 替换为“动态”的东西?该站点可以在本地运行 (localhost)、由 Vercel 创建的预览 URL (something-random.now.sh)、另一个 Vercel URL (something-else.now.sh) 或自定义 URL (mydomain.com)。有没有办法确定如何在所有没有.env 文件的人上调用 Next.js /api?使用.env 文件,我不知道如何为 Vercel 为预览创建的“动态”URL(和其他 Vercel URL)设置它。
谢谢!
【问题讨论】:
-
你为什么不使用
next.config.js文件?
标签: javascript reactjs next.js