From 6d99abf5d36b7ade92f56659da0ffd003254fb1e Mon Sep 17 00:00:00 2001 From: Samet Date: Fri, 22 Mar 2024 00:11:56 +0300 Subject: [PATCH 1/3] feat(#239): implemented back cache control --- pages/api/view/[id].ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/pages/api/view/[id].ts b/pages/api/view/[id].ts index da66174..40bf345 100644 --- a/pages/api/view/[id].ts +++ b/pages/api/view/[id].ts @@ -1,6 +1,10 @@ import nextConnect from "next-connect"; import passport from "passport"; +export const config = { + runtime: "edge", +}; + import handlePlatformAPI from "@/services/api/handler"; import { @@ -9,7 +13,7 @@ import { } from "@/middlewares/api/private"; import { validateAccessToken, loadPassport } from "@/middlewares/api/auth"; import { resolveHandler } from "@/middlewares/api"; -// import { setCacheControl } from "@/middlewares/api/cache"; +import { setCacheControl } from "@/middlewares/api/cache"; import { NextApiRequest, NextApiResponse } from "next"; export default nextConnect() @@ -19,14 +23,20 @@ export default nextConnect() .use(resolveHandler) .use(loadPassport) .use(validateAccessToken) - // .use(setCacheControl) + .use(setCacheControl) .get((req: NextApiRequest, res: NextApiResponse) => { - const { services, templates, connection, query, config } = res.locals; + const { + services, + templates, + connection, + query, + config: configLocal, + } = res.locals; return handlePlatformAPI( services, templates, query, - config, + configLocal, connection )(req, res); }); From e4f21ff5e5a602dbba679bdcd692da7c425d5681 Mon Sep 17 00:00:00 2001 From: Samet Date: Fri, 22 Mar 2024 00:16:31 +0300 Subject: [PATCH 2/3] fix: switched the runtime to default on the view API --- pages/api/view/[id].ts | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/pages/api/view/[id].ts b/pages/api/view/[id].ts index 40bf345..97fff7b 100644 --- a/pages/api/view/[id].ts +++ b/pages/api/view/[id].ts @@ -1,10 +1,6 @@ import nextConnect from "next-connect"; import passport from "passport"; -export const config = { - runtime: "edge", -}; - import handlePlatformAPI from "@/services/api/handler"; import { @@ -25,18 +21,12 @@ export default nextConnect() .use(validateAccessToken) .use(setCacheControl) .get((req: NextApiRequest, res: NextApiResponse) => { - const { - services, - templates, - connection, - query, - config: configLocal, - } = res.locals; + const { services, templates, connection, query, config } = res.locals; return handlePlatformAPI( services, templates, query, - configLocal, + config, connection )(req, res); }); From ba7e94690ae5b1c3788ec17a6b658f657f34c94b Mon Sep 17 00:00:00 2001 From: Samet Date: Fri, 22 Mar 2024 00:26:49 +0300 Subject: [PATCH 3/3] feat: added stale-white-revalidate directive --- middlewares/api/cache.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/middlewares/api/cache.ts b/middlewares/api/cache.ts index 6e47f0a..f2b3ce4 100644 --- a/middlewares/api/cache.ts +++ b/middlewares/api/cache.ts @@ -9,10 +9,16 @@ export const setCacheControl = ( query: { cache_time, _vercel_no_cache }, } = res.locals; - res.setHeader("Cache-Control", "max-age=60"); + res.setHeader("Cache-Control", "max-age=60, stale-while-revalidate=2592000"); if (_vercel_no_cache === undefined) { - res.setHeader("CDN-Cache-Control", `max-age=${cache_time}`); - res.setHeader("Vercel-CDN-Cache-Control", `max-age=${cache_time}`); + res.setHeader( + "CDN-Cache-Control", + `max-age=${cache_time}, stale-while-revalidate=2592000` + ); + res.setHeader( + "Vercel-CDN-Cache-Control", + `max-age=${cache_time}, stale-while-revalidate=2592000` + ); } next();