Skip to content

Commit

Permalink
$mol_offline add require-corp header
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Jan 3, 2025
1 parent e995539 commit 48fe3a1
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions offline/offline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,29 @@ namespace $ {
return true
}

protected static fetch(request: Request, fallback_header?: string) {
const raw = this.$.$mol_fetch.response(request).native
if (raw.status < 400) return raw
if (! this.corp() && ! fallback_header) return raw

const response = raw.clone()
const headers = response.headers

if ( this.corp() ) {
headers.set( 'Cross-Origin-Embedder-Policy', 'require-corp' )
headers.set( 'Cross-Origin-Opener-Policy', 'same-origin' )
}

if (fallback_header) {
headers.set( '$mol_offline_remote_status', `${fallback_header} $mol_offline fallback to cache`)
}

return response

}

static corp() { return true }

static override modify(request: Request) {
let fallback_header

Expand All @@ -38,10 +61,11 @@ namespace $ {

// fetch with fallback to cache if statuses not match
try {
const actual = this.$.$mol_fetch.response(request)
if (actual.code() < 400) return actual.native
const response = this.fetch(request)

fallback_header = actual.message()
if (response.status < 400) return response

fallback_header = response.statusText || `HTTP Error ${ response.status }`
} catch (err) {
if ( $mol_promise_like(err) ) $mol_fail_hidden(err)
fallback_header = (err as Error).message || 'Fetch error'
Expand All @@ -52,14 +76,9 @@ namespace $ {
request = new ($mol_wire_sync(Request))(request, { cache: 'force-cache' })
}

const cached = this.$.$mol_fetch.response(request)

if (! fallback_header ) return cached.native

const clone = cached.clone()
clone.headers().set( '$mol_offline_remote_status', `${fallback_header} $mol_offline fallback to cache`)
const cached = this.fetch(request, fallback_header)

return clone.native
return cached
}
}

Expand Down

0 comments on commit 48fe3a1

Please sign in to comment.