8000 feat: offline support with service worker caching by veryard · Pull Request #141 · glzr-io/zebar · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: offline support with service worker caching #141

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

Merged
merged 37 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
15132ba
feat(server): Serve widgets through server
veryard Oct 30, 2024
08343fb
wip: Linux?
veryard Nov 1, 2024
fd1f089
remove setting of user agent
lars-berger Dec 6, 2024
f7432ae
attach token via header in service worker
lars-berger Dec 6, 2024
36e7419
go to init route first then redirect to target
lars-berger Dec 6, 2024
29aed6c
change to use "asset id" for identifying incoming requests
lars-berger Dec 6, 2024
6fb869b
move server-related functions to new `asset_server` mod
lars-berger Dec 6, 2024
c90e495
move initialization script to its own file
lars-berger Dec 6, 2024
6073d2a
feat: add normalize css in initialization script
lars-berger Dec 6, 2024
3c6fe0b
add empty favicon in initialization script
lars-berger Dec 7, 2024
77ea1ce
remove `cache_assets` from config
lars-berger Dec 7, 2024
aa58b93
change localhost port to `6124`
lars-berger Dec 7, 2024
67c5745
parse with params when creating webview url
lars-berger Dec 7, 2024
7d21a74
share cache id for widgets in the same top-level dir
lars-berger Dec 7, 2024
3c620c9
simplify retrieval of widget assets from rocket route
lars-berger Dec 7, 2024
f10668b
simplify `NotFound` errors by returning an `Option`
lars-berger Dec 7, 2024
3d173db
allow service worker to be served from subroute
lars-berger Dec 7, 2024
ad72515
handle opaque responses in sw
lars-berger Dec 8, 2024
a6e2262
prevent directory traversal when serving assets
lars-berger Dec 8, 2024
91e269a
add config options and create form component
lars-berger Dec 8, 2024
9e2d05f
wip custom field for cache duration
lars-berger Dec 8, 2024
8e3a008
working cache duration field
lars-berger Dec 8, 2024
d484cea
form styling
lars-berger Dec 8, 2024
dbbece6
add state route for the sw to fetch from
lars-berger Dec 9, 2024
7d3b109
bump @glzr/components
lars-berger Dec 9, 2024
e18c986
wip passing config to sw
lars-berger Dec 9, 2024
1ffa1c7
successfully set config from initialization script
lars-berger Dec 9, 2024
eda8728
update widget config schema
lars-berger Dec 9, 2024
4776009
wip implementation of cache duration + rules
lars-berger Dec 9, 2024
b9f4f82
store metadata in separate cache
lars-berger Dec 9, 2024
688a54b
fix cache duration
lars-berger Dec 9, 2024
8b016f3
clear sw cache from system tray
lars-berger Dec 9, 2024
0523027
inline script for clearing sw cache
lars-berger Dec 9, 2024
f0badde
share token for widgets in the same directory
lars-berger Dec 10, 2024
fe26e6b
implement `PathExt` for `Path`
lars-berger Dec 10, 2024
9e7202b
move `create_init_url` to `asset_server` mod
lars-berger Dec 10, 2024
c15d322
move out asset server token map to `asset_server` mod
lars-berger Dec 10, 2024
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
386 changes: 386 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/boilerplate-react-buildless/example.zebar.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://github.com/glzr-io/zebar/raw/v2.4.0/resources/widget-schema.json",
"$schema": "https://github.com/glzr-io/zebar/raw/v2.7.0/resources/widget-schema.json",
"htmlPath": "./index.html",
"zOrder": "normal",
"shownInTaskbar": false,
Expand Down
10 changes: 2 additions & 8 deletions examples/boilerplate-react-buildless/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

<!-- Base styles for better consistency across platforms (aka. CSS reset). -->
<link rel="stylesheet" type="text/css" href="../normalize.css" />

<!-- Custom styles. -->
<link rel="stylesheet" type="text/css" href="./styles.css" />

<!-- Add empty favicon to suppress not found errors. -->
<link rel="icon" href="data:;" />

