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 { 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,
|
||||||
|
|
|
@ -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]
|
||||||
|
|
|
@ -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,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -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(
|
||||||
|
|
|
@ -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,
|
||||||
})
|
})
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -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[]
|
||||||
|
|
Loading…
Reference in a new issue