0

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 ?

Stormsson
  • 1,391
  • 2
  • 16
  • 29
  • I'm not sure I understand the question. Why are you trying to use `getStaticProps`/`getStaticPaths` for an API route? Those are used to generate static pages. What's the issue with the current API route? – juliomalves Jun 02 '22 at 16:09
  • I want to generate a static API, if that makes sense: I dont't want it to be called each time a request arrives and misses the cache. I'd like to have it called only once at build time. I could probably just make a script and fetch it at build time, but I hoped that the same approach for pages could be used for APIs too – Stormsson Jun 03 '22 at 06:41
  • _"I could probably just make a script and fetch it at build time"_ - I believe that's what you should do. – juliomalves Jun 03 '22 at 11:17

0 Answers0