<!-- Allows React to be run buildless via "text/babel" script below. -->
<script
src="https://unpkg.com/@babel/standalone@7.25.6/babel.min.js"
Expand Down Expand Up @@ -52,7 +46,7 @@
return (
<div className="app">
{output.audio?.defaultPlaybackDevice && (
<div class="chip">
<div className="chip">
{output.audio.defaultPlaybackDevice.name}-
{output.audio.defaultPlaybackDevice.volume}
<input
Expand All @@ -67,7 +61,7 @@
/>
</div>
)}
<div class="chip">
<div className="chip">
Media: {output.media?.currentSession?.title}-
{output.media?.currentSession?.artist}
<button => output.media?.togglePlayPause()}>
Expand Down
2 changes: 1 addition & 1 deletion examples/boilerplate-solid-ts/example.zebar.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://github.com/glzr-io/zebar/raw/v2.4.0/resources/widget-schema.json",
"$schema": "https://github.com/glzr-io/zebar/raw/v2.7.0/resources/widget-schema.json",
"htmlPath": "./dist/index.html",
"zOrder": "normal",
"shownInTaskbar": false,
Expand Down
1 change: 0 additions & 1 deletion examples/boilerplate-solid-ts/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<title>Zebar</title>
<link rel="stylesheet" type="text/css" href="../normalize.css" />
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
6 changes: 0 additions & 6 deletions examples/starter/vanilla.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

<!-- Base styles for better consistency across platforms (aka. CSS reset). -->
<link rel="stylesheet" type="text/css" href="../normalize.css" />

<!-- Custom styles. -->
<link rel="stylesheet" type="text/css" href="./styles.css" />

<!-- Add empty favicon to suppress not found errors. -->
<link rel="icon" href="data:;" />

<!-- Allows React to be run buildless via "text/babel" script below. -->
<script
src="https://unpkg.com/@babel/standalone@7.25.6/babel.min.js"
Expand Down
2 changes: 1 addition & 1 deletion examples/starter/vanilla.zebar.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://github.com/glzr-io/zebar/raw/v2.4.0/resources/widget-schema.json",
"$schema": "https://github.com/glzr-io/zebar/raw/v2.7.0/resources/widget-schema.json",
"htmlPath": "./vanilla.html",
"zOrder": "normal",
"shownInTaskbar": false,
Expand Down
6 changes: 0 additions & 6 deletions examples/starter/with-glazewm.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

<!-- Base styles for better consistency across platforms (aka. CSS reset). -->
<link rel="stylesheet" type="text/css" href="../normalize.css" />

<!-- Custom styles. -->
<link rel="stylesheet" type="text/css" href="./styles.css" />

<!-- Add empty favicon to suppress not found errors. -->
<link rel="icon" href="data:;" />

<!-- Allows React to be run buildless via "text/babel" script below. -->
<script
src="https://unpkg.com/@babel/standalone@7.25.6/babel.min.js"
Expand Down
2 changes: 1 addition & 1 deletion examples/starter/with-glazewm.zebar.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://github.com/glzr-io/zebar/raw/v2.4.0/resources/widget-schema.json",
"$schema": "https://github.com/glzr-io/zebar/raw/v2.7.0/resources/widget-schema.json",
"htmlPath": "./with-glazewm.html",
"zOrder": "normal",
"shownInTaskbar": false,
Expand Down
6 changes: 0 additions & 6 deletions examples/starter/with-komorebi.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

<!-- Base styles for better consistency across platforms (aka. CSS reset). -->
<link rel="stylesheet" type="text/css" href="../normalize.css" />

<!-- Custom styles. -->
<link rel="stylesheet" type="text/css" href="./styles.css" />

<!-- Add empty favicon to suppress not found errors. -->
<link rel="icon" href="data:;" />

