Skip to content

Commit

Permalink
fix: SW cache race condition (QwikDev#5780)
Browse files Browse the repository at this point in the history
* fix(sw): fix cache race condition

* chore(lint): lint package

* refactor(sw): remove excess logging from core
  • Loading branch information
thejackshelton authored Jan 26, 2024
1 parent c87c8ed commit 857ebd6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ scripts/**/*
**/server/**/*.js
*.tsbuildinfo
packages/docs/api/**/*
packages/docs/public/repl/bundled/**/*
packages/docs/src/routes/examples/apps/**/*
packages/docs/src/routes/playground/app/**/*
packages/docs/src/routes/tutorial/**/*
Expand Down
15 changes: 14 additions & 1 deletion packages/qwik/src/prefetch-service-worker/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ import { createState, type SWState } from './state';

export const setupServiceWorker = (swScope: ServiceWorkerGlobalScope) => {
const swState: SWState = createState(swScope.fetch.bind(swScope), new URL(swScope.location.href));
let cacheTimeout: any;
swState.$getCache$ = () => {
if (swState.$cache$) {
return swState.$cache$;
}
clearTimeout(cacheTimeout);
setTimeout(() => {
swState.$cache$ = null;
}, 5000);
return swScope.caches.open('QwikBundles');
};
swScope.addEventListener('fetch', (ev) => {
const request = ev.request;
if (request.method === 'GET') {
Expand All @@ -17,7 +28,9 @@ export const setupServiceWorker = (swScope: ServiceWorkerGlobalScope) => {
swState.$msgQueue$.push(ev.data);
drainMsgQueue(swState);
});
swScope.addEventListener('install', () => swScope.skipWaiting());
swScope.addEventListener('install', () => {
swScope.skipWaiting();
});
swScope.addEventListener('activate', (event) => {
let cacheTimeout: any;
swState.$getCache$ = () => {
Expand Down

0 comments on commit 857ebd6

Please sign in to comment.