-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
proxy all files instead of using signed bucket urls
- Loading branch information
Showing
4 changed files
with
50 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,25 @@ | ||
export default { | ||
async fetch(request, env, ctx) { | ||
const url = new URL(request.url); | ||
const extension = url.pathname.split('.').pop(); | ||
|
||
// Forward the request to the origin | ||
const response = await fetch(request); | ||
const modifiedResponse = new Response(response.body, response); | ||
|
||
// Clear any existing cache headers | ||
modifiedResponse.headers.delete('Pragma'); | ||
modifiedResponse.headers.delete('Cache-Control'); | ||
modifiedResponse.headers.delete('Expires'); | ||
|
||
// Check if this is a redirect to a signed URL | ||
if (response.status === 302 && response.headers.get('Location')?.includes('storage.googleapis.com')) { | ||
// Signed URL redirects: private, 50 minutes | ||
const maxAge = 50 * 60; // 50 minutes in seconds | ||
modifiedResponse.headers.set('Cache-Control', `private, max-age=${maxAge}`); | ||
const expiresDate = new Date(Date.now() + maxAge * 1000); | ||
modifiedResponse.headers.set('Expires', expiresDate.toUTCString()); | ||
} else if (extension) { | ||
// Files with extensions | ||
switch (extension.toLowerCase()) { | ||
case 'html': | ||
// HTML files: private, 60 seconds | ||
modifiedResponse.headers.set('Cache-Control', 'private, max-age=60'); | ||
break; | ||
|
||
case 'css': | ||
case 'js': | ||
case 'map': | ||
case 'wasm': | ||
case 'data': | ||
case 'bin': // Binary files often used with WebGL | ||
case 'gltf': // 3D models | ||
case 'glb': // Binary 3D models | ||
case 'obj': // 3D models | ||
case 'mtl': // Material files for 3D models | ||
case 'json': // Often used for model/scene data | ||
// Static assets: private, 10 minutes | ||
modifiedResponse.headers.set('Cache-Control', 'private, max-age=600'); | ||
break; | ||
|
||
default: | ||
// Other static assets that aren't redirects: private, short cache | ||
modifiedResponse.headers.set('Cache-Control', 'private, max-age=60'); | ||
// Check for custom cache policy | ||
const cachePolicyHeader = response.headers.get('X-Cache-Policy'); | ||
if (cachePolicyHeader) { | ||
try { | ||
const policy = JSON.parse(cachePolicyHeader); | ||
const modifiedResponse = new Response(response.body, response); | ||
modifiedResponse.headers.delete('Pragma'); | ||
modifiedResponse.headers.delete('Cache-Control'); | ||
modifiedResponse.headers.delete('Expires'); | ||
modifiedResponse.headers.set('Cache-Control', `${policy.visibility}, max-age=${policy.maxAge}`); | ||
const expiresDate = new Date(Date.now() + policy.maxAge * 1000); | ||
modifiedResponse.headers.set('Expires', expiresDate.toUTCString()); | ||
return modifiedResponse; | ||
} catch (e) { | ||
console.error('Failed to parse cache policy:', e); | ||
} | ||
} else { | ||
// Paths without extensions (HTML fallbacks): private, 60 seconds | ||
modifiedResponse.headers.set('Cache-Control', 'private, max-age=60'); | ||
} | ||
return modifiedResponse; | ||
|
||
return response; | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters