Skip to content

Commit

Permalink
feat: use subrequestCacheBehavior for getting image
Browse files Browse the repository at this point in the history
  • Loading branch information
ymtszw committed Apr 21, 2023
1 parent 0baf869 commit ad3a377
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ function handleOptions(request: Request, origin: string) {
}
}

const subrequestCacheBehavior = {
cacheTtlByStatus: { "200-499": 60 * 60 * 24 * 7, "500-599": 0 },
cacheEverything: true,
};

async function handleGetTwitterProfileImage(
twitterUserName: string,
origin: string
Expand All @@ -82,7 +87,7 @@ async function handleGetTwitterProfileImage(
const md = await extractMetadata(url);
// Profile page has profile image URL as metadata
if (md.image) {
const res = await fetch(md.image);
const res = await fetch(md.image, { cf: subrequestCacheBehavior });
const contentType = res.headers.get("content-type") || "text/plain";
if (contentType.startsWith("image/")) {
// Hide origin info, creating new Response object.
Expand Down Expand Up @@ -120,14 +125,10 @@ async function extractMetadata(query: string): Promise<Metadata> {
"user-agent": `LinkPreviewBot/${version}`,
"accept-language": "ja-JP",
};
const cacheBehavior = {
cacheTtlByStatus: { "200-499": 60 * 60 * 24 * 7, "500-599": 0 },
cacheEverything: true,
};
const res = await fetch(query, {
redirect: "follow",
headers: headers,
cf: cacheBehavior,
cf: subrequestCacheBehavior,
});
if (res.status >= 400) {
return { error: `[Error] ${query} returned status code: ${res.status}!` };
Expand Down

0 comments on commit ad3a377

Please sign in to comment.