Skip to content

Commit

Permalink
feat: Add public route to reset local data
Browse files Browse the repository at this point in the history
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:
cozy/cozy-stack#4483
  • Loading branch information
paultranvan committed Nov 5, 2024
1 parent 607d336 commit fbfb898
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 5 deletions.
5 changes: 5 additions & 0 deletions manifest.webapp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
"folder": "/",
"index": "index.html",
"public": false
},
"/reset": {
"folder": "/reset",
"index": "index.html",
"public": true
}
},
"intents": [
Expand Down
14 changes: 11 additions & 3 deletions rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export default defineConfig({
output: {
cleanDistPath: true,
distPath: {
root: 'build'
}
root: 'build',
},
},
html: {
template: './src/targets/browser/index.ejs',
Expand Down Expand Up @@ -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'
},
],
})
]
Expand Down
5 changes: 5 additions & 0 deletions src/dataproxy/worker/SharedWorkerProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]')

Expand Down Expand Up @@ -44,7 +45,11 @@ export const SharedWorkerProvider = React.memo(
if (!client) return

const doAsync = async (): Promise<void> => {
// Cleanup any remaining local data
await removeStaleLocalData()

log.debug('Init SharedWorker')

const workerInst = new SharedWorker(
new URL('./shared-worker.ts', import.meta.url),
{
Expand Down
3 changes: 2 additions & 1 deletion src/dataproxy/worker/shared-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -62,6 +62,7 @@ const dataProxy: DataProxyWorker = {

searchEngine = new SearchEngine(client)

log.debug('Setup done')
updateState()
},

Expand Down
26 changes: 26 additions & 0 deletions src/dataproxy/worker/utils.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
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<void> => {
// 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
}
2 changes: 2 additions & 0 deletions src/search/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ export const DOCTYPE_ORDER = {
[CONTACTS_DOCTYPE]: 1,
[FILES_DOCTYPE]: 2
}

export const LOCALSTORAGE_KEY_DELETING_DATA = 'deletingLocalData'
1 change: 0 additions & 1 deletion src/targets/browser/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="/favicon-16x16.png" sizes="16x16">
<link rel="manifest" href="/manifest.json" crossOrigin="use-credentials">
<meta name="color-scheme" content="light dark" />
<meta name="theme-color" content="#ffffff">
<meta name="viewport" content="width=device-width, initial-scale=1">
Expand Down
6 changes: 6 additions & 0 deletions src/targets/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<head>
<script src="reset/reset.js">
</script>
</head>
</html>
20 changes: 20 additions & 0 deletions src/targets/public/reset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function resetData() {
// Remove localstorage
window.localStorage.clear()
console.log('Deleted localStorage')

Check failure on line 4 in src/targets/public/reset.js

View workflow job for this annotation

GitHub Actions / Build and publish

Unexpected console statement

// 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)

Check failure on line 13 in src/targets/public/reset.js

View workflow job for this annotation

GitHub Actions / Build and publish

Unexpected console statement
})
// Everything is done, remove the flag
window.localStorage.removeItem('deletingLocalData')
})
}

resetData()

0 comments on commit fbfb898

Please sign in to comment.