Skip to content

Commit

Permalink
Merge pull request #241 from sametcodes/develop
Browse files Browse the repository at this point in the history
Implemented a better cache control
  • Loading branch information
sametcodes authored Mar 21, 2024
2 parents 87bb897 + c0d7c04 commit 7d01bcb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 12 additions & 3 deletions middlewares/api/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@ export const setCacheControl = (
query: { cache_time, _vercel_no_cache },
} = res.locals;

res.setHeader("Cache-Control", "max-age=60");
res.setHeader(
"Cache-Control",
`max-age=${cache_time}, 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 7d01bcb

Please sign in to comment.