Skip to content

Commit a66231d

Browse files
committed
🗃️(service-worker) cache successfull update in doc-item
When we updated the doc successfully then directly pass offline, the cache was not updated. It is because a successful update didn't update the cache of the doc-item.
1 parent 2e0b6b2 commit a66231d

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

src/frontend/apps/impress/src/features/service-worker/ApiPlugin.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,36 @@ export class ApiPlugin implements WorkboxPlugin {
4444
request,
4545
response,
4646
}) => {
47-
if (this.options.type === 'list' || this.options.type === 'item') {
48-
if (response.status !== 200) {
49-
return response;
50-
}
47+
if (response.status !== 200) {
48+
return response;
49+
}
5150

51+
if (this.options.type === 'list' || this.options.type === 'item') {
5252
const tableName = this.options.tableName;
5353
const body = (await response.clone().json()) as DocsResponse | Doc;
5454
await DocsDB.cacheResponse(request.url, body, tableName);
5555
}
5656

57+
if (this.options.type === 'update') {
58+
const db = await DocsDB.open();
59+
const storedResponse = await db.get('doc-item', request.url);
60+
61+
if (!storedResponse || !this.initialRequest) {
62+
return response;
63+
}
64+
65+
const bodyMutate = (await this.initialRequest
66+
.clone()
67+
.json()) as Partial<Doc>;
68+
69+
const newResponse = {
70+
...storedResponse,
71+
...bodyMutate,
72+
};
73+
74+
await DocsDB.cacheResponse(request.url, newResponse, 'doc-item');
75+
}
76+
5777
return response;
5878
};
5979

src/frontend/apps/impress/src/features/service-worker/__tests__/ApiPlugin.test.tsx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,34 @@ jest.mock('idb', () => ({
3030
describe('ApiPlugin', () => {
3131
afterEach(() => jest.clearAllMocks());
3232

33-
['doc-item', 'doc-list'].forEach((type) => {
33+
[
34+
{ type: 'item', table: 'doc-item' },
35+
{ type: 'list', table: 'doc-list' },
36+
{ type: 'update', table: 'doc-item' },
37+
].forEach(({ type, table }) => {
3438
it(`calls fetchDidSucceed with type ${type} and status 200`, async () => {
39+
const mockedSync = jest.fn().mockResolvedValue({});
3540
const apiPlugin = new ApiPlugin({
36-
tableName: type as any,
37-
type: 'list',
38-
syncManager: jest.fn() as any,
41+
tableName: table as any,
42+
type: type as any,
43+
syncManager: {
44+
sync: () => mockedSync(),
45+
} as any,
3946
});
4047

4148
const body = { lastName: 'Doe' };
4249
const bodyBuffer = RequestSerializer.objectToArrayBuffer(body);
4350

51+
const requestInit = {
52+
request: {
53+
url: 'test-url',
54+
clone: () => mockedClone(),
55+
json: () => body,
56+
} as unknown as Request,
57+
} as any;
58+
const mockedClone = jest.fn().mockReturnValue(requestInit.request);
59+
await apiPlugin.requestWillFetch?.(requestInit);
60+
4461
const response = await apiPlugin.fetchDidSucceed?.({
4562
request: {
4663
url: 'test-url',
@@ -55,15 +72,15 @@ describe('ApiPlugin', () => {
5572
}),
5673
} as any);
5774

58-
expect(mockedPut).toHaveBeenCalledWith(type, body, 'test-url');
75+
expect(mockedPut).toHaveBeenCalledWith(table, body, 'test-url');
5976
expect(mockedClose).toHaveBeenCalled();
6077
expect(response?.status).toBe(200);
6178
});
6279

6380
it(`calls fetchDidSucceed with type ${type} and status other that 200`, async () => {
6481
const apiPlugin = new ApiPlugin({
65-
tableName: type as any,
66-
type: 'list',
82+
tableName: table as any,
83+
type: type as any,
6784
syncManager: jest.fn() as any,
6885
});
6986

0 commit comments

Comments
 (0)