🐛(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.
This commit is contained in:
Anthony LC
2024-07-02 14:59:33 +02:00
parent 24d08b2970
commit 4eca472eec
3 changed files with 5 additions and 3 deletions
@@ -3,3 +3,5 @@ export const SW_DEV_URL = [
'https://impress.127.0.0.1.nip.io',
'https://impress-staging.beta.numerique.gouv.fr',
];
export const SW_DEV_API = 'http://localhost:8071';
@@ -4,6 +4,7 @@ import { NetworkOnly } from 'workbox-strategies';
import { ApiPlugin } from './ApiPlugin';
import { DocsDB } from './DocsDB';
import { SyncManager } from './SyncManager';
import { SW_DEV_API } from './conf';
declare const self: ServiceWorkerGlobalScope;
@@ -14,10 +15,9 @@ self.addEventListener('activate', function (event) {
});
export const isApiUrl = (href: string) => {
const devDomain = 'http://localhost:8071';
return (
href.includes(`${self.location.origin}/api/`) ||
href.includes(`${devDomain}/api/`)
href.includes(`${SW_DEV_API}/api/`)
);
};
@@ -53,7 +53,7 @@ const getStrategy = (
): NetworkFirst | CacheFirst => {
return SW_DEV_URL.some((devDomain) =>
self.location.origin.includes(devDomain),
)
) || isApiUrl(self.location.href)
? new NetworkFirst(options)
: new CacheFirst(options);
};