enhance: embedページではstoreの保存先を完全に分離するように

This commit is contained in:
kakkokari-gtyih
2024-06-01 21:03:39 +09:00
parent ecf7945fe8
commit e9b3b5ffcd
13 changed files with 168 additions and 23 deletions

View File

@@ -10,10 +10,12 @@ import {
set as iset,
del as idel,
} from 'idb-keyval';
import { isEmbedPage } from './embed-page.js';
import { miLocalStorage } from '@/local-storage.js';
const fallbackName = (key: string) => `idbfallback::${key}`;
const PREFIX = 'idbfallback::';
let idbAvailable = typeof window !== 'undefined' ? !!(window.indexedDB && window.indexedDB.open) : true;
let idbAvailable = typeof window !== 'undefined' ? !!(window.indexedDB && typeof window.indexedDB.open === 'function' && !isEmbedPage()) : true;
// iframe.contentWindow.indexedDB.deleteDatabase() がchromeのバグで使用できないため、indexedDBを無効化している。
// バグが治って再度有効化するのであれば、cypressのコマンド内のコメントアウトを外すこと
@@ -38,15 +40,15 @@ if (idbAvailable) {
export async function get(key: string) {
if (idbAvailable) return iget(key);
return JSON.parse(window.localStorage.getItem(fallbackName(key)));
return miLocalStorage.getItemAsJson(`${PREFIX}${key}`);
}
export async function set(key: string, val: any) {
if (idbAvailable) return iset(key, val);
return window.localStorage.setItem(fallbackName(key), JSON.stringify(val));
return miLocalStorage.setItemAsJson(`${PREFIX}${key}`, val);
}
export async function del(key: string) {
if (idbAvailable) return idel(key);
return window.localStorage.removeItem(fallbackName(key));
return miLocalStorage.removeItem(`${PREFIX}${key}`);
}