diff --git a/offline/offline.ts b/offline/offline.ts index e646ad56ea..7f7ba4a375 100644 --- a/offline/offline.ts +++ b/offline/offline.ts @@ -21,10 +21,16 @@ namespace $.$mol_service { return this.respond(request) } + protected static fetch(request: Request): Promise { + throw new Error('implement') + } + + static force_cache_regexp = /.+\/(index|test)\.html/ + protected static async respond(request: Request) { let fallback_header - const index_html = /.+\/index\.html/.test(request.url) + const index_html = this.force_cache_regexp.test(request.url) const cache = request.cache @@ -36,7 +42,7 @@ namespace $.$mol_service { // fetch with fallback to cache if statuses not match try { - const actual = await fetch(request) + const actual = await this.fetch(request) if (actual.status < 400) return actual throw new Error( @@ -54,7 +60,7 @@ namespace $.$mol_service { request = new Request(request, { cache: 'force-cache' }) } - const cached = await fetch(request) + const cached = await this.fetch(request) if (! fallback_header || cached.headers.get('$mol_offline_remote_status')) return cached diff --git a/offline/offline.web.ts b/offline/offline.web.ts new file mode 100644 index 0000000000..41d4967087 --- /dev/null +++ b/offline/offline.web.ts @@ -0,0 +1,9 @@ +namespace $.$mol_service { + export class $mol_offline_web extends $mol_offline { + protected static override fetch(request: Request) { + return fetch(request) + } + } + + $.$mol_service.$mol_offline = $mol_offline_web +} diff --git a/service/host/host.web.ts b/service/host/host.web.ts index 1ff0f99434..21cb76c056 100644 --- a/service/host/host.web.ts +++ b/service/host/host.web.ts @@ -165,8 +165,10 @@ namespace $ { } } + const waitUntil = event.waitUntil.bind(event) + for (const plugin of this.plugins) { - const response = plugin.modify(request) + const response = plugin.modify(request, waitUntil) if (response) return event.respondWith(response) } } diff --git a/service/plugin/plugin.ts b/service/plugin/plugin.ts index 1d48b99bfe..39009d30ef 100644 --- a/service/plugin/plugin.ts +++ b/service/plugin/plugin.ts @@ -7,7 +7,11 @@ namespace $ { static state_change() {} static message_data(data: {}) { return null as null | undefined | Promise } - static blocked(res: Request) { return false } - static modify(res: Request) { return null as null | Response | PromiseLike } + static blocked(request: Request) { return false } + static modify(request: Request, waitUntil: (promise: Promise) => void) { + return null as null | Response | PromiseLike + } + + static service() { return this.$.$mol_service_host } } }