From 9be8e2700169d89097563d0505b3e3e73b6c48e5 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sat, 6 Sep 2025 20:40:44 +0200 Subject: [PATCH] Cache resources --- page/index.html | 1 + page/vite.config.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/page/index.html b/page/index.html index d6487a55..d5cb1bc9 100644 --- a/page/index.html +++ b/page/index.html @@ -7,6 +7,7 @@ diff --git a/page/vite.config.ts b/page/vite.config.ts index 6cede1ae..dc78b2be 100644 --- a/page/vite.config.ts +++ b/page/vite.config.ts @@ -3,9 +3,30 @@ import tailwindcss from "@tailwindcss/vite"; import { defineConfig } from "vite"; import { VitePWA } from "vite-plugin-pwa"; import react from "@vitejs/plugin-react"; +import { RuntimeCaching } from "workbox-build"; const mb = 1024 ** 2; +function generateExternalCache( + pattern: string | RegExp, + name: string, +): RuntimeCaching { + return { + urlPattern: pattern, + handler: "CacheFirst", + options: { + cacheName: name, + expiration: { + maxEntries: 10, + maxAgeSeconds: 60 * 60 * 24 * 365, // <== 365 days + }, + cacheableResponse: { + statuses: [0, 200], + }, + }, + }; +} + // https://vite.dev/config/ export default defineConfig({ plugins: [ @@ -19,6 +40,17 @@ export default defineConfig({ }, workbox: { maximumFileSizeToCacheInBytes: 100 * mb, + globPatterns: ["**/*.{js,css,html,woff,woff2}"], + runtimeCaching: [ + generateExternalCache( + /^https:\/\/momo5502\.com\/.*/i, + "momo5502-cache", + ), + generateExternalCache( + /^https:\/\/img\.youtube\.com\/.*/i, + "youtube-img-cache", + ), + ], }, }), ],