From fbfb898f1ea34b20c09379c9f2b3391be7e19d78 Mon Sep 17 00:00:00 2001 From: Paul Tran-Van Date: Mon, 4 Nov 2024 16:41:08 +0100 Subject: [PATCH] feat: Add public route to reset local data When the stack serves the login page, we take this opportunity to remove any remaining local data, i.e. indexedDB and localstorage data. See how it is done for the stack part: https://github.com/cozy/cozy-stack/pull/4483 --- manifest.webapp | 5 ++++ rsbuild.config.ts | 14 +++++++--- src/dataproxy/worker/SharedWorkerProvider.tsx | 5 ++++ src/dataproxy/worker/shared-worker.ts | 3 ++- src/dataproxy/worker/utils.ts | 26 +++++++++++++++++++ src/search/consts.ts | 2 ++ src/targets/browser/index.ejs | 1 - src/targets/public/index.html | 6 +++++ src/targets/public/reset.js | 20 ++++++++++++++ 9 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 src/dataproxy/worker/utils.ts create mode 100644 src/targets/public/index.html create mode 100644 src/targets/public/reset.js diff --git a/manifest.webapp b/manifest.webapp index eab9cb1..151d9c0 100644 --- a/manifest.webapp +++ b/manifest.webapp @@ -16,6 +16,11 @@ "folder": "/", "index": "index.html", "public": false + }, + "/reset": { + "folder": "/reset", + "index": "index.html", + "public": true } }, "intents": [ diff --git a/rsbuild.config.ts b/rsbuild.config.ts index fa6a734..c405920 100644 --- a/rsbuild.config.ts +++ b/rsbuild.config.ts @@ -9,8 +9,8 @@ export default defineConfig({ output: { cleanDistPath: true, distPath: { - root: 'build' - } + root: 'build', + }, }, html: { template: './src/targets/browser/index.ejs', @@ -49,7 +49,15 @@ export default defineConfig({ }, { from: 'icon.svg' - } + }, + { + from: 'src/targets/public/index.html', + to: 'reset/index.html' + }, + { + from: 'src/targets/public/reset.js', + to: 'reset/reset.js' + }, ], }) ] diff --git a/src/dataproxy/worker/SharedWorkerProvider.tsx b/src/dataproxy/worker/SharedWorkerProvider.tsx index f8cdbe0..6b305fe 100644 --- a/src/dataproxy/worker/SharedWorkerProvider.tsx +++ b/src/dataproxy/worker/SharedWorkerProvider.tsx @@ -10,6 +10,7 @@ import { DataProxyWorkerPartialState } from '@/dataproxy/common/DataProxyInterface' import { TabCountSync } from '@/dataproxy/common/TabCountSync' +import { removeStaleLocalData } from '@/dataproxy/worker/utils' const log = Minilog('👷‍♂️ [SharedWorkerProvider]') @@ -44,7 +45,11 @@ export const SharedWorkerProvider = React.memo( if (!client) return const doAsync = async (): Promise => { + // Cleanup any remaining local data + await removeStaleLocalData() + log.debug('Init SharedWorker') + const workerInst = new SharedWorker( new URL('./shared-worker.ts', import.meta.url), { diff --git a/src/dataproxy/worker/shared-worker.ts b/src/dataproxy/worker/shared-worker.ts index 0e47b94..0daba01 100644 --- a/src/dataproxy/worker/shared-worker.ts +++ b/src/dataproxy/worker/shared-worker.ts @@ -24,7 +24,7 @@ const broadcastChannel = new BroadcastChannel('DATA_PROXY_BROADCAST_CHANANEL') const dataProxy: DataProxyWorker = { setup: async (clientData: ClientData) => { - log.debug('Received data for setting client') + log.debug('Received data for setting up client') if (client) return updateState() @@ -62,6 +62,7 @@ const dataProxy: DataProxyWorker = { searchEngine = new SearchEngine(client) + log.debug('Setup done') updateState() }, diff --git a/src/dataproxy/worker/utils.ts b/src/dataproxy/worker/utils.ts new file mode 100644 index 0000000..999dd02 --- /dev/null +++ b/src/dataproxy/worker/utils.ts @@ -0,0 +1,26 @@ +import Minilog from 'cozy-minilog' + +import { LOCALSTORAGE_KEY_DELETING_DATA } from '@/search/consts' +const log = Minilog('👷‍♂️ [Worker utils]') + +const deleteDatabases = async (): Promise => { + const databases = await window.indexedDB.databases() + // Remove all indexedDB databases + for (const db of databases) { + if (db.name) { + window.indexedDB.deleteDatabase(db.name) + log.info('Deleted indexedDB database : ', db.name) + } + } +} + +export const removeStaleLocalData = async (): Promise => { + // Check flag existence proving the reset process was incomplete + const hasStaleData = localStorage.getItem(LOCALSTORAGE_KEY_DELETING_DATA) + if (hasStaleData) { + log.info('Found stale data: remove it') + await deleteDatabases() + localStorage.removeItem(LOCALSTORAGE_KEY_DELETING_DATA) + } + return +} diff --git a/src/search/consts.ts b/src/search/consts.ts index 51ac525..eac32e5 100644 --- a/src/search/consts.ts +++ b/src/search/consts.ts @@ -40,3 +40,5 @@ export const DOCTYPE_ORDER = { [CONTACTS_DOCTYPE]: 1, [FILES_DOCTYPE]: 2 } + +export const LOCALSTORAGE_KEY_DELETING_DATA = 'deletingLocalData' diff --git a/src/targets/browser/index.ejs b/src/targets/browser/index.ejs index 4cd1c9e..89c5ed1 100644 --- a/src/targets/browser/index.ejs +++ b/src/targets/browser/index.ejs @@ -6,7 +6,6 @@ - diff --git a/src/targets/public/index.html b/src/targets/public/index.html new file mode 100644 index 0000000..63ab2d1 --- /dev/null +++ b/src/targets/public/index.html @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/src/targets/public/reset.js b/src/targets/public/reset.js new file mode 100644 index 0000000..094e296 --- /dev/null +++ b/src/targets/public/reset.js @@ -0,0 +1,20 @@ +function resetData() { + // Remove localstorage + window.localStorage.clear() + console.log('Deleted localStorage') + + // Add this flag for future check + window.localStorage.setItem('deletingLocalData', new Date().toISOString()) + + // Remove all indexedDB databases + window.indexedDB.databases().then(function (databases) { + databases.forEach(function (db) { + window.indexedDB.deleteDatabase(db.name) + console.log('Deleted indexedDB database : ', db.name) + }) + // Everything is done, remove the flag + window.localStorage.removeItem('deletingLocalData') + }) +} + +resetData()