Skip to content

Commit e26b806

Browse files
committed
🐛(service-worker) networkFirst on api request
Some api requests were served by the cache first, which caused the data to be outdated. This change makes sure that the api requests are always served by the network first.
1 parent aef19cf commit e26b806

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1+
import pkg from '@/../package.json';
2+
13
export const SW_DEV_URL = [
24
'http://localhost:3000',
35
'https://impress.127.0.0.1.nip.io',
46
'https://impress-staging.beta.numerique.gouv.fr',
57
];
8+
9+
export const SW_DEV_API = 'http://localhost:8071';
10+
11+
export const SW_VERSION = `v-${process.env.NEXT_PUBLIC_BUILD_ID}`;
12+
13+
export const DAYS_EXP = 5;
14+
15+
export const getCacheNameVersion = (cacheName: string) =>
16+
`${pkg.name}-${cacheName}-${SW_VERSION}`;

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import { CacheableResponsePlugin } from 'workbox-cacheable-response';
2+
import { ExpirationPlugin } from 'workbox-expiration';
13
import { registerRoute } from 'workbox-routing';
2-
import { NetworkOnly } from 'workbox-strategies';
4+
import { NetworkFirst, NetworkOnly } from 'workbox-strategies';
35

46
import { ApiPlugin } from './ApiPlugin';
57
import { DocsDB } from './DocsDB';
68
import { SyncManager } from './SyncManager';
9+
import { DAYS_EXP, SW_DEV_API, getCacheNameVersion } from './conf';
710

811
declare const self: ServiceWorkerGlobalScope;
912

@@ -14,10 +17,9 @@ self.addEventListener('activate', function (event) {
1417
});
1518

1619
export const isApiUrl = (href: string) => {
17-
const devDomain = 'http://localhost:8071';
1820
return (
1921
href.includes(`${self.location.origin}/api/`) ||
20-
href.includes(`${devDomain}/api/`)
22+
href.includes(`${SW_DEV_API}/api/`)
2123
);
2224
};
2325

@@ -91,3 +93,17 @@ registerRoute(
9193
}),
9294
'DELETE',
9395
);
96+
97+
registerRoute(
98+
({ url }) => isApiUrl(url.href),
99+
new NetworkFirst({
100+
cacheName: getCacheNameVersion('api'),
101+
plugins: [
102+
new CacheableResponsePlugin({ statuses: [0, 200] }),
103+
new ExpirationPlugin({
104+
maxAgeSeconds: 24 * 60 * 60 * DAYS_EXP,
105+
}),
106+
],
107+
}),
108+
'GET',
109+
);

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919

2020
// eslint-disable-next-line import/order
2121
import { ApiPlugin } from './ApiPlugin';
22-
import { SW_DEV_URL } from './conf';
22+
import { DAYS_EXP, SW_DEV_URL, SW_VERSION, getCacheNameVersion } from './conf';
2323
import { isApiUrl } from './service-worker-api';
2424

2525
// eslint-disable-next-line import/order
@@ -31,16 +31,11 @@ declare const self: ServiceWorkerGlobalScope & {
3131

3232
self.__WB_DISABLE_DEV_LOGS = true;
3333

34-
const version = `v-${process.env.NEXT_PUBLIC_BUILD_ID}`;
35-
3634
setCacheNameDetails({
3735
prefix: pkg.name,
38-
suffix: version,
36+
suffix: SW_VERSION,
3937
});
4038

41-
const getCacheNameVersion = (cacheName: string) =>
42-
`${pkg.name}-${cacheName}-${version}`;
43-
4439
/**
4540
* In development, use NetworkFirst strategy, in production use CacheFirst strategy
4641
* We will be able to test the application in development without having to clear the cache.
@@ -53,7 +48,7 @@ const getStrategy = (
5348
): NetworkFirst | CacheFirst => {
5449
return SW_DEV_URL.some((devDomain) =>
5550
self.location.origin.includes(devDomain),
56-
)
51+
) || isApiUrl(self.location.href)
5752
? new NetworkFirst(options)
5853
: new CacheFirst(options);
5954
};
@@ -66,7 +61,7 @@ self.addEventListener('install', function (event) {
6661
});
6762

6863
self.addEventListener('activate', function (event) {
69-
const cacheAllow = `${version}`;
64+
const cacheAllow = SW_VERSION;
7065

7166
event.waitUntil(
7267
// Delete old caches
@@ -135,8 +130,6 @@ setCatchHandler(async ({ request, url, event }) => {
135130
}
136131
});
137132

138-
const DAYS_EXP = 5;
139-
140133
/**
141134
* Cache stategy static files images (images / svg)
142135
*/

0 commit comments

Comments
 (0)