diff --git a/api/index.ts b/api/index.ts index daa117e..6f39b93 100644 --- a/api/index.ts +++ b/api/index.ts @@ -13,10 +13,18 @@ import { cacheProvider } from "../src/config/cache.ts"; const serviceProvider = new GithubApiService(); const client = new GithubRepositoryService(serviceProvider).repository; +// Build cache control header with optimized caching strategy +const cacheControlHeader = [ + "public", + `max-age=${CONSTANTS.CACHE_MAX_AGE}`, + `s-maxage=${CONSTANTS.CDN_CACHE_MAX_AGE}`, + `stale-while-revalidate=${CONSTANTS.STALE_WHILE_REVALIDATE}`, +].join(", "); + const defaultHeaders = new Headers( { "Content-Type": "image/svg+xml", - "Cache-Control": `public, max-age=${CONSTANTS.CACHE_MAX_AGE}`, + "Cache-Control": cacheControlHeader, }, ); @@ -86,7 +94,7 @@ async function app(req: Request): Promise { status: error.status, headers: new Headers({ "Content-Type": "text", - "Cache-Control": `public, max-age=${CONSTANTS.CACHE_MAX_AGE}`, + "Cache-Control": cacheControlHeader, }), }, ); @@ -132,7 +140,7 @@ async function app(req: Request): Promise { status: userResponseInfo.code, headers: new Headers({ "Content-Type": "text", - "Cache-Control": `public, max-age=${CONSTANTS.CACHE_MAX_AGE}`, + "Cache-Control": cacheControlHeader, }), }, ); diff --git a/src/utils.ts b/src/utils.ts index 86f90ce..9d52973 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -52,6 +52,8 @@ const HOUR_IN_MILLISECONDS = 60 * 60 * 1000; export const CONSTANTS = { CACHE_MAX_AGE: 18800, + CDN_CACHE_MAX_AGE: 28800, // 8 hours for CDN edge cache + STALE_WHILE_REVALIDATE: 86400, // 24 hours - serve stale while revalidating DEFAULT_PANEL_SIZE: 110, DEFAULT_MAX_COLUMN: 8, DEFAULT_MAX_ROW: 3, @@ -60,7 +62,7 @@ export const CONSTANTS = { DEFAULT_NO_BACKGROUND: false, DEFAULT_NO_FRAME: false, DEFAULT_GITHUB_API: "https://api.github.com/graphql", - DEFAULT_GITHUB_RETRY_DELAY: 1000, + DEFAULT_GITHUB_RETRY_DELAY: 500, REVALIDATE_TIME: HOUR_IN_MILLISECONDS, REDIS_TTL: HOUR_IN_MILLISECONDS * 4, };