From 64303400a4b68ce575602e3093906903c29048e4 Mon Sep 17 00:00:00 2001 From: Jake Jarvis Date: Mon, 2 May 2022 10:29:01 -0400 Subject: [PATCH] lengthen cache-control headers --- .node-version | 2 +- components/CopyButton/CopyButton.tsx | 4 +++- components/HitCounter/HitCounter.tsx | 15 +++++++++++---- lib/helpers/build-feed.ts | 6 +++--- lib/helpers/parse-notes.ts | 4 ++-- package.json | 4 ++-- pages/api/hits.ts | 2 +- pages/site.webmanifest.ts | 4 ++-- pages/sitemap.xml.ts | 2 +- yarn.lock | 20 ++++++++++---------- 10 files changed, 36 insertions(+), 27 deletions(-) diff --git a/.node-version b/.node-version index d9f88006..99cdd800 100644 --- a/.node-version +++ b/.node-version @@ -1 +1 @@ -16.14.2 +16.15.0 diff --git a/components/CopyButton/CopyButton.tsx b/components/CopyButton/CopyButton.tsx index cff94f99..efd95142 100644 --- a/components/CopyButton/CopyButton.tsx +++ b/components/CopyButton/CopyButton.tsx @@ -66,7 +66,9 @@ const CopyButton = forwardRef(function CopyButton( }, timeout); // cancel timeout to avoid memory leaks if unmounted in the middle of this - return () => clearTimeout(reset); + return () => { + clearTimeout(reset); + }; }, [timeout, copied]); return ( diff --git a/components/HitCounter/HitCounter.tsx b/components/HitCounter/HitCounter.tsx index c5a274ea..cc323ba8 100644 --- a/components/HitCounter/HitCounter.tsx +++ b/components/HitCounter/HitCounter.tsx @@ -10,10 +10,17 @@ export type HitCounterProps = { const HitCounter = ({ slug, className }: HitCounterProps) => { // start fetching repos from API immediately - const { data, error } = useSWR(`/api/hits/?slug=${encodeURIComponent(slug)}`, fetcher, { - // avoid double (or more) counting views - revalidateOnFocus: false, - }); + const { data, error } = useSWR( + `/api/hits/?${new URLSearchParams({ + slug, + })}`, + fetcher, + { + // avoid double (or more) counting views + revalidateOnFocus: false, + revalidateOnReconnect: false, + } + ); // show spinning loading indicator if data isn't fetched yet if (!data) { diff --git a/lib/helpers/build-feed.ts b/lib/helpers/build-feed.ts index 653fa258..59395046 100644 --- a/lib/helpers/build-feed.ts +++ b/lib/helpers/build-feed.ts @@ -9,7 +9,7 @@ import type { ParsedUrlQuery } from "querystring"; export type BuildFeedOptions = { type?: "rss" | "atom" | "json"; // defaults to rss - edgeCacheAge?: number; // defaults to 3600 (one hour) + edgeCacheAge?: number; // in seconds, defaults to 43200 (12 hours) }; // handles literally *everything* about building the server-side rss/atom feeds and writing the response. @@ -59,8 +59,8 @@ export const buildFeed = async ( }); }); - // cache on edge for one hour by default - res.setHeader("cache-control", `s-maxage=${options.edgeCacheAge ?? 3600}, stale-while-revalidate`); + // cache on edge for 12 hours by default + res.setHeader("cache-control", `s-maxage=${options.edgeCacheAge ?? 43200}, stale-while-revalidate=3600`); // generates RSS by default if (options.type === "atom") { diff --git a/lib/helpers/parse-notes.ts b/lib/helpers/parse-notes.ts index 943bcffb..0e819332 100644 --- a/lib/helpers/parse-notes.ts +++ b/lib/helpers/parse-notes.ts @@ -115,7 +115,7 @@ export const getAllNotes = async (): Promise => { }); // sort the results by date - const sorted = data.sort((note1, note2) => (note1.date > note2.date ? -1 : 1)); + data.sort((note1, note2) => (note1.date > note2.date ? -1 : 1)); - return sorted; + return data; }; diff --git a/package.json b/package.json index e8634b55..427f8dcf 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ }, "dependencies": { "@fontsource/comic-neue": "4.5.8", - "@fontsource/inter": "4.5.9", + "@fontsource/inter": "4.5.10", "@fontsource/roboto-mono": "4.5.7", "@giscus/react": "^2.0.3", "@hcaptcha/react-hcaptcha": "^1.3.0", @@ -110,7 +110,7 @@ }, "packageManager": "yarn@1.22.18", "volta": { - "node": "16.14.2", + "node": "16.15.0", "yarn": "1.22.18" } } diff --git a/pages/api/hits.ts b/pages/api/hits.ts index fba35743..464da3d3 100644 --- a/pages/api/hits.ts +++ b/pages/api/hits.ts @@ -46,7 +46,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { const siteStats = await getSiteStats(client); // let Vercel edge cache results for 15 mins - res.setHeader("Cache-Control", "s-maxage=900, stale-while-revalidate"); + res.setHeader("Cache-Control", "s-maxage=900, stale-while-revalidate=60"); // return in JSON format return res.status(200).json(siteStats); diff --git a/pages/site.webmanifest.ts b/pages/site.webmanifest.ts index bec65e40..8a741293 100644 --- a/pages/site.webmanifest.ts +++ b/pages/site.webmanifest.ts @@ -41,8 +41,8 @@ export const getServerSideProps: GetServerSideProps = async (context) => { // https://developer.mozilla.org/en-US/docs/Web/Manifest#deploying_a_manifest res.setHeader("content-type", "application/manifest+json; charset=utf-8"); - // cache on edge for one day - res.setHeader("cache-control", "s-maxage=604800, stale-while-revalidate"); + // cache on edge for one week + res.setHeader("cache-control", "s-maxage=604800, stale-while-revalidate=86400"); res.write(JSON.stringify(manifest)); res.end(); diff --git a/pages/sitemap.xml.ts b/pages/sitemap.xml.ts index 93a1dea2..522bc710 100644 --- a/pages/sitemap.xml.ts +++ b/pages/sitemap.xml.ts @@ -47,7 +47,7 @@ export const getServerSideProps: GetServerSideProps = async (context) => { // cache on edge for 12 hours const { res } = context; - res.setHeader("cache-control", "s-maxage=43200, stale-while-revalidate"); + res.setHeader("cache-control", "s-maxage=43200, stale-while-revalidate=3600"); // next-sitemap takes care of the rest of the response for us return getServerSideSitemap(context, pages); diff --git a/yarn.lock b/yarn.lock index 94d9dd76..5dd0b651 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1048,10 +1048,10 @@ resolved "https://registry.yarnpkg.com/@fontsource/comic-neue/-/comic-neue-4.5.8.tgz#269237c1b11d16efa1729a45cdd317799156204d" integrity sha512-m4Y/o/L8mzAuiWXBHisijMmCHDpHXFMxRyFQ6mDeBlOwlyUU7PB2XdrV6utR/1Y0wPjxa8AZKEYPMy1tBXDURA== -"@fontsource/inter@4.5.9": - version "4.5.9" - resolved "https://registry.yarnpkg.com/@fontsource/inter/-/inter-4.5.9.tgz#ae3d49935172684c39a75f1ec9d405d015ac5959" - integrity sha512-EwLZGA8De5r/EFokSNU+JKZRcpC9SlqBCg/2XhNzt9QWS3aPR6Xdo1oHJ8LhhGkgOReRo1sgpIrdcz9UMtUcOQ== +"@fontsource/inter@4.5.10": + version "4.5.10" + resolved "https://registry.yarnpkg.com/@fontsource/inter/-/inter-4.5.10.tgz#c4e6839197dc4c77842eae9475ff0e036ff87fc9" + integrity sha512-YOt2/K8yo25MVBjrTImHxVimmyZEt0GcrWp2w7O29sdFX9SJqbGlOqjFJ1wI5yBbP6AmTeimyPE0UC/jjFRoIA== "@fontsource/roboto-mono@4.5.7": version "4.5.7" @@ -1643,9 +1643,9 @@ integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA== "@types/node@*": - version "17.0.30" - resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.30.tgz#2c6e8512acac70815e8176aa30c38025067880ef" - integrity sha512-oNBIZjIqyHYP8VCNAV9uEytXVeXG2oR0w9lgAXro20eugRQfY002qr3CUl6BAe+Yf/z3CRjPdz27Pu6WWtuSRw== + version "17.0.31" + resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.31.tgz#a5bb84ecfa27eec5e1c802c6bbf8139bdb163a5d" + integrity sha512-AR0x5HbXGqkEx9CadRH3EBYx/VkiUgZIhP4wvPn/+5KIsgpNoyFaRlVe0Zlx9gRtg8fA06a9tskE2MSN7TcG4Q== "@types/parse-json@^4.0.0": version "4.0.0" @@ -2150,9 +2150,9 @@ camelcase@^6.2.0: integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA== caniuse-lite@^1.0.30001283, caniuse-lite@^1.0.30001332: - version "1.0.30001334" - resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001334.tgz#892e9965b35285033fc2b8a8eff499fe02f13d8b" - integrity sha512-kbaCEBRRVSoeNs74sCuq92MJyGrMtjWVfhltoHUCW4t4pXFvGjUBrfo47weBRViHkiV3eBYyIsfl956NtHGazw== + version "1.0.30001335" + resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001335.tgz#899254a0b70579e5a957c32dced79f0727c61f2a" + integrity sha512-ddP1Tgm7z2iIxu6QTtbZUv6HJxSaV/PZeSrWFZtbY4JZ69tOeNhBCl3HyRQgeNZKE5AOn1kpV7fhljigy0Ty3w== ccount@^1.0.0: version "1.1.0"