Skip to content

Commit

Permalink
fix: prevent race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery committed Jan 14, 2024
1 parent 67c85a6 commit 2ab1af2
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions packages/qwik/src/prefetch-service-worker/direct-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async function getResponse(swState: SWState, url: URL): Promise<Response> {
const currentRequestTask = swState.$queue$.find((task) => task.$url$.pathname === url.pathname)!;
if (!currentRequestTask) {
swState.$log$('CACHE HIT', url.pathname);
return swState.$cache$!.match(url) as Promise<Response>;
return (await swState.$cache$).match(url) as Promise<Response>;
} else {
return currentRequestTask.$response$;
}
Expand All @@ -55,7 +55,7 @@ async function enqueueFetchIfNeeded(swState: SWState, url: URL, priority: number
swState.$log$('already in queue', mode, state, url.pathname);
}
} else {
const cacheEntry = await swState.$cache$!.match(url);
const cacheEntry = await (await swState.$cache$).match(url);
if (!cacheEntry) {
swState.$log$('enqueue', mode, url.pathname);
task = {
Expand Down Expand Up @@ -91,7 +91,7 @@ function taskTick(swState: SWState) {
.then(async (response) => {
if (response.status === 200) {
swState.$log$('CACHED', task.$url$.pathname);
await swState.$cache$!.put(task.$url$, response.clone());
await (await swState.$cache$).put(task.$url$, response.clone());
}
task.$resolveResponse$(response);
})
Expand Down
4 changes: 2 additions & 2 deletions packages/qwik/src/prefetch-service-worker/process-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ async function processBundleGraph(
});
if (cleanup) {
const bundles = new Set<string>(graph.filter((item) => typeof item === 'string') as string[]);
for (const request of await swState.$cache$!.keys()) {
for (const request of await (await swState.$cache$).keys()) {
const [cacheBase, filename] = parseBaseFilename(new URL(request.url));
const promises: Promise<boolean>[] = [];
if (cacheBase === base && !bundles.has(filename)) {
swState.$log$('deleting', request.url);
promises.push(swState.$cache$!.delete(request));
promises.push((await swState.$cache$).delete(request));
}
await Promise.all(promises);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/qwik/src/prefetch-service-worker/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export const setupServiceWorker = (swScope: ServiceWorkerGlobalScope) => {
swScope.addEventListener('install', () => swScope.skipWaiting());
swScope.addEventListener('activate', async (event) => {
event.waitUntil(swScope.clients.claim());
swState.$cache$ = await swScope.caches.open('QwikBundles');
swState.$cache$ = swScope.caches.open('QwikBundles');
});
};
4 changes: 2 additions & 2 deletions packages/qwik/src/prefetch-service-worker/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface SWState {
/** List of Base paths */
$bases$: SWStateBase[];
// Browser Cache
$cache$: Cache | null;
$cache$: Promise<Cache>;
// Maximum number of prefetch requests. (Direct requests are not limited.)
$maxPrefetchRequests$: number;
// Log function
Expand Down Expand Up @@ -54,7 +54,7 @@ export const createState = (fetch: ServiceWorkerGlobalScope['fetch'], url: URL):
$fetch$: fetch,
$queue$: [],
$bases$: [],
$cache$: null,
$cache$: null!,
$msgQueue$: [],
$msgQueuePromise$: null,
$maxPrefetchRequests$: 10,
Expand Down

0 comments on commit 2ab1af2

Please sign in to comment.