Skip to content

Commit

Permalink
feat: sw 업데이트 확인 처음 10초는 1초마다
Browse files Browse the repository at this point in the history
  • Loading branch information
wildcatco committed Aug 9, 2023
1 parent dc6d003 commit 5e99056
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/serviceWorkerRegistration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
// To learn more about the benefits of this model and instructions on how to
// opt-in, read https://cra.link/PWA

import { setIntervalX } from '@/utils/set-interval-x-times';

const isLocalhost = Boolean(
window.location.hostname === 'localhost' ||
// [::1] is the IPv6 localhost address.
Expand Down Expand Up @@ -63,11 +65,19 @@ function registerValidSW(swUrl: string, config?: Config) {
navigator.serviceWorker
.register(swUrl)
.then((registration) => {
navigator.serviceWorker.register(swUrl).then((registration) =>
navigator.serviceWorker.register(swUrl).then((registration) => {
setIntervalX(
() => {
registration.update();
},
1000,
9,
);

setInterval(() => {
registration.update();
}, 1000 * 10),
);
}, 1000 * 10);
});
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
Expand Down
14 changes: 14 additions & 0 deletions src/utils/set-interval-x-times.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function setIntervalX(
callback: () => void,
delay: number,
repetitions: number,
) {
let x = 0;
const intervalID = setInterval(() => {
callback();

if (++x === repetitions) {
window.clearInterval(intervalID);
}
}, delay);
}

0 comments on commit 5e99056

Please sign in to comment.