feat: use @ayco/astro-sw esbuild minify

This commit is contained in:
Ayo Ayco 2024-08-17 20:57:52 +02:00
parent 7de0df94b5
commit 5e85834637
4 changed files with 15 additions and 15 deletions

View file

@ -1,6 +1,5 @@
import { defineConfig } from 'astro/config'; import { defineConfig } from 'astro/config';
import serviceWorker from '@ayco/astro-sw'; import serviceWorker from '@ayco/astro-sw';
import sitemap from "@astrojs/sitemap"; import sitemap from "@astrojs/sitemap";
// https://astro.build/config // https://astro.build/config
@ -9,11 +8,14 @@ export default defineConfig({
integrations: [ integrations: [
sitemap(), sitemap(),
serviceWorker({ serviceWorker({
path: './src/sw.js', path: './src/sw.ts',
assetCachePrefix: 'ayco-personal-site', assetCachePrefix: 'ayco-personal-site',
excludeRoutes: [ excludeRoutes: [
'/threads' '/threads'
] ],
esbuild: {
minify: true
}
}) })
] ]
}); });

View file

@ -17,7 +17,7 @@
"@astro-reactive/form": "^0.10.0", "@astro-reactive/form": "^0.10.0",
"@astro-reactive/validator": "^0.5.0", "@astro-reactive/validator": "^0.5.0",
"@astrojs/sitemap": "^3.1.6", "@astrojs/sitemap": "^3.1.6",
"@ayco/astro-sw": "^0.7.8", "@ayco/astro-sw": "^0.7.9",
"astro": "^4.14.2", "astro": "^4.14.2",
"astro-github-stats": "^0.7.0", "astro-github-stats": "^0.7.0",
"astro-iconify": "^1.2.0" "astro-iconify": "^1.2.0"

View file

@ -18,8 +18,8 @@ importers:
specifier: ^3.1.6 specifier: ^3.1.6
version: 3.1.6 version: 3.1.6
'@ayco/astro-sw': '@ayco/astro-sw':
specifier: ^0.7.8 specifier: ^0.7.9
version: 0.7.8 version: 0.7.9
astro: astro:
specifier: ^4.14.2 specifier: ^4.14.2
version: 4.14.2(@types/node@22.4.0)(rollup@4.20.0)(typescript@5.5.4) version: 4.14.2(@types/node@22.4.0)(rollup@4.20.0)(typescript@5.5.4)
@ -75,8 +75,8 @@ packages:
resolution: {integrity: sha512-/ca/+D8MIKEC8/A9cSaPUqQNZm+Es/ZinRv0ZAzvu2ios7POQSsVD+VOj7/hypWNsNM3T7RpfgNq7H2TU1KEHA==} resolution: {integrity: sha512-/ca/+D8MIKEC8/A9cSaPUqQNZm+Es/ZinRv0ZAzvu2ios7POQSsVD+VOj7/hypWNsNM3T7RpfgNq7H2TU1KEHA==}
engines: {node: ^18.17.1 || ^20.3.0 || >=21.0.0} engines: {node: ^18.17.1 || ^20.3.0 || >=21.0.0}
'@ayco/astro-sw@0.7.8': '@ayco/astro-sw@0.7.9':
resolution: {integrity: sha512-NA3m2YXNReZQla3tv98rJ89HfftmEC+4dsX/bG+NIIlFVcnGTxvwPdhTZ3vD5K/NqVyo7wr4n132k4dCtKUWvw==} resolution: {integrity: sha512-hVdRdZz+IJvmg/qpWHX52dK0Pnp8ErsMy1lYcs7g3rueLDS+r5JdFwdfs3GgU5DyDBfBg/VJZ7dWETYS+sUrYw==}
engines: {node: '>=18.0.0'} engines: {node: '>=18.0.0'}
'@babel/code-frame@7.24.7': '@babel/code-frame@7.24.7':
@ -2270,7 +2270,7 @@ snapshots:
transitivePeerDependencies: transitivePeerDependencies:
- supports-color - supports-color
'@ayco/astro-sw@0.7.8': '@ayco/astro-sw@0.7.9':
dependencies: dependencies:
esbuild: 0.23.1 esbuild: 0.23.1
pathe: 1.1.2 pathe: 1.1.2

View file

@ -5,13 +5,13 @@
*/ */
const cacheName = `${__prefix ?? 'app'}-v${__version ?? '000'}` const cacheName = `${__prefix ?? 'app'}-v${__version ?? '000'}`
const addResourcesToCache = async (resources) => { const addResourcesToCache = async (resources: string[]) => {
const cache = await caches.open(cacheName); const cache = await caches.open(cacheName);
console.log('[personal-sw]: adding resources to cache...', resources) console.log('[personal-sw]: adding resources to cache...', resources)
await cache.addAll(new Set([...resources])); await cache.addAll(new Set([...resources]));
}; };
const putInCache = async (request, response) => { const putInCache = async (request: Request, response: Response) => {
const cache = await caches.open(cacheName); const cache = await caches.open(cacheName);
console.log('[personal-sw]: adding one response to cache...', request.url) console.log('[personal-sw]: adding one response to cache...', request.url)
// if exists, replace // if exists, replace
@ -25,7 +25,7 @@ const putInCache = async (request, response) => {
}; };
const cacheAndRevalidate = async ({ request, preloadResponsePromise, fallbackUrl }) => { const cacheAndRevalidate = async ({ request, preloadResponsePromise, fallbackUrl }: {request: Request, preloadResponsePromise: Promise<any>, fallbackUrl: string}) => {
const cache = await caches.open(cacheName); const cache = await caches.open(cacheName);
@ -111,9 +111,7 @@ self.addEventListener('activate', (event) => {
self.addEventListener('install', (event) => { self.addEventListener('install', (event) => {
console.log('[personal-sw]: installing...', event) console.log('[personal-sw]: installing...', event)
event.waitUntil( event.waitUntil(
addResourcesToCache([ addResourcesToCache(__assets ?? [])
...(__assets ?? [])
])
); );
self.skipWaiting(); // activate updated SW self.skipWaiting(); // activate updated SW
}); });