feat: 🎸 add assets cache

This commit is contained in:
zhangqingwu
2024-08-04 14:58:38 +08:00
committed by Lyric Wai
parent ea6d261806
commit ffcd6cda80
3 changed files with 32 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
<template> <template>
<div class="default-layout"> <div class="default-layout bg-white">
<PageHeader /> <PageHeader />
<slot /> <slot />
<PageFooter /> <PageFooter />

View File

@@ -3,3 +3,7 @@
@tailwind base; @tailwind base;
@tailwind components; @tailwind components;
@tailwind utilities; @tailwind utilities;
html {
background: white;
}

View File

@@ -1,4 +1,7 @@
import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler' import {
getAssetFromKV,
mapRequestToAsset,
} from "@cloudflare/kv-asset-handler";
/** /**
* The DEBUG flag will do two things that help during development: * The DEBUG flag will do two things that help during development:
@@ -7,26 +10,26 @@ import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler'
* 2. we will return an error message on exception in your Response rather * 2. we will return an error message on exception in your Response rather
* than the default 404.html page. * than the default 404.html page.
*/ */
const DEBUG = true const DEBUG = true;
addEventListener('fetch', event => { addEventListener("fetch", (event) => {
try { try {
event.respondWith(handleEvent(event)) event.respondWith(handleEvent(event));
} catch (e) { } catch (e) {
if (DEBUG) { if (DEBUG) {
return event.respondWith( return event.respondWith(
new Response(e.message || e.toString(), { new Response(e.message || e.toString(), {
status: 500, status: 500,
}), })
) );
} }
event.respondWith(new Response('Internal Error', { status: 500 })) event.respondWith(new Response("Internal Error", { status: 500 }));
} }
}) });
async function handleEvent(event) { async function handleEvent(event) {
// const url = new URL(event.request.url) // const url = new URL(event.request.url)
let options = {} let options = {};
// const key = url.pathname.slice(1); // const key = url.pathname.slice(1);
// if (event.request.method === 'GET' && key.startsWith("storage/")) { // if (event.request.method === 'GET' && key.startsWith("storage/")) {
@@ -79,8 +82,11 @@ async function handleEvent(event) {
response.headers.set("Referrer-Policy", "unsafe-url"); response.headers.set("Referrer-Policy", "unsafe-url");
response.headers.set("Feature-Policy", "none"); response.headers.set("Feature-Policy", "none");
return response; if (event.request.url.match(/\.(js|css|png|jpg|svg|ttf)$/)) {
response.headers.set("Cache-Control", "max-age=604800");
}
return response;
} catch (e) { } catch (e) {
// if an error is thrown try to serve the asset at 404.html // if an error is thrown try to serve the asset at 404.html
// if (!DEBUG) { // if (!DEBUG) {
@@ -103,18 +109,18 @@ async function handleEvent(event) {
} }
// If the pathname ends with '/', assume it's a directory and append 'index.html' // If the pathname ends with '/', assume it's a directory and append 'index.html'
if (pathname.endsWith('/')) { if (pathname.endsWith("/")) {
pathname += 'index.html'; pathname += "index.html";
} }
// If it doesn't have an extension, assume it's a directory and append '/index.html' // If it doesn't have an extension, assume it's a directory and append '/index.html'
else if (!pathname.split("/").pop().includes(".")) { else if (!pathname.split("/").pop().includes(".")) {
pathname += '/index.html'; pathname += "/index.html";
} }
return new Request(`${url.origin}${pathname}`, req); return new Request(`${url.origin}${pathname}`, req);
}, },
}) });
// return new Response(e.message || e.toString(), { status: 500 }) // return new Response(e.message || e.toString(), { status: 500 })
} }
@@ -128,15 +134,15 @@ async function handleEvent(event) {
* to exist at a specific path. * to exist at a specific path.
*/ */
function handlePrefix(prefix) { function handlePrefix(prefix) {
return request => { return (request) => {
// compute the default (e.g. / -> index.html) // compute the default (e.g. / -> index.html)
let defaultAssetKey = mapRequestToAsset(request) let defaultAssetKey = mapRequestToAsset(request);
let url = new URL(defaultAssetKey.url) let url = new URL(defaultAssetKey.url);
// strip the prefix from the path for lookup // strip the prefix from the path for lookup
url.pathname = url.pathname.replace(prefix, '/') url.pathname = url.pathname.replace(prefix, "/");
// inherit all other props from the default request // inherit all other props from the default request
return new Request(url.toString(), defaultAssetKey) return new Request(url.toString(), defaultAssetKey);
} };
} }