NextJS从SSR到SSG重构API路线
我有一个SSR API,我希望将其转换为SSG在构建时间生成
/pages/api/dostuff/[CID] .TSX
export default async function handler(req, res) {
const { cid } = req.query;
let data;
data = await doStuff(cid);
res.status(200).json(data)
}
我的方法是开始实现 getstatic 如下:
export const getStaticPaths: GetStaticPaths = async () => {
return {
paths: [
{ params: {"cid": "AAA"} },
{ params: {"cid": "BBB"} },
],
fallback: false // false or 'blocking'
};
}
但是我停下来,因为我找不到实现 getstaticprops 的可行选择,以便拥有SSG。
我本来会像遵循一样继续进行,
export async function getStaticProps(context) {
const { cid } = context.params.cid;
let data;
data = await doStuff(cid);
return { props: { data } };
}
我应该创建一个仅输出普通JSON的组件吗? 这对我来说并不正确。最好的方法是什么?
I have a SSR API that I want convert to SSG to generate it at build time
/pages/api/dostuff/[cid].tsx
export default async function handler(req, res) {
const { cid } = req.query;
let data;
data = await doStuff(cid);
res.status(200).json(data)
}
My approach has been to start implementing getStaticPaths as follows:
export const getStaticPaths: GetStaticPaths = async () => {
return {
paths: [
{ params: {"cid": "AAA"} },
{ params: {"cid": "BBB"} },
],
fallback: false // false or 'blocking'
};
}
But than I stopped because I couldn't find a viable option of implementing getStaticProps in order to have SSG.
I would have proceeded like follow
export async function getStaticProps(context) {
const { cid } = context.params.cid;
let data;
data = await doStuff(cid);
return { props: { data } };
}
Should I create a component that just outputs plain JSON?
This doesn't look right to me; what's the best approach ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论