feat: update type names and make serviceWorker options optional)

This commit is contained in:
Ayo Ayco 2025-04-07 23:48:39 +01:00
parent 2ae36ffb81
commit 6e64d75b1c
7 changed files with 20 additions and 15 deletions

View file

@ -7,7 +7,7 @@ import { readFile, writeFile, readdir, unlink } from 'node:fs/promises'
import { fileURLToPath } from 'node:url' import { fileURLToPath } from 'node:url'
import { resolve, dirname, join } from 'node:path' import { resolve, dirname, join } from 'node:path'
import { build } from 'esbuild' import { build } from 'esbuild'
import type { Config } from './types' import type { AstroServiceWorkerConfig } from './types'
import type { AstroIntegration } from 'astro' import type { AstroIntegration } from 'astro'
const ASTROSW = '@ayco/astro-sw' const ASTROSW = '@ayco/astro-sw'
@ -16,7 +16,9 @@ const ASTROSW = '@ayco/astro-sw'
* Accepts configuration options with service worker path * Accepts configuration options with service worker path
* and injects needed variables such as `__assets` generated by Astro * and injects needed variables such as `__assets` generated by Astro
*/ */
export default function serviceWorker(options: Config): AstroIntegration { export default function serviceWorker(
options?: AstroServiceWorkerConfig
): AstroIntegration {
const { const {
presets, presets,
assetCachePrefix = ASTROSW, assetCachePrefix = ASTROSW,

View file

@ -1,6 +1,6 @@
import { ServiceWorkerPreset } from '../../types' import { AstroServiceWorkerPreset } from '../../types'
export const activateFn: ServiceWorkerPreset['activate'] = async ({ export const activateFn: AstroServiceWorkerPreset['activate'] = async ({
cacheName, cacheName,
}) => { }) => {
const allowCacheNames = [cacheName] const allowCacheNames = [cacheName]

View file

@ -1,7 +1,7 @@
import { ServiceWorkerPreset } from '../../types' import { AstroServiceWorkerPreset } from '../../types'
import activate from './activate' import activate from './activate'
export const deleteOldCaches: () => ServiceWorkerPreset = () => ({ export const deleteOldCaches: () => AstroServiceWorkerPreset = () => ({
activate, activate,
}) })

View file

@ -1,6 +1,9 @@
import { ServiceWorkerPreset } from '../../types' import { AstroServiceWorkerPreset } from '../../types'
export const fetchFn: ServiceWorkerPreset['fetch'] = ({ event, cacheName }) => { export const fetchFn: AstroServiceWorkerPreset['fetch'] = ({
event,
cacheName,
}) => {
console.info('fetch happened', { data: event }) console.info('fetch happened', { data: event })
event.respondWith( event.respondWith(

View file

@ -2,11 +2,11 @@
* preset for stale-while-revalidate caching strategy * preset for stale-while-revalidate caching strategy
*/ */
import { ServiceWorkerPreset } from '../../types' import { AstroServiceWorkerPreset } from '../../types'
import install from './install' import install from './install'
import fetch from './fetch' import fetch from './fetch'
export const staleWhileRevalidate: () => ServiceWorkerPreset = () => ({ export const staleWhileRevalidate: () => AstroServiceWorkerPreset = () => ({
install, install,
fetch, fetch,
}) })

View file

@ -1,8 +1,8 @@
import { ServiceWorkerPreset } from '../../types' import { AstroServiceWorkerPreset } from '../../types'
declare const self: ServiceWorkerGlobalScope declare const self: ServiceWorkerGlobalScope
export const installFn: ServiceWorkerPreset['install'] = ({ export const installFn: AstroServiceWorkerPreset['install'] = ({
event, event,
routes, routes,
cacheName, cacheName,

View file

@ -1,6 +1,6 @@
import type { BuildOptions } from 'esbuild' import type { BuildOptions } from 'esbuild'
export type ServiceWorkerPreset = { export type AstroServiceWorkerPreset = {
activate?: (options: { event: ExtendableEvent; cacheName: string }) => void activate?: (options: { event: ExtendableEvent; cacheName: string }) => void
install?: (options: { install?: (options: {
event: ExtendableEvent event: ExtendableEvent
@ -10,9 +10,9 @@ export type ServiceWorkerPreset = {
fetch?: (options: { event: FetchEvent; cacheName: string }) => void fetch?: (options: { event: FetchEvent; cacheName: string }) => void
} }
export type Config = { export type AstroServiceWorkerConfig = {
path?: string path?: string
presets?: ServiceWorkerPreset[] presets?: AstroServiceWorkerPreset[]
assetCachePrefix?: string assetCachePrefix?: string
assetCacheVersionID?: string assetCacheVersionID?: string
customRoutes?: string[] customRoutes?: string[]