feat: update type names and make serviceWorker options optional)
This commit is contained in:
parent
2ae36ffb81
commit
6e64d75b1c
7 changed files with 20 additions and 15 deletions
|
@ -7,7 +7,7 @@ import { readFile, writeFile, readdir, unlink } from 'node:fs/promises'
|
|||
import { fileURLToPath } from 'node:url'
|
||||
import { resolve, dirname, join } from 'node:path'
|
||||
import { build } from 'esbuild'
|
||||
import type { Config } from './types'
|
||||
import type { AstroServiceWorkerConfig } from './types'
|
||||
import type { AstroIntegration } from 'astro'
|
||||
|
||||
const ASTROSW = '@ayco/astro-sw'
|
||||
|
@ -16,7 +16,9 @@ const ASTROSW = '@ayco/astro-sw'
|
|||
* Accepts configuration options with service worker path
|
||||
* 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 {
|
||||
presets,
|
||||
assetCachePrefix = ASTROSW,
|
||||
|
|
|
@ -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,
|
||||
}) => {
|
||||
const allowCacheNames = [cacheName]
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { ServiceWorkerPreset } from '../../types'
|
||||
import { AstroServiceWorkerPreset } from '../../types'
|
||||
import activate from './activate'
|
||||
|
||||
export const deleteOldCaches: () => ServiceWorkerPreset = () => ({
|
||||
export const deleteOldCaches: () => AstroServiceWorkerPreset = () => ({
|
||||
activate,
|
||||
})
|
||||
|
||||
|
|
|
@ -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 })
|
||||
|
||||
event.respondWith(
|
||||
|
|
|
@ -2,11 +2,11 @@
|
|||
* preset for stale-while-revalidate caching strategy
|
||||
*/
|
||||
|
||||
import { ServiceWorkerPreset } from '../../types'
|
||||
import { AstroServiceWorkerPreset } from '../../types'
|
||||
import install from './install'
|
||||
import fetch from './fetch'
|
||||
|
||||
export const staleWhileRevalidate: () => ServiceWorkerPreset = () => ({
|
||||
export const staleWhileRevalidate: () => AstroServiceWorkerPreset = () => ({
|
||||
install,
|
||||
fetch,
|
||||
})
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import { ServiceWorkerPreset } from '../../types'
|
||||
import { AstroServiceWorkerPreset } from '../../types'
|
||||
|
||||
declare const self: ServiceWorkerGlobalScope
|
||||
|
||||
export const installFn: ServiceWorkerPreset['install'] = ({
|
||||
export const installFn: AstroServiceWorkerPreset['install'] = ({
|
||||
event,
|
||||
routes,
|
||||
cacheName,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import type { BuildOptions } from 'esbuild'
|
||||
|
||||
export type ServiceWorkerPreset = {
|
||||
export type AstroServiceWorkerPreset = {
|
||||
activate?: (options: { event: ExtendableEvent; cacheName: string }) => void
|
||||
install?: (options: {
|
||||
event: ExtendableEvent
|
||||
|
@ -10,9 +10,9 @@ export type ServiceWorkerPreset = {
|
|||
fetch?: (options: { event: FetchEvent; cacheName: string }) => void
|
||||
}
|
||||
|
||||
export type Config = {
|
||||
export type AstroServiceWorkerConfig = {
|
||||
path?: string
|
||||
presets?: ServiceWorkerPreset[]
|
||||
presets?: AstroServiceWorkerPreset[]
|
||||
assetCachePrefix?: string
|
||||
assetCacheVersionID?: string
|
||||
customRoutes?: string[]
|
||||
|
|
Loading…
Reference in a new issue