Skip to content

Commit

Permalink
feat: add cache header to spotify and newsletter
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorusclarence committed Nov 25, 2022
1 parent 2f26272 commit 292dc43
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/pages/api/newsletter/count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ export default function count(req: NextApiRequest, res: NextApiResponse) {
)
.then((response) => {
const count = response.data.length;
res.status(200).json({ count });

// Cache for an hour
res.setHeader(
'Cache-Control',
'public, s-maxage=3600, stale-while-revalidate=60'
);
return res.status(200).json({ count });
})
.catch(() => res.status(500).json({ error: 'Something was wrong' }));
} else {
Expand Down
11 changes: 10 additions & 1 deletion src/pages/api/spotify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ export default async function spotify(
response.status > 400 ||
response.data.currently_playing_type !== 'track'
) {
//? s-maxage=180 because song usually lasts 3 minutes
res.setHeader(
'Cache-Control',
'public, s-maxage=180, stale-while-revalidate=90'
);
return res.status(200).json({ isPlaying: false });
}

Expand All @@ -82,6 +87,10 @@ export default async function spotify(
songUrl: response.data.item.external_urls.spotify,
};

res.status(200).json(data);
res.setHeader(
'Cache-Control',
'public, s-maxage=180, stale-while-revalidate=90'
);
return res.status(200).json(data);
}
}

1 comment on commit 292dc43

@vercel
Copy link

@vercel vercel bot commented on 292dc43 Nov 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.