Question from the Next.js test

How to use the useSWR hook in Next.js to fetch data from an API?

Archived

Take a look at this Route Handler:

// app/items/route.ts
export async function GET(){
  const res= await fetch('https://data.mongodb-api.com/...');
  const data= await res.json();

  return Response.json({ data },{ next:{ revalidate: 60 } });
}

How is cached data revalidated?

Author: AnasStatus: Archived(New question!)Question passed 339 times
5
Community Evaluations
developer avatar
Incorrect answer
Marcin
12/08/2024
`next:{ revalidate: 60 }` should be added to the `fetch` handler, not the response. https://nextjs.org/docs/app/api-reference/functions/fetch