Skip to content

Commit

Permalink
Merge pull request #240 from sametcodes/239-implementing-caching-on-t…
Browse files Browse the repository at this point in the history
…he-view-api

feat(#239): implemented back cache control
  • Loading branch information
sametcodes authored Mar 21, 2024
2 parents 7e461f1 + ba7e946 commit ae52627
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 9 additions & 3 deletions middlewares/api/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions pages/api/view/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,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()
Expand All @@ -19,7 +19,7 @@ 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;
return handlePlatformAPI(
Expand Down

0 comments on commit ae52627

Please sign in to comment.