mirror of
https://github.com/suitenumerique/docs.git
synced 2026-05-10 09:02:35 +02:00
🐛(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:
@@ -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);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user