From e43b58379323144cb4067ec1329a2ccfaaa5cc5e Mon Sep 17 00:00:00 2001 From: João Augusto Costa Branco Marado Torres Date: Fri, 4 Jul 2025 21:14:34 -0300 Subject: fix: better caching MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Augusto Costa Branco Marado Torres --- astro.config.ts | 59 +++++++++++++++++------------- deno.lock | 5 +-- package.json | 2 - public/blog/TEMPLATE | 46 ----------------------- public/blog/legislativas-2025.md | 41 --------------------- public/blog/legislativas-2025.md.sig | Bin 2097 -> 0 bytes public/blog/micro-test.md | 16 -------- public/blog/read/TEMPLATE | 46 +++++++++++++++++++++++ public/blog/read/legislativas-2025.md | 41 +++++++++++++++++++++ public/blog/read/legislativas-2025.md.sig | Bin 0 -> 2097 bytes public/blog/read/micro-test.md | 16 ++++++++ src/components/BaseHead.astro | 7 +++- src/components/signature/Downloads.astro | 2 +- src/content.config.ts | 2 +- 14 files changed, 146 insertions(+), 137 deletions(-) delete mode 100644 public/blog/TEMPLATE delete mode 100644 public/blog/legislativas-2025.md delete mode 100644 public/blog/legislativas-2025.md.sig delete mode 100644 public/blog/micro-test.md create mode 100644 public/blog/read/TEMPLATE create mode 100644 public/blog/read/legislativas-2025.md create mode 100644 public/blog/read/legislativas-2025.md.sig create mode 100644 public/blog/read/micro-test.md 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$/, diff --git a/deno.lock b/deno.lock index 865e396..7bd717f 100644 --- a/deno.lock +++ b/deno.lock @@ -41,8 +41,7 @@ "npm:vfile@^6.0.3": "6.0.3", "npm:vite@7": "7.0.0_picomatch@4.0.2_@types+node@22.15.15", "npm:workbox-build@^7.3.0": "7.3.0_ajv@8.17.1_@babel+core@7.27.7_rollup@2.79.2", - "npm:workbox-expiration@^7.3.0": "7.3.0", - "npm:workbox-strategies@^7.3.0": "7.3.0", + "npm:workbox-core@^7.3.0": "7.3.0", "npm:workbox-window@^7.3.0": "7.3.0", "npm:yaqrcode@~0.2.1": "0.2.1", "npm:zod@^3.25.67": "3.25.67" @@ -5321,8 +5320,6 @@ "npm:vfile@^6.0.3", "npm:vite@7", "npm:workbox-build@^7.3.0", - "npm:workbox-expiration@^7.3.0", - "npm:workbox-strategies@^7.3.0", "npm:workbox-window@^7.3.0", "npm:yaqrcode@~0.2.1", "npm:zod@^3.25.67" diff --git a/package.json b/package.json index e93d4b7..f0adf95 100644 --- a/package.json +++ b/package.json @@ -24,8 +24,6 @@ "unist-util-visit": "^5.0.0", "vfile": "^6.0.3", "workbox-build": "^7.3.0", - "workbox-expiration": "^7.3.0", - "workbox-strategies": "^7.3.0", "workbox-window": "^7.3.0", "yaqrcode": "^0.2.1", "zod": "^3.25.67" diff --git a/public/blog/TEMPLATE b/public/blog/TEMPLATE deleted file mode 100644 index ea0bdd7..0000000 --- a/public/blog/TEMPLATE +++ /dev/null @@ -1,46 +0,0 @@ -+++ -title = '' -# subtitle = '' -# description = ''' -# -# ''' -keywords = [] # list of available keywords in `src/consts.ts` -dateCreated = 1974-04-25T00:21:00+01:00 -# dateUpdated = 2025-04-25 -# locationCreated = '' -# relatedPosts = [] -lang = 'pt-PT' -# translationOf = '' -# license = '' -# -# [[signers]] -# entity = '' -# role = '' -# -# [[signers]] -# entity = '' -# role = '' -+++ - - - -paragraph after title header - -# (Sub)Header - -paragraph. - -## (Subsub)Header - -paragraph. - -# (Sub)Header - -paragraph. - -paragraph. diff --git a/public/blog/legislativas-2025.md b/public/blog/legislativas-2025.md deleted file mode 100644 index f6baf82..0000000 --- a/public/blog/legislativas-2025.md +++ /dev/null @@ -1,41 +0,0 @@ -+++ -kind = 'original' -title = 'Eleições para a Assembleia da República 2025' -subtitle = 'A minha opinião acerca dos resultados' -description = ''' -Dia 18 de maio de 2025 houve eleições legislativas em Portugal. - -A direita e o conservadorismo ganharam mais poder do que já tinham. -''' -keywords = ['democracy', 'Portugal'] -dateCreated = 2025-05-18T21:31:00-03:00 -dateUpdated = 2025-05-19T00:00:00-03:00 -locationCreated = 'RJ, Brasil' -lang = 'pt-PT' -license = "CC-BY-NC-SA" - -[[signers]] -entity = 'cravodeabril' -role = 'author' -+++ - -Hoje eu vi uma esquerda derrotada[^1]. Vou para a cama, organizar as minhas -ideias, dormir, logo volto. - -# Quem sou eu (brevemente) - -Acabou que este será o primeiro blog post, e ainda nem me -apresentei, ainda não expliquei o porquê de eu ter decidido comprar este domínio -e para quê que o quero usar. - -O meu nome é João Augusto Costa Branco Marado Torres, nasci em Abril de 2005, -sou atualmente estudante no IPBeja, 2.º ano da Licenciatura de -Engenharia Informática, apesar de já não pisar em Beja vai fazer 1 ano. Estive -no semestre passado em intercâmbio em İstanbul, Türkiye, -e neste semestre estou no RJ, Brasil. - -Deve fazer cerca de 2 anos que tive um "woke awakening". -Vá lá, talvez eu sempre tenha sido "woke", mas antes eu -não me importava tanto - -[^1]: diff --git a/public/blog/legislativas-2025.md.sig b/public/blog/legislativas-2025.md.sig deleted file mode 100644 index 8ad85c9..0000000 Binary files a/public/blog/legislativas-2025.md.sig and /dev/null differ diff --git a/public/blog/micro-test.md b/public/blog/micro-test.md deleted file mode 100644 index 8d64b60..0000000 --- a/public/blog/micro-test.md +++ /dev/null @@ -1,16 +0,0 @@ -+++ -kind = 'micro' -title = 'Isto é um microblog post de teste' -keywords = ['test'] -dateCreated = 2025-06-25T23:21:00-03:00 -locationCreated = 'RJ, Brasil' -lang = 'pt-PT' - -[[signers]] -entity = 'cravodeabril' -role = 'author' -+++ - -Apenas testando uma coisa aqui. - -Como está a correr o teu dia? diff --git a/public/blog/read/TEMPLATE b/public/blog/read/TEMPLATE new file mode 100644 index 0000000..ea0bdd7 --- /dev/null +++ b/public/blog/read/TEMPLATE @@ -0,0 +1,46 @@ ++++ +title = '' +# subtitle = '' +# description = ''' +# +# ''' +keywords = [] # list of available keywords in `src/consts.ts` +dateCreated = 1974-04-25T00:21:00+01:00 +# dateUpdated = 2025-04-25 +# locationCreated = '' +# relatedPosts = [] +lang = 'pt-PT' +# translationOf = '' +# license = '' +# +# [[signers]] +# entity = '' +# role = '' +# +# [[signers]] +# entity = '' +# role = '' ++++ + + + +paragraph after title header + +# (Sub)Header + +paragraph. + +## (Subsub)Header + +paragraph. + +# (Sub)Header + +paragraph. + +paragraph. diff --git a/public/blog/read/legislativas-2025.md b/public/blog/read/legislativas-2025.md new file mode 100644 index 0000000..f6baf82 --- /dev/null +++ b/public/blog/read/legislativas-2025.md @@ -0,0 +1,41 @@ ++++ +kind = 'original' +title = 'Eleições para a Assembleia da República 2025' +subtitle = 'A minha opinião acerca dos resultados' +description = ''' +Dia 18 de maio de 2025 houve eleições legislativas em Portugal. + +A direita e o conservadorismo ganharam mais poder do que já tinham. +''' +keywords = ['democracy', 'Portugal'] +dateCreated = 2025-05-18T21:31:00-03:00 +dateUpdated = 2025-05-19T00:00:00-03:00 +locationCreated = 'RJ, Brasil' +lang = 'pt-PT' +license = "CC-BY-NC-SA" + +[[signers]] +entity = 'cravodeabril' +role = 'author' ++++ + +Hoje eu vi uma esquerda derrotada[^1]. Vou para a cama, organizar as minhas +ideias, dormir, logo volto. + +# Quem sou eu (brevemente) + +Acabou que este será o primeiro blog post, e ainda nem me +apresentei, ainda não expliquei o porquê de eu ter decidido comprar este domínio +e para quê que o quero usar. + +O meu nome é João Augusto Costa Branco Marado Torres, nasci em Abril de 2005, +sou atualmente estudante no IPBeja, 2.º ano da Licenciatura de +Engenharia Informática, apesar de já não pisar em Beja vai fazer 1 ano. Estive +no semestre passado em intercâmbio em İstanbul, Türkiye, +e neste semestre estou no RJ, Brasil. + +Deve fazer cerca de 2 anos que tive um "woke awakening". +Vá lá, talvez eu sempre tenha sido "woke", mas antes eu +não me importava tanto + +[^1]: diff --git a/public/blog/read/legislativas-2025.md.sig b/public/blog/read/legislativas-2025.md.sig new file mode 100644 index 0000000..8ad85c9 Binary files /dev/null and b/public/blog/read/legislativas-2025.md.sig differ diff --git a/public/blog/read/micro-test.md b/public/blog/read/micro-test.md new file mode 100644 index 0000000..8d64b60 --- /dev/null +++ b/public/blog/read/micro-test.md @@ -0,0 +1,16 @@ ++++ +kind = 'micro' +title = 'Isto é um microblog post de teste' +keywords = ['test'] +dateCreated = 2025-06-25T23:21:00-03:00 +locationCreated = 'RJ, Brasil' +lang = 'pt-PT' + +[[signers]] +entity = 'cravodeabril' +role = 'author' ++++ + +Apenas testando uma coisa aqui. + +Como está a correr o teu dia? diff --git a/src/components/BaseHead.astro b/src/components/BaseHead.astro index 41926ad..65e568c 100644 --- a/src/components/BaseHead.astro +++ b/src/components/BaseHead.astro @@ -98,9 +98,14 @@ const { const wb = new Workbox("/sw.js", { type: "module" }); wb.addEventListener("activated", (_event) => { + if (!location.pathname.startsWith("/blog/read/")) { + return; + } const urlsToCache = [ location.href, - ...performance.getEntriesByType("resource").map((r) => r.name), + location.href + ".md", + location.href + ".md.sig", + ///...performance.getEntriesByType("resource").map((r) => r.name), ]; wb.messageSW({ type: "CACHE_URLS", diff --git a/src/components/signature/Downloads.astro b/src/components/signature/Downloads.astro index ac8215f..3497b37 100644 --- a/src/components/signature/Downloads.astro +++ b/src/components/signature/Downloads.astro @@ -9,7 +9,7 @@ interface Props { const { lang } = Astro.props; let source = new URL( - `${Astro.url.href.replace("read/", "").replace(/\/$/, "")}.md`, + `${Astro.url.href.replace(/\/$/, "")}.md`, ); const dir = await gitDir(); diff --git a/src/content.config.ts b/src/content.config.ts index 821faf5..d3504e5 100644 --- a/src/content.config.ts +++ b/src/content.config.ts @@ -5,7 +5,7 @@ import { parse } from "toml"; import { Blog, Entity } from "./lib/collection/schemas.ts"; const blog = defineCollection({ - loader: glob({ base: "./public/blog", pattern: "+([0-9a-z-]).md" }), + loader: glob({ base: "./public/blog/read", pattern: "+([0-9a-z-]).md" }), schema: Blog, }); -- cgit v1.2.3