From 8e76b7efee553b6be87e1cbf2ffb9276bd36e527 Mon Sep 17 00:00:00 2001 From: Brian Lovin Date: Fri, 6 Dec 2019 21:17:41 -0500 Subject: [PATCH] Cache networked pages for 12 hours --- src/pages/index.tsx | 6 +++++- src/pages/overthought/[slug].tsx | 7 ++++++- src/pages/overthought/index.tsx | 6 +++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 3e824f563..9d5774f8e 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -87,7 +87,11 @@ function Home(props: Props) { ); } -Home.getInitialProps = async () => { +Home.getInitialProps = async ({ res }) => { + if (res) { + const cacheAge = 60 * 60 * 12; + res.setHeader('Cache-Control', `public,s-maxage=${cacheAge}`); + } const posts = await getPosts(); return { posts: posts } } diff --git a/src/pages/overthought/[slug].tsx b/src/pages/overthought/[slug].tsx index be50e4ce7..942ad7a24 100644 --- a/src/pages/overthought/[slug].tsx +++ b/src/pages/overthought/[slug].tsx @@ -112,7 +112,12 @@ export function OverthoughtPost(props: Props) { ) } -OverthoughtPost.getInitialProps = async ({ query }) => { +OverthoughtPost.getInitialProps = async ({ query, res }) => { + if (res) { + const cacheAge = 60 * 60 * 12; + res.setHeader('Cache-Control', `public,s-maxage=${cacheAge}`); + } + const post = await getPostBySlug(query.slug); return { post: post, slug: query.slug } } diff --git a/src/pages/overthought/index.tsx b/src/pages/overthought/index.tsx index 172eb17f1..70b7fffc7 100644 --- a/src/pages/overthought/index.tsx +++ b/src/pages/overthought/index.tsx @@ -58,7 +58,11 @@ function Overthought({ posts }: Props) { ); } -Overthought.getInitialProps = async () => { +Overthought.getInitialProps = async ({ res }) => { + if (res) { + const cacheAge = 60 * 60 * 12; + res.setHeader('Cache-Control', `public,s-maxage=${cacheAge}`); + } const posts = await getPosts(); return { posts: posts } }