<!-- Allows React to be run buildless via "text/babel" script below. -->
<script
src="https://unpkg.com/@babel/standalone@7.25.6/babel.min.js"
Expand Down
2 changes: 1 addition & 1 deletion examples/starter/with-komorebi.zebar.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://github.com/glzr-io/zebar/raw/v2.4.0/resources/widget-schema.json",
"$schema": "https://github.com/glzr-io/zebar/raw/v2.7.0/resources/widget-schema.json",
"htmlPath": "./with-komorebi.html",
"zOrder": "normal",
"shownInTaskbar": false,
Expand Down
1 change: 1 addition & 0 deletions packages/client-api/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './monitor-selection';
export * from './dock-config';
export * from './widget-caching';
export * from './widget-config';
export * from './widget-placement';
export * from './widget-preset';
9 changes: 9 additions & 0 deletions packages/client-api/src/config/widget-caching.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type WidgetCaching = {
defaultDuration: number;
rules: WidgetCachingRule[];
};

export type WidgetCachingRule = {
urlRegex: string;
duration: number;
};
2 changes: 2 additions & 0 deletions packages/client-api/src/config/widget-config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { WidgetCaching } from './widget-caching';
import type { WidgetPreset } from './widget-preset';

export type WidgetConfig = {
Expand All @@ -7,5 +8,6 @@ export type WidgetConfig = {
focused: boolean;
resizable: boolean;
transparent: boolean;
caching: WidgetCaching;
presets: WidgetPreset[];
};
14 changes: 8 additions & 6 deletions packages/desktop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ async-trait = "0.1"
base64 = "0.22"
clap = { version = "4", features = ["derive"] }
crossbeam = "0.8"
netdev = "0.24"
regex = "1"
reqwest = { version = "0.11", features = ["json"] }
rocket = { version = "0.5", features = ["json"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
starship-battery = "0.8"
sysinfo = "0.30"
tauri = { version = "2.0", features = [
"devtools",
"macos-private-api",
Expand All @@ -29,15 +36,10 @@ tauri-plugin-dialog = "2.0"
tauri-plugin-http = "2.0"
tauri-plugin-shell = "2.0"
tauri-plugin-single-instance = "2.0"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
starship-battery = "0.8"
sysinfo = "0.30"
tokio = { version = "1.33", features = ["full"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
netdev = "0.24"
regex = "1"
uuid = { version = "1", features = ["v4"] }

[target.'cfg(target_os = "windows")'.dependencies]
komorebi-client = { git = "https://github.com/LGUG2Z/komorebi", tag = "v0.1.28" }
Expand Down
6 changes: 5 additions & 1 deletion packages/desktop/capabilities/widget.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
"description": "Widget capabilities",
"windows": ["widget-*"],
"remote": {
"urls": ["http://asset.localhost", "asset://localhost"]
"urls": [
"http://asset.localhost",
"asset://localhost",
"http://127.0.0.1:6124"
]
},
"permissions": [
"core:app:default",
Expand Down
67 changes: 67 additions & 0 deletions packages/desktop/resources/initialization-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
if (window.location.host === '127.0.0.1:6124') {
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('/__zebar/sw.js', { scope: '/' })
.then(sw => {
console.log('[Zebar] Service Worker registered.');

const message = {
type: 'SET_CONFIG',
config: window.__ZEBAR_STATE.config.caching,
};

sw.active?.postMessage(message);
sw.installing?.postMessage(message);
sw.waiting?.postMessage(message);
})
.catch(err =>
console.error('[Zebar] Service Worker failed to register:', err),
);
}

document.addEventListener('DOMContentLoaded', () => {
addFavicon();
loadCss('/__zebar/normalize.css');
});
}

/**
* Adds a CSS file with the given path to the head element.
*/
function loadCss(path) {
const link = document.createElement('link');
link.setAttribute('data-zebar', '');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = path;
insertIntoHead(link);
}

/**
* Adds a favicon to the head element if one is not already present.
*/
function addFavicon() {
if (!document.querySelector('link[rel="icon"]')) {
const link = document.createElement('link');
link.setAttribute('data-zebar', '');
link.rel = 'icon';
link.href = 'data:;';
insertIntoHead(link);
}
}

/**
* Inserts the element before any other resource tags in the head element.
* Ensures that user-defined stylesheets or favicons are prioritized over
* Zebar's defaults.
*/
function insertIntoHead(element) {
const resources = document.head.querySelectorAll('link, script, style');
const target = resources[0]?.previousElementSibling;

if (target) {
target.after(element);
} else {
document.head.appendChild(element);
}
}
File renamed without changes.
Loading
Loading
0