Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$mol_offline refactor, disable cache on page reload via dev server #710

Open
wants to merge 65 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 54 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
b22b11a
$mol_offline refactor, disable cache on page reload via dev server
zerkalica Oct 25, 2024
9d5f058
$mol_offline refactor
zerkalica Oct 25, 2024
ce606ba
$mol_offline refactor 2 - simplify respond
zerkalica Oct 25, 2024
ca3be5d
$mol_offline refactor 3 - simplify respond
zerkalica Oct 25, 2024
f965597
$mol_offline refactor 4 - extracted send
zerkalica Oct 25, 2024
81a9ac6
$mol_offline refactor
zerkalica Oct 25, 2024
c83d213
$mol_offline mol_build_obsolete message pass to worker to disable cache
zerkalica Oct 25, 2024
9f3722b
$mol_offline review fixes
zerkalica Oct 26, 2024
84c0253
$mol_offline review fixes 2
zerkalica Oct 26, 2024
c476bb3
$mol_offline fixes 3
zerkalica Oct 26, 2024
f8eab6a
$mol_offline refactor, extract $mol_worker
zerkalica Oct 26, 2024
f17f33b
$mol_offline fix refresh
zerkalica Oct 26, 2024
6efe454
$mol_offline use force-cache
zerkalica Oct 26, 2024
5864255
$mol_offline rules refactored, some bugs fixed
zerkalica Oct 27, 2024
201dcb2
$mol_offline configure cached and blocked urls regexp
zerkalica Oct 27, 2024
cd84bfa
$mol_offline fix regexp
zerkalica Oct 27, 2024
958e207
$mol_worker_service plugins, refactor
zerkalica Oct 28, 2024
2dcd5aa
$mol_offline save ignore_cache between window.reload()
zerkalica Oct 28, 2024
08aa70c
$mol_offline fix test.html caching
zerkalica Oct 28, 2024
3593a0a
$mol_worker refactor
zerkalica Oct 29, 2024
e25b677
$mol_worker fix worker singletone
zerkalica Oct 29, 2024
db14037
$mol_worker fix try/catch in attach
zerkalica Oct 29, 2024
bb46b57
$mol_worker_service renamed to $mol_service, $mol_offline refactor to…
zerkalica Oct 29, 2024
121c90e
$mol_service fix init hook
zerkalica Oct 29, 2024
9f86a71
$mol_service refactor extracted fetch service and notify
zerkalica Oct 29, 2024
7d3ce8a
$mol_fetch_service fix bug with fetch_event binded scope
zerkalica Oct 29, 2024
b168b72
$mol_fetch_service refactor simplify add logic
zerkalica Oct 29, 2024
354468b
$mol_service_plugin extracted, used static, fetch hook moved back to …
zerkalica Oct 30, 2024
7a0cf1e
$mol_service refactor detach method, auto init
zerkalica Oct 30, 2024
d53c252
$mol_service_plugin namespace
zerkalica Oct 30, 2024
21da91e
$mol_service_plugin remove plugin filter, simplify
zerkalica Oct 30, 2024
cbcef3f
$mol_service_plugin moved to $ namespace
zerkalica Oct 30, 2024
ea7d089
$mol_build_client removed unused postMessage
zerkalica Oct 30, 2024
02d8dcc
$mol_service_plugin - include mol_service, add waitUntil to modify, $…
zerkalica Oct 30, 2024
7f7ce8d
$mol_service fix inner namespace
zerkalica Oct 30, 2024
4b10be0
$mol_service_host refactor, better update handling api
zerkalica Oct 31, 2024
d12f71f
$mol_service review fixes
zerkalica Nov 1, 2024
a202b1b
$mol_offline regexp to URL parser
zerkalica Nov 1, 2024
61fb6db
$mol_service separate worker plugins, fixes
zerkalica Nov 1, 2024
5b68802
$mol_service moved to wires, better errors
zerkalica Nov 2, 2024
00dcb49
$mol_service refactor
zerkalica Nov 2, 2024
33b1fc6
$mol_service refactor
zerkalica Nov 2, 2024
3ee7e45
$mol_worker async
zerkalica Nov 2, 2024
6fabdac
$mol_offline refactoring
zerkalica Nov 3, 2024
10b0cef
$mol_service push event handler added, sync scope
zerkalica Nov 3, 2024
1efed9b
$mol_service_worker refactor, extracted prompt events
zerkalica Nov 3, 2024
7f008e6
$mol_service_worker type fix
zerkalica Nov 3, 2024
2169769
$mol_service_worker simple notify
zerkalica Nov 3, 2024
2265c73
$mol_service_prompt moved to offline, refactor
zerkalica Nov 3, 2024
9dafc68
$mol_offline web
zerkalica Nov 3, 2024
7778411
$mol_service_worker fix start
zerkalica Nov 4, 2024
a2aa4c3
$mol_offline fix prompt offline
zerkalica Nov 4, 2024
b13f25e
Merge branch 'master' of github.com:hyoo-ru/mam_mol into offline2
zerkalica Nov 4, 2024
6715f91
$mol_service_worker minor fixes
zerkalica Nov 5, 2024
001e562
$mol_fetch removed unused request
zerkalica Nov 5, 2024
60811c1
$mol_service refactor, split self and worker
zerkalica Nov 6, 2024
d579f8a
$mol_service fixes
zerkalica Nov 6, 2024
370372a
$mol_service fixes 2
zerkalica Nov 6, 2024
a33dcaf
$mol_worker rpc refactored
zerkalica Nov 6, 2024
7849b8a
$mol_notify fixes
zerkalica Nov 6, 2024
854046d
$mol_rpc_client fix call bug
zerkalica Nov 7, 2024
667e459
Merge branch 'master' of github.com:hyoo-ru/mam_mol into offline2
zerkalica Dec 29, 2024
e995539
Merge branch 'master' of github.com:hyoo-ru/mam_mol into offline2
zerkalica Jan 3, 2025
48fe3a1
$mol_offline add require-corp header
zerkalica Jan 3, 2025
3cb5e0f
$mol_file_node fix error handler
zerkalica Jan 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fail/catch/catch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace $ {

const catched = new WeakMap< any , boolean >()

export function $mol_fail_catch( error: unknown ) {
export function $mol_fail_catch( error: unknown ): error is Error {

if( typeof error !== 'object' ) return false
if( $mol_promise_like( error ) ) $mol_fail_hidden( error )
Expand Down
9 changes: 9 additions & 0 deletions fetch/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ namespace $ {
super()
}

@ $mol_action
clone() {
return new this.$.$mol_fetch_response(new Response(this.native.body, this.native))
}

status() {
const types = [ 'unknown', 'inform', 'success', 'redirect', 'wrong', 'failed' ] as const
return types[ Math.floor( this.native.status / 100 ) ]
Expand Down Expand Up @@ -75,6 +80,10 @@ namespace $ {
}

}
export function $mol_fetch_request_clone(this: typeof $, original: Request, options?: RequestInit) {
return new Request(original, options)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Хотя, сделать и над реквестом обёртку возможно не плохая идея. Есть ил ещё какая полезная функциональность для реквестов помимо клонирования?

$.$mol_fetch_request_clone = $mol_wire_sync($).$mol_fetch_request_clone
zerkalica marked this conversation as resolved.
Show resolved Hide resolved

export class $mol_fetch extends $mol_object2 {

Expand Down
1 change: 0 additions & 1 deletion notify/notify.meta.tree

This file was deleted.

20 changes: 0 additions & 20 deletions notify/notify.node.ts

This file was deleted.

39 changes: 39 additions & 0 deletions notify/notify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace $ {

export type $mol_notify_info = {
context: string,
message: string,
uri: string
}

export class $mol_notify extends $mol_object {

@ $mol_mem
static allowed( next?: boolean ) {
return false
}

static show( info: $mol_notify_info ) {
this.$.$mol_service_worker.send(info)
zerkalica marked this conversation as resolved.
Show resolved Hide resolved
}

}

export class $mol_notify_service extends $mol_service_plugin_notify {
static override data(data: {}) {
if ('uri' in data && 'message' in data) {
this.show(data as $mol_notify_info)
return true
}
return null
}

static show(info: $mol_notify_info) {}

}

export namespace $mol_service_plugin {
export let $mol_notify_service = $.$mol_notify_service
}

}
77 changes: 33 additions & 44 deletions notify/notify.web.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
namespace $ {

/** Manages system notifications. Notifications of same context are auto joined to one notification. */
export class $mol_notify {
export class $mol_notify_web extends $mol_notify {

@ $mol_mem
static allowed( next?: boolean ) {
static override allowed( next?: boolean ) {

let perm = Notification.permission

let perm = this.$.$mol_dom_context.Notification.permission
if( next === undefined ) return perm === 'granted'

if( perm === 'granted' ) return true
Expand All @@ -16,33 +17,23 @@ namespace $ {
return perm === 'granted'
}

static async request_permissions() {
static request_permissions() {
return new Promise< NotificationPermission >( done =>
Notification.requestPermission( perm => {
this.$.$mol_dom_context.Notification.requestPermission( perm => {
done( perm )
} )
)
}

@ $mol_action
static show( info: {
context: string,
message: string,
uri: string
} ) {
navigator.serviceWorker.controller!.postMessage( info )
}

}
if( typeof window === 'undefined' ) {
self.addEventListener( 'message', async event => {
let { context: title, message: body, uri: data } = event.data

$.$mol_notify = $mol_notify_web

export class $mol_notify_service_web extends $mol_notify_service {
static override show({ context: title, message: body, uri: data }: $mol_notify_info) {
const registration = this.$.$mol_service_worker_web.registration().native()
const tag = data

const existen = await $mol_service().getNotifications({ tag })
const existen = registration.getNotifications({ tag })

for( const not of existen ) {

Expand All @@ -54,29 +45,27 @@ namespace $ {

// const vibrate = [ 100, 200, 300, 400, 500 ]

await $mol_service().showNotification( title, { body, data, /*vibrate,*/ tag } )

} )

self.addEventListener( 'notificationclick', $mol_service_handler( async ( event: any )=> {

const clients: any[] = await ( self as any ).clients.matchAll({ includeUncontrolled: true })
event.notification.close()
registration.showNotification( title, { body, data, /*vibrate,*/ tag } )

if( clients.length ) {

const last = clients[ clients.length - 1 ]
await last.focus()
await last.navigate( event.notification.data )

} else {

await ( self as any ).clients.openWindow( event.notification.data )

}

static override notification( notification: Notification ) {
const matched = this.$.$mol_service_worker_web.clients_grab({ includeUncontrolled: true, type: 'window' })
const last = matched.at(-1)

if( last ) {
last.focus()
last.navigate( notification.data )

return null
}

} ) )


this.$.$mol_service_worker_web.window_open( notification.data )
return null
}
}


$.$mol_notify_service = $mol_notify_service_web

$mol_service_plugin.$mol_notify_service = $mol_notify_service_web
}
6 changes: 1 addition & 5 deletions offline/install/install.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
namespace $ {
try {
$mol_offline()
} catch( error ) {
console.error( error )
}
$mol_offline
}
66 changes: 65 additions & 1 deletion offline/offline.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,69 @@
namespace $ {

export function $mol_offline( ) {}
export class $mol_offline extends $mol_service_plugin_cache {
static blocked_urls = [
'//cse.google.com/adsense/search/async-ads.js'
]

static override blocked( request: Request ) {
const normalized_url = request.url.replace( /^https?:/, '' )

return this.blocked_urls.includes(normalized_url)
}

static override activate() {
this.$.$mol_service_worker.claim()
}

static override need_modify(request: Request) {
if( request.method !== 'GET' ) return false
if( !/^https?:/.test( request.url ) ) return false
if( /\?/.test( request.url ) ) return false
if( request.cache === 'no-store' ) return false

return true
}

static override modify(request: Request) {
let fallback_header

const html = request.mode === 'navigate'
const cache = request.cache

if (cache === 'reload' || ( cache === 'no-cache' && ! html ) ) {
if (cache === 'reload') {
// F5 + Disable cache
request = new ($mol_wire_sync(Request))(request, { cache: 'no-cache' })
}

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

fallback_header = actual.message()
} catch (err) {
if ( $mol_promise_like(err) ) $mol_fail_hidden(err)
fallback_header = (err as Error).message || 'Fetch error'
}
}

if (cache !== 'force-cache') {
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`)

return clone.native
}
}

export namespace $mol_service_plugin {
export let $mol_offline = $.$mol_offline
}
}
Loading
Loading