diff options
| -rw-r--r-- | src/routes/api/session/is-online.zig | 15 | ||||
| -rw-r--r-- | src/routes/api/session/root.zig | 1 | ||||
| -rw-r--r-- | static/api/session.js | 9 | ||||
| -rw-r--r-- | static/index.html | 6 | ||||
| -rw-r--r-- | static/index.js | 10 | ||||
| -rw-r--r-- | static/pages/index.js | 5 | ||||
| -rw-r--r-- | static/pages/login/index.js | 3 | ||||
| -rw-r--r-- | static/pages/main/index.js | 3 | ||||
| -rw-r--r-- | static/pages/settings/index.js | 6 | ||||
| -rw-r--r-- | static/service-worker/worker.js | 51 | ||||
| -rw-r--r-- | static/widgets/editable/index.js | 3 | ||||
| -rw-r--r-- | static/widgets/image/index.js | 3 | ||||
| -rw-r--r-- | static/widgets/index.js | 6 | ||||
| -rw-r--r-- | static/widgets/month-select/index.js | 3 | ||||
| -rw-r--r-- | static/widgets/password-dialog/index.js | 6 | ||||
| -rw-r--r-- | static/widgets/search/index.js | 3 |
16 files changed, 95 insertions, 38 deletions
diff --git a/src/routes/api/session/is-online.zig b/src/routes/api/session/is-online.zig new file mode 100644 index 0000000..f5ebaee --- /dev/null +++ b/src/routes/api/session/is-online.zig @@ -0,0 +1,15 @@ +const std = @import("std"); + +const memora = @import("memora"); +const Context = memora.Context; + +pub const access = .everyone; + +const Result = struct { + is_online: bool, +}; + +pub fn get(ctx: *Context) !Result { + _ = ctx; + return .{ .is_online = true }; +} diff --git a/src/routes/api/session/root.zig b/src/routes/api/session/root.zig index 0b85e20..c8c2223 100644 --- a/src/routes/api/session/root.zig +++ b/src/routes/api/session/root.zig @@ -4,3 +4,4 @@ pub const current: HandlerInfo = .from_type(@import("current.zig")); pub const drop: HandlerInfo = .from_type(@import("renew.zig")); pub const renew: HandlerInfo = .from_type(@import("drop.zig")); pub const @"is-valid": HandlerInfo = .from_type(@import("is-valid.zig")); +pub const @"is-online": HandlerInfo = .from_type(@import("is-online.zig")); diff --git a/static/api/session.js b/static/api/session.js index a5f9c78..870709f 100644 --- a/static/api/session.js +++ b/static/api/session.js @@ -8,3 +8,12 @@ export async function is_valid() { const result = await rest.get('/api/session/is-valid'); return result.is_valid; } + +export async function is_online() { + try { + const result = await rest.get('/api/session/is-online'); + return result.is_online; + } catch { + return false; + } +} diff --git a/static/index.html b/static/index.html index b3f609d..cf0a613 100644 --- a/static/index.html +++ b/static/index.html @@ -5,7 +5,11 @@ <script type="importmap"> { "imports": { - "sfw": "./sfw/index.js" + "sfw": "./sfw/index.js", + "api": "./api/index.js", + "icons": "./icons/index.js", + "widgets": "./widgets/index.js", + "pages": "./pages/index.js" } } </script> diff --git a/static/index.js b/static/index.js index 28c6228..c5bfccc 100644 --- a/static/index.js +++ b/static/index.js @@ -1,10 +1,8 @@ import * as sfw from 'sfw'; -import * as api from './api/index.js'; +import * as api from 'api'; import { literal as m } from './month.js'; import * as service_worker from './service-worker/index.js'; -//service_worker.register(); - import LoginView from './pages/login/index.js'; import MainView from './pages/main/index.js'; import ImageViewer from './pages/image-viewer/index.js'; @@ -124,8 +122,10 @@ document.body.append( upload_bar, ); - -if (await api.session.is_valid()) { +if (!await api.session.is_online()) { + login.hide(); + reload(); +} if (await api.session.is_valid()) { login.hide(); reload(); } else { diff --git a/static/pages/index.js b/static/pages/index.js new file mode 100644 index 0000000..56b0848 --- /dev/null +++ b/static/pages/index.js @@ -0,0 +1,5 @@ +export ImageViewer from './image-viewer/index.js'; +export LoginView from './login/index.js'; +export MainView from './main/index.js'; +export SettingsView from './settings/index.js'; +export ShuffleView from './shuffle/index.js'; diff --git a/static/pages/login/index.js b/static/pages/login/index.js index 80bacb0..984d001 100644 --- a/static/pages/login/index.js +++ b/static/pages/login/index.js @@ -1,8 +1,7 @@ +import icons from 'icons'; import * as sfw from 'sfw'; const { Div, Label, H1: Title, Input, Button } = sfw.element.native; -import icons from '../../icons/index.js'; - const css = await sfw.css(import.meta.url, './index.css'); export default class LoginView extends sfw.element.Container { diff --git a/static/pages/main/index.js b/static/pages/main/index.js index 9bf0aae..8c0cac1 100644 --- a/static/pages/main/index.js +++ b/static/pages/main/index.js @@ -1,8 +1,7 @@ +import icons from 'icons'; import * as sfw from 'sfw'; const { Div, Input } = sfw.element.native; -import icons from '/icons/index.js'; - const css = await sfw.css(import.meta.url, './index.css'); export default class MainView extends sfw.element.Container { diff --git a/static/pages/settings/index.js b/static/pages/settings/index.js index 4924f8b..c46987b 100644 --- a/static/pages/settings/index.js +++ b/static/pages/settings/index.js @@ -1,13 +1,11 @@ +import icons from 'icons'; +import * as api from 'api'; import * as sfw from 'sfw'; const { Div, Img } = sfw.element.native; -import * as api from '../../api/index.js'; - import Editable from '../../widgets/editable/index.js'; import PasswordDialog from '../../widgets/password-dialog/index.js'; -import icons from '../../icons/index.js'; - const css = await sfw.css(import.meta.url, './index.css'); export default class SettingsView extends sfw.element.Container { diff --git a/static/service-worker/worker.js b/static/service-worker/worker.js index ad5a374..be23018 100644 --- a/static/service-worker/worker.js +++ b/static/service-worker/worker.js @@ -1,18 +1,45 @@ -const putInCache = async (request, response) => { - const cache = await caches.open("v1"); - await cache.put(request, response); +const cachable_api = [ + '/image/', + '/profile/', +]; + +const is_cachable = (url) => { + if (!url.includes('/api/') || url.endsWith('.js')) { + return true; + } + + for (const api of cachable_api) { + if (url.includes(api)) { + return true; + } + } + + return false; +} + +const put_in_cache = async (request, response) => { + const cache = await caches.open("v1"); + await cache.put(request, response); }; -const cacheFirst = async (request, event) => { - const responseFromCache = await caches.match(request); - if (responseFromCache) { - return responseFromCache; - } - const responseFromNetwork = await fetch(request); - event.waitUntil(putInCache(request, responseFromNetwork.clone())); - return responseFromNetwork; +const cache_first = async (request, event) => { + if (request.method !== 'GET') { + return await fetch(request); + } + + if (!is_cachable(request.url)) { + return await fetch(request); + } + + const responseFromCache = await caches.match(request); + if (responseFromCache) { + return responseFromCache; + } + const responseFromNetwork = await fetch(request); + event.waitUntil(put_in_cache(request, responseFromNetwork.clone())); + return responseFromNetwork; }; self.addEventListener("fetch", (event) => { - event.respondWith(cacheFirst(event.request, event)); + event.respondWith(cache_first(event.request, event)); }); diff --git a/static/widgets/editable/index.js b/static/widgets/editable/index.js index 0c93c4e..116946f 100644 --- a/static/widgets/editable/index.js +++ b/static/widgets/editable/index.js @@ -1,8 +1,7 @@ +import icons from 'icons'; import * as sfw from 'sfw'; const { Div, Label, Input } = sfw.element.native; -import icons from '../../icons/index.js'; - const css = await sfw.css(import.meta.url, './index.css'); export default class Editable extends sfw.element.Container { diff --git a/static/widgets/image/index.js b/static/widgets/image/index.js index 7111ec7..202494b 100644 --- a/static/widgets/image/index.js +++ b/static/widgets/image/index.js @@ -1,8 +1,7 @@ +import * as api from 'api'; import * as sfw from 'sfw'; const { Div, Img } = sfw.element.native; -import * as api from '../../api/index.js'; - const css = await sfw.css(import.meta.url, './index.css') export default class Image extends sfw.element.Container { diff --git a/static/widgets/index.js b/static/widgets/index.js new file mode 100644 index 0000000..c468f34 --- /dev/null +++ b/static/widgets/index.js @@ -0,0 +1,6 @@ +export Editable from './editable/index.js'; +export Image from './image/index.js'; +export MonthSelect from './month-select/index.js'; +export PasswordDialog from './password-dialog/index.js'; +export Search from './search/index.js'; +export UploadBar from './upload-bar/index.js'; diff --git a/static/widgets/month-select/index.js b/static/widgets/month-select/index.js index d23469d..6ffbac2 100644 --- a/static/widgets/month-select/index.js +++ b/static/widgets/month-select/index.js @@ -1,8 +1,7 @@ +import icons from 'icons'; import * as sfw from 'sfw'; const { Div } = sfw.element.native; -import icons from '../../icons/index.js'; - const css = await sfw.css(import.meta.url, './index.css'); export default class MonthSelect extends sfw.element.Container { diff --git a/static/widgets/password-dialog/index.js b/static/widgets/password-dialog/index.js index 334e02d..6dbba12 100644 --- a/static/widgets/password-dialog/index.js +++ b/static/widgets/password-dialog/index.js @@ -1,10 +1,8 @@ +import * as api from 'api'; +import icons from 'icons'; import * as sfw from 'sfw'; const { Div, Input } = sfw.element.native; -import * as api from '../../api/index.js'; - -import icons from '../../icons/index.js'; - const css = await sfw.css(import.meta.url, './index.css'); export default class PasswordDialog extends sfw.element.Container { diff --git a/static/widgets/search/index.js b/static/widgets/search/index.js index 324141b..1a367f3 100644 --- a/static/widgets/search/index.js +++ b/static/widgets/search/index.js @@ -1,8 +1,7 @@ +import icons from 'icons'; import * as sfw from 'sfw'; const { Div, Input, Button } = sfw.element.native; -import icons from '../../icons/index.js'; - const css = await sfw.css(import.meta.url, './index.css'); export default class Search extends sfw.element.Container { |