From 6cb75d54f356fa93a7a206b18f5742c4ce7ee9e6 Mon Sep 17 00:00:00 2001 From: ryo-ma Date: Sun, 23 Aug 2020 23:07:27 +0900 Subject: [PATCH] Support cache-control --- dev_main.ts | 11 ++++++++--- index.ts | 9 ++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/dev_main.ts b/dev_main.ts index 3d37f01..2574661 100644 --- a/dev_main.ts +++ b/dev_main.ts @@ -5,15 +5,21 @@ import "https://deno.land/x/dotenv@v0.5.0/load.ts"; const s = serve({ port: 8080 }); const client = new GithubAPIClient(); +const cacheMaxAge = 7200; for await (const req of s) { - const username = new URLSearchParams(req.url.split("?")[1]).get("username") + const username = new URLSearchParams(req.url.split("?")[1]).get("username"); if (username != null) { const userInfo = await client.requestUserInfo(username); req.respond( { body: new Card().render(userInfo), - headers: new Headers({ "Content-Type": "image/svg+xml" }), + headers: new Headers( + { + "Content-Type": "image/svg+xml", + "Cache-Control": `public, max-age: ${cacheMaxAge}`, + }, + ), }, ); } else { @@ -24,6 +30,5 @@ for await (const req of s) { headers: new Headers({ "Content-Type": "text" }), }, ); - } } diff --git a/index.ts b/index.ts index 440ea63..9b9a2a9 100644 --- a/index.ts +++ b/index.ts @@ -2,7 +2,9 @@ import { ServerRequest } from "./deps.ts"; import { GithubAPIClient } from "./src/github_api_client.ts"; import { Card } from "./src/card.ts"; import "https://deno.land/x/dotenv@v0.5.0/load.ts"; + const client = new GithubAPIClient(); +const cacheMaxAge = 7200; export default async (req: ServerRequest) => { const username = new URLSearchParams(req.url.split("?")[1]).get("username"); @@ -12,7 +14,12 @@ export default async (req: ServerRequest) => { req.respond( { body: new Card().render(userInfo), - headers: new Headers({ "Content-Type": "image/svg+xml" }), + headers: new Headers( + { + "Content-Type": "image/svg+xml", + "Cache-Control": `public, max-age: ${cacheMaxAge}`, + }, + ), }, ); } else {