From 19481de4595728bf5537c98bf15c0e10aa222b94 Mon Sep 17 00:00:00 2001 From: tamaina Date: Wed, 24 Feb 2021 18:40:24 +0900 Subject: [PATCH 1/8] wip --- src/client/account.ts | 2 +- src/client/init.ts | 2 +- src/client/scripts/get-account-from-id.ts | 2 +- src/client/scripts/idb-proxy.ts | 23 +++++++++++++++++++++++ src/client/sw/lang.ts | 2 +- src/client/sw/notification-read.ts | 2 +- 6 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 src/client/scripts/idb-proxy.ts diff --git a/src/client/account.ts b/src/client/account.ts index 1ba7a28908..5519fd028c 100644 --- a/src/client/account.ts +++ b/src/client/account.ts @@ -1,4 +1,4 @@ -import { get, set } from 'idb-keyval'; +import { get, set } from '@/scripts/idb-proxy'; import { reactive } from 'vue'; import { apiUrl } from '@/config'; import { waiting } from '@/os'; diff --git a/src/client/init.ts b/src/client/init.ts index 264ecaf8d1..28d919e8b9 100644 --- a/src/client/init.ts +++ b/src/client/init.ts @@ -4,7 +4,7 @@ import '@/style.scss'; -import { set } from 'idb-keyval'; +import { set } from '@/scripts/idb-proxy'; // TODO: そのうち消す if (localStorage.getItem('vuex') != null) { diff --git a/src/client/scripts/get-account-from-id.ts b/src/client/scripts/get-account-from-id.ts index be4cfaeba4..ba3adceecc 100644 --- a/src/client/scripts/get-account-from-id.ts +++ b/src/client/scripts/get-account-from-id.ts @@ -1,4 +1,4 @@ -import { get } from 'idb-keyval'; +import { get } from '@/scripts/idb-proxy'; export async function getAccountFromId(id: string) { const accounts = await get('accounts') as { token: string; id: string; }[]; diff --git a/src/client/scripts/idb-proxy.ts b/src/client/scripts/idb-proxy.ts new file mode 100644 index 0000000000..986e8ee8dc --- /dev/null +++ b/src/client/scripts/idb-proxy.ts @@ -0,0 +1,23 @@ +// FirefoxのプライベートモードなどではindexedDBが使用不可能なので、使う +import { + get as iget, + set as iset, + del as idel +} from 'idb-keyval'; + +const fallbackName = (key: string) => `idbfallback::${key}`; + +export async function get(key: string) { + if (window.indexedDB) return iget(key); + return JSON.parse(localStorage.getItem(fallbackName(key)) || 'null'); +} + +export async function set(key: string, val: any) { + if (window.indexedDB) return iset(key, val); + return localStorage.setItem(fallbackName(key), JSON.stringify(val)); +} + +export async function del(key: string) { + if (window.indexedDB) return idel(key); + return localStorage.removeItem(fallbackName(key)); +} diff --git a/src/client/sw/lang.ts b/src/client/sw/lang.ts index 1b3f1ad620..99f0e518c3 100644 --- a/src/client/sw/lang.ts +++ b/src/client/sw/lang.ts @@ -3,7 +3,7 @@ */ declare var self: ServiceWorkerGlobalScope; -import { get, set } from 'idb-keyval'; +import { get, set } from '@/scripts/idb-proxy'; import { I18n } from '../../misc/i18n'; class SwLang { diff --git a/src/client/sw/notification-read.ts b/src/client/sw/notification-read.ts index 093322a8b7..03cb0611ca 100644 --- a/src/client/sw/notification-read.ts +++ b/src/client/sw/notification-read.ts @@ -1,6 +1,6 @@ declare var self: ServiceWorkerGlobalScope; -import { get } from 'idb-keyval'; +import { get } from '@/scripts/idb-proxy'; import { pushNotificationData } from '../../types'; import { api } from './operations'; From a7180b32621c132264dd147a36e97fcb1af15dfc Mon Sep 17 00:00:00 2001 From: tamaina Date: Wed, 24 Feb 2021 19:00:01 +0900 Subject: [PATCH 2/8] ok --- src/client/components/sidebar.vue | 2 +- src/client/scripts/idb-proxy.ts | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/client/components/sidebar.vue b/src/client/components/sidebar.vue index 8eace36f19..476c7f8734 100644 --- a/src/client/components/sidebar.vue +++ b/src/client/components/sidebar.vue @@ -242,7 +242,7 @@ export default defineComponent({ addAcount() { os.popup(import('./signin-dialog.vue'), {}, { done: async res => { - addAccount(res.id, res.i); + await addAccount(res.id, res.i); os.success(); }, }, 'closed'); diff --git a/src/client/scripts/idb-proxy.ts b/src/client/scripts/idb-proxy.ts index 986e8ee8dc..804d7ca625 100644 --- a/src/client/scripts/idb-proxy.ts +++ b/src/client/scripts/idb-proxy.ts @@ -1,23 +1,23 @@ // FirefoxのプライベートモードなどではindexedDBが使用不可能なので、使う import { - get as iget, - set as iset, - del as idel + get as iget, + set as iset, + del as idel } from 'idb-keyval'; -const fallbackName = (key: string) => `idbfallback::${key}`; +const fallbackName = (key: string) => `idbfallback::${key}`; export async function get(key: string) { - if (window.indexedDB) return iget(key); - return JSON.parse(localStorage.getItem(fallbackName(key)) || 'null'); + if (window.indexedDB) return iget(key); + return JSON.parse(localStorage.getItem(fallbackName(key))); } export async function set(key: string, val: any) { - if (window.indexedDB) return iset(key, val); - return localStorage.setItem(fallbackName(key), JSON.stringify(val)); + if (window.indexedDB) return iset(key, val); + return localStorage.setItem(fallbackName(key), JSON.stringify(val)); } export async function del(key: string) { - if (window.indexedDB) return idel(key); - return localStorage.removeItem(fallbackName(key)); + if (window.indexedDB) return idel(key); + return localStorage.removeItem(fallbackName(key)); } From 40d12949b1a39fd3a52372d9b0e382d2e895963d Mon Sep 17 00:00:00 2001 From: tamaina Date: Wed, 24 Feb 2021 19:27:00 +0900 Subject: [PATCH 3/8] fix --- src/client/scripts/idb-proxy.ts | 19 ++++++-- src/client/ui/_common_/common.vue | 72 ++++++++++++++++--------------- 2 files changed, 52 insertions(+), 39 deletions(-) diff --git a/src/client/scripts/idb-proxy.ts b/src/client/scripts/idb-proxy.ts index 804d7ca625..aba17c796c 100644 --- a/src/client/scripts/idb-proxy.ts +++ b/src/client/scripts/idb-proxy.ts @@ -2,22 +2,33 @@ import { get as iget, set as iset, - del as idel + del as idel, } from 'idb-keyval'; const fallbackName = (key: string) => `idbfallback::${key}`; +let idbAvailable = !!window.indexedDB; + +if (idbAvailable) { + try { + const request = indexedDB.open('keyval-store'); + if (request.error) idbAvailable = false; + } catch (e) { + idbAvailable = false; + } +} + export async function get(key: string) { - if (window.indexedDB) return iget(key); + if (idbAvailable) return iget(key); return JSON.parse(localStorage.getItem(fallbackName(key))); } export async function set(key: string, val: any) { - if (window.indexedDB) return iset(key, val); + if (idbAvailable) return iset(key, val); return localStorage.setItem(fallbackName(key), JSON.stringify(val)); } export async function del(key: string) { - if (window.indexedDB) return idel(key); + if (idbAvailable) return idel(key); return localStorage.removeItem(fallbackName(key)); } diff --git a/src/client/ui/_common_/common.vue b/src/client/ui/_common_/common.vue index 8709f5a686..26e3a5b35e 100644 --- a/src/client/ui/_common_/common.vue +++ b/src/client/ui/_common_/common.vue @@ -55,44 +55,46 @@ export default defineComponent({ const sideViewHook = inject('sideViewHook', null); //#region Listen message from SW - navigator.serviceWorker.addEventListener('message', ev => { - if (_DEV_) { - console.log('sw msg', ev.data); - } + if ('serviceWorker' in navigator) { + navigator.serviceWorker.addEventListener('message', ev => { + if (_DEV_) { + console.log('sw msg', ev.data); + } - const data = ev.data as SwMessage; - if (data.type !== 'order') return; + const data = ev.data as SwMessage; + if (data.type !== 'order') return; - if (data.loginId !== $i?.id) { - return getAccountFromId(data.loginId).then(account => { - if (!account) return; - return login(account.token, data.url); - }); - } + if (data.loginId !== $i?.id) { + return getAccountFromId(data.loginId).then(account => { + if (!account) return; + return login(account.token, data.url); + }); + } - switch (data.order) { - case 'post': - return post(data.options); - case 'push': - if (data.url.startsWith('/my/messaging')) { - if (router.currentRoute.value.path === data.url) return; - if (ColdDeviceStorage.get('chatOpenBehavior') === 'window') return pageWindow(data.url); - if (ColdDeviceStorage.get('chatOpenBehavior') === 'popout') return popout(data.url); - } - if (router.currentRoute.value.path === data.url) { - return window.scroll({ top: 0, behavior: 'smooth' }); - } - if (navHook) { - return navHook(data.url); - } - if (sideViewHook && defaultStore.state.defaultSideView && data.url !== '/') { - return sideViewHook(data.url); - } - return router.push(data.url); - default: - return; - } - }); + switch (data.order) { + case 'post': + return post(data.options); + case 'push': + if (data.url.startsWith('/my/messaging')) { + if (router.currentRoute.value.path === data.url) return; + if (ColdDeviceStorage.get('chatOpenBehavior') === 'window') return pageWindow(data.url); + if (ColdDeviceStorage.get('chatOpenBehavior') === 'popout') return popout(data.url); + } + if (router.currentRoute.value.path === data.url) { + return window.scroll({ top: 0, behavior: 'smooth' }); + } + if (navHook) { + return navHook(data.url); + } + if (sideViewHook && defaultStore.state.defaultSideView && data.url !== '/') { + return sideViewHook(data.url); + } + return router.push(data.url); + default: + return; + } + }); + } } return { From c88d7a10878ab022a1d4b840a9ee5ecdbe79dcf1 Mon Sep 17 00:00:00 2001 From: tamaina Date: Wed, 24 Feb 2021 19:29:35 +0900 Subject: [PATCH 4/8] =?UTF-8?q?SW=E3=81=AF=E3=83=97=E3=83=AD=E3=82=AD?= =?UTF-8?q?=E3=82=B7=E4=B8=8D=E8=A6=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/sw/lang.ts | 2 +- src/client/sw/notification-read.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/sw/lang.ts b/src/client/sw/lang.ts index 99f0e518c3..1b3f1ad620 100644 --- a/src/client/sw/lang.ts +++ b/src/client/sw/lang.ts @@ -3,7 +3,7 @@ */ declare var self: ServiceWorkerGlobalScope; -import { get, set } from '@/scripts/idb-proxy'; +import { get, set } from 'idb-keyval'; import { I18n } from '../../misc/i18n'; class SwLang { diff --git a/src/client/sw/notification-read.ts b/src/client/sw/notification-read.ts index 03cb0611ca..093322a8b7 100644 --- a/src/client/sw/notification-read.ts +++ b/src/client/sw/notification-read.ts @@ -1,6 +1,6 @@ declare var self: ServiceWorkerGlobalScope; -import { get } from '@/scripts/idb-proxy'; +import { get } from 'idb-keyval'; import { pushNotificationData } from '../../types'; import { api } from './operations'; From 34e8ea58fbee7a02eef33289ab8b50b0ce25d3a3 Mon Sep 17 00:00:00 2001 From: tamaina Date: Wed, 3 Mar 2021 13:39:19 +0900 Subject: [PATCH 5/8] Fix --- src/client/scripts/idb-proxy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/scripts/idb-proxy.ts b/src/client/scripts/idb-proxy.ts index aba17c796c..70d2b19bb8 100644 --- a/src/client/scripts/idb-proxy.ts +++ b/src/client/scripts/idb-proxy.ts @@ -7,7 +7,7 @@ import { const fallbackName = (key: string) => `idbfallback::${key}`; -let idbAvailable = !!window.indexedDB; +let idbAvailable = window ? !!window.indexedDB : true; if (idbAvailable) { try { From b24c41c7777b751bed5df717c5716a600ffcf78a Mon Sep 17 00:00:00 2001 From: tamaina Date: Wed, 3 Mar 2021 13:56:00 +0900 Subject: [PATCH 6/8] Offline response --- src/client/sw/sw.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/client/sw/sw.ts b/src/client/sw/sw.ts index ec4de17551..a8bd9e2a1b 100644 --- a/src/client/sw/sw.ts +++ b/src/client/sw/sw.ts @@ -46,7 +46,10 @@ self.addEventListener('activate', ev => { //#region When: Fetching self.addEventListener('fetch', ev => { - // Nothing to do + ev.respondWith( + fetch(ev.request) + .catch(() => new Response(`Offline. Service Worker @${version}`, { status: 200 })) + ); }); //#endregion From 7b5e46276065413c536158aa6bad88b51d6636c7 Mon Sep 17 00:00:00 2001 From: tamaina Date: Wed, 3 Mar 2021 14:33:33 +0900 Subject: [PATCH 7/8] fix --- src/client/scripts/idb-proxy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/scripts/idb-proxy.ts b/src/client/scripts/idb-proxy.ts index 70d2b19bb8..c0d01e6292 100644 --- a/src/client/scripts/idb-proxy.ts +++ b/src/client/scripts/idb-proxy.ts @@ -7,7 +7,7 @@ import { const fallbackName = (key: string) => `idbfallback::${key}`; -let idbAvailable = window ? !!window.indexedDB : true; +let idbAvailable = typeof window !== 'undefined' ? !!window.indexedDB : true; if (idbAvailable) { try { From 4de7e57e29bfdab3541aa3570014cd23758ce513 Mon Sep 17 00:00:00 2001 From: tamaina Date: Wed, 3 Mar 2021 14:48:06 +0900 Subject: [PATCH 8/8] Fix --- src/client/sw/sw.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/sw/sw.ts b/src/client/sw/sw.ts index b24c887f32..6f78f3cb38 100644 --- a/src/client/sw/sw.ts +++ b/src/client/sw/sw.ts @@ -34,7 +34,7 @@ self.addEventListener('activate', ev => { self.addEventListener('fetch', ev => { ev.respondWith( fetch(ev.request) - .catch(() => new Response(`Offline. Service Worker @${version}`, { status: 200 })) + .catch(() => new Response(`Offline. Service Worker @${_VERSION_}`, { status: 200 })) ); }); //#endregion