Skip to content

Instantly share code, notes, and snippets.

@carvalhochris
Created March 30, 2024 18:24
Show Gist options
  • Select an option

  • Save carvalhochris/a4ecd589731f40f3e1b640b71b0b5582 to your computer and use it in GitHub Desktop.

Select an option

Save carvalhochris/a4ecd589731f40f3e1b640b71b0b5582 to your computer and use it in GitHub Desktop.
add cache control headers to dynamic page (Next.js pages router)
export const getServerSideProps = (async (context) => {
const id = context.query.id;
const res = await fetch(`https://api.../posts/${id}/`, {
...
});
context.res.setHeader(
"Cache-Control",
"public, s-maxage=604800, stale-while-revalidate=59"
);
const post = await res.json();
return { props: { post } };
}) satisfies GetServerSideProps<{
post: Post;
}>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment