Skip to content

Commit

Permalink
fix(offline): clear SWR caches betwen users [LIBS-358] (#1268)
Browse files Browse the repository at this point in the history
* fix(offline): clear SWR caches betwen users

* chore: format
  • Loading branch information
KaiVandivier committed Oct 24, 2022
1 parent ddcc521 commit fc0d143
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
16 changes: 11 additions & 5 deletions services/offline/src/lib/__tests__/clear-sensitive-caches.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const deleteMockDefault = makeCachesDeleteMock([])
const cachesDefault = {
keys: keysMockDefault,
delete: deleteMockDefault,
// the following to satisfy types:
has: () => Promise.resolve(true),
open: () => Promise.resolve(new Cache()),
match: () => Promise.resolve(new Response()),
}
window.caches = cachesDefault

Expand Down Expand Up @@ -53,6 +57,7 @@ it('returns false if caches.keys throws', async () => {
throw new Error('Security Error')
})
window.caches = {
...cachesDefault,
keys: spy,
}

Expand All @@ -63,20 +68,21 @@ it('returns false if caches.keys throws', async () => {
})

it('clears potentially sensitive caches', async () => {
const testKeys = ['cache1', 'cache2', 'app-shell']
const testKeys = ['cache1', 'cache2', 'app-shell', 'other-assets']
const keysMock = jest
.fn()
.mockImplementation(() => Promise.resolve(testKeys))
const deleteMock = makeCachesDeleteMock(testKeys)
window.caches = { keys: keysMock, delete: deleteMock }
window.caches = { ...cachesDefault, keys: keysMock, delete: deleteMock }

const cachesDeleted = await clearSensitiveCaches()
expect(cachesDeleted).toBe(true)

expect(deleteMock).toHaveBeenCalledTimes(3)
expect(deleteMock).toHaveBeenCalledTimes(4)
expect(deleteMock.mock.calls[0][0]).toBe('cache1')
expect(deleteMock.mock.calls[1][0]).toBe('cache2')
expect(deleteMock.mock.calls[2][0]).toBe('app-shell')
expect(deleteMock.mock.calls[3][0]).toBe('other-assets')
})

it('preserves keepable caches', async () => {
Expand All @@ -93,11 +99,11 @@ it('preserves keepable caches', async () => {

await clearSensitiveCaches()

expect(deleteMockDefault).toHaveBeenCalledTimes(3)
expect(deleteMockDefault).toHaveBeenCalledTimes(4)
expect(deleteMockDefault.mock.calls[0][0]).toBe('cache1')
expect(deleteMockDefault.mock.calls[1][0]).toBe('cache2')
expect(deleteMockDefault.mock.calls[2][0]).toBe('app-shell')
expect(deleteMockDefault).not.toHaveBeenCalledWith('other-assets')
expect(deleteMockDefault.mock.calls[3][0]).toBe('other-assets')
expect(deleteMockDefault).not.toHaveBeenCalledWith(
'workbox-precache-v2-https://hey.howareya.now/'
)
Expand Down
1 change: 0 additions & 1 deletion services/offline/src/lib/clear-sensitive-caches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export const SECTIONS_STORE = 'sections-store'
// Non-sensitive caches that can be kept:
const KEEPABLE_CACHES = [
/^workbox-precache/, // precached static assets
/^other-assets/, // static assets cached at runtime - shouldn't be sensitive
]

declare global {
Expand Down

0 comments on commit fc0d143

Please sign in to comment.