Skip to content

Commit

Permalink
$mol_offline cache control
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Feb 2, 2024
1 parent 6103fc1 commit 5ce0d8c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions memo/memo.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

namespace $ {

export class $mol_memo extends $mol_wrapper {
Expand Down
15 changes: 11 additions & 4 deletions offline/offline.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ namespace $ {
'//cse.google.com/adsense/search/async-ads.js'
])

// function is_fresh(res: Response) {
// res.headers.get('ca')
// }

/** Installs service worker proxy, which caches all requests and respond from cache on http errors. */
export function $mol_offline() {

Expand Down Expand Up @@ -46,9 +50,11 @@ namespace $ {
if( request.method !== 'GET' ) return
if( !/^https?:/.test( request.url ) ) return
if( /\?/.test( request.url ) ) return

const fresh = fetch( event.request ).then( response => {
if (request.cache === 'no-store') return

let fresh = null as null | Promise<Response>

const fetch_data = () => fresh = fresh ?? fetch( event.request ).then( response => {
event.waitUntil(
caches.open( '$mol_offline' ).then(
cache => cache.put( event.request , response )
Expand All @@ -57,10 +63,11 @@ namespace $ {

return response.clone()
} )
event.waitUntil( fresh )

if (request.cache !== 'force-cache') event.waitUntil( fetch_data() )

event.respondWith(
caches.match( event.request ).then( response => response || fresh )
caches.match( event.request ).then( response => response || fetch_data() )
)

})
Expand Down

0 comments on commit 5ce0d8c

Please sign in to comment.