Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
cache raw url in CDN for 1h before expire
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenlx committed Feb 26, 2023
1 parent a7fdf49 commit cbedcf6
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/utils/api/raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ export async function handleRaw(ctx: { headers?: Headers; requestPath: string; a
if (!downloadUrl) return ResponseCompat.json({ error: 'No download url found.' }, { status: 404, ...init })

// Only proxy raw file content response for files up to 4MB
if (!(proxy && size && size < 4194304)) return ResponseCompat.redirect(downloadUrl, init)
if (!(proxy && size && size < 4194304)) {
// CDN Cache for 1 hour
// https://learn.microsoft.com/en-us/graph/api/resources/driveitem?view=graph-rest-1.0#instance-attributes
init.headers.set('Cache-Control', 'public, max-age=0, s-maxage=3600, immutable')
return ResponseCompat.redirect(downloadUrl, { status: 308, ...init })
}

const { body: dlBody, headers: dlHeader } = await fetch(downloadUrl)
dlHeader.set('Cache-Control', cacheControlHeader)
Expand Down

0 comments on commit cbedcf6

Please sign in to comment.