aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/config/cache_control_middleware.ts15
-rw-r--r--src/server.ts2
2 files changed, 17 insertions, 0 deletions
diff --git a/src/config/cache_control_middleware.ts b/src/config/cache_control_middleware.ts
new file mode 100644
index 0000000..5543ddc
--- /dev/null
+++ b/src/config/cache_control_middleware.ts
@@ -0,0 +1,15 @@
+import type { MiddlewareHandler } from "hono";
+
+// Define middleware to add Cache-Control header
+const cacheControlMiddleware: MiddlewareHandler = async (c, next) => {
+ const sMaxAge = process.env.S_MAXAGE || "60";
+ const staleWhileRevalidate = process.env.STALE_WHILE_REVALIDATE || "30";
+ c.header(
+ "Cache-Control",
+ `s-maxage=${sMaxAge}, stale-while-revalidate=${staleWhileRevalidate}`
+ );
+
+ await next();
+};
+
+export default cacheControlMiddleware;
diff --git a/src/server.ts b/src/server.ts
index 41a64c5..780d25b 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -5,6 +5,7 @@ import corsConfig from "./config/cors.js";
import { ratelimit } from "./config/ratelimit.js";
import { hianimeRouter } from "./routes/hianime.js";
+import cacheControlMiddleware from "./config/cache_control_middleware.js";
import { Hono } from "hono";
import { logger } from "hono/logger";
@@ -26,6 +27,7 @@ const app = new Hono<{ Variables: AniwatchAPIVariables }>();
app.use(logger());
app.use(corsConfig);
+app.use(cacheControlMiddleware);
// CAUTION: For personal deployments, "refrain" from having an env
// named "ANIWATCH_API_HOSTNAME". You may face rate limitting