diff options
Diffstat (limited to 'astro.config.ts')
-rw-r--r-- | astro.config.ts | 59 |
1 files changed, 34 insertions, 25 deletions
diff --git a/astro.config.ts b/astro.config.ts index c7d4946..35fb69d 100644 --- a/astro.config.ts +++ b/astro.config.ts @@ -15,9 +15,7 @@ import remarkToc from "remark-toc"; import { get } from "./src/utils/anonymous.ts"; import { loadEnv } from "vite"; import process from "node:process"; -import { generateSW, injectManifest } from "workbox-build"; -import { NetworkFirst, StaleWhileRevalidate } from "workbox-strategies"; -import { ExpirationPlugin } from "workbox-expiration"; +import { generateSW } from "workbox-build"; // deno-lint-ignore no-non-null-assertion const { PUBLIC_SITE_URL } = loadEnv(process.env.NODE_ENV!, process.cwd(), ""); @@ -80,40 +78,51 @@ export default defineConfig({ name: "service worker", hooks: { "astro:build:done": async ({ dir, logger }) => { - // const { warnings, count, size } = await injectManifest({ - // globDirectory: dir.pathname, - // swSrc: "src/sw.ts", - // swDest: new URL("sw.js", dir).pathname, - // }); - - const theLatest = /^(\/public)?\/blog/; const { warnings, count, size } = await generateSW({ swDest: new URL("sw.js", dir).pathname, globDirectory: dir.pathname, - globPatterns: ["**/*"], - skipWaiting: true, + globPatterns: [ + "**/*.{html,css,webmanifest,svg,txt,xml}", + "_astro/*.js", + ], + globIgnores: ["blog/**/*", "keys/**/*"], + globStrict: true, + skipWaiting: false, clientsClaim: true, runtimeCaching: [{ - urlPattern({ sameOrigin, url }) { - return sameOrigin && theLatest.test(url.pathname); - }, - handler: new NetworkFirst({ + urlPattern: ({ sameOrigin, url }) => + sameOrigin && + /^\/blog\/read\/[a-z0-9-]+(?:\/|\.md|\.md\.sig)?$/.test( + url.pathname, + ), + handler: "NetworkFirst", + options: { cacheName: "posts", networkTimeoutSeconds: 3, - }), + }, }, { - urlPattern({ sameOrigin, url }) { - return sameOrigin && !theLatest.test(url.pathname); + urlPattern: ({ sameOrigin, url }) => + sameOrigin && + /^\/blog(\/(20\d{2}(\/\d{2}(\/\d{2})?)?)?|\/keywords(\/[\w-]+)?)?\/?$/ + .test(url.pathname), + handler: "NetworkFirst", + options: { + cacheName: "indexes", + networkTimeoutSeconds: 3, }, - handler: new StaleWhileRevalidate({ + }, { + urlPattern: ({ sameOrigin, url }) => + sameOrigin && /^(?!\/blog).*/.test(url.pathname), + handler: "StaleWhileRevalidate", + options: { cacheName: "resources", - plugins: [ - new ExpirationPlugin({ maxAgeSeconds: 60 * 60 * 24 * 30 }), - ], - }), + expiration: { + maxAgeSeconds: 60 * 60 * 24 * 30, + }, + }, }], navigateFallback: undefined, - navigationPreload: undefined, + navigationPreload: true, inlineWorkboxRuntime: true, cleanupOutdatedCaches: true, dontCacheBustURLsMatching: /^_astro\/.*\.js$/, |