【发布时间】:2021-04-08 00:57:30
【问题描述】:
下面是我的函数,其中我使用 getServerSideProps() 方法并根据我的 URL 中的参数 post_slug 动态获取数据。
// This gets called on every request
export async function getServerSideProps({ query }) {
const { post_slug } = query || {};
let url = API_URLS.POST.GET_POST_BY_SLUG;
url = url.replace(/#POST_SLUG#/g, post_slug);
const res = await fetch(url);
const { data } = (await res.json()) || {};
// Pass post_data to the page via props
return { props: { data } };
}
但是这个函数在每次请求时都会渲染一个完整的页面。
所以现在我决定使用 getStaticProps() 但在这里我无法从 URL 获取参数 post_slug。
我阅读了next js 文档,他们告诉我需要使用 getStaticPaths() 和 getStaticProps() 方法,但这里的问题是我必须在 getStaticPaths() 方法中定义静态参数,但我想要来自当前 URL 的参数。
有没有办法在getStaticProps()方法中从URL获取param post_slug?
【问题讨论】: