Skip to content

Commit

Permalink
fix: change sw
Browse files Browse the repository at this point in the history
  • Loading branch information
wildcatco committed Aug 24, 2023
1 parent 768ed43 commit 937bcb7
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 11 deletions.
18 changes: 18 additions & 0 deletions public/my-sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker
.getRegistrations()
.then((registrations) => {
for (const registration of registrations) {
registration.unregister();
}
})
.then(() => {
navigator.serviceWorker
.register('/serviceWorker.js')
.then((registration) => {
registration.waiting.postMessage({ type: 'SKIP_WAITING' });
});
});
});
}
18 changes: 18 additions & 0 deletions public/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker
.getRegistrations()
.then((registrations) => {
for (const registration of registrations) {
registration.unregister();
}
})
.then(() => {
navigator.serviceWorker
.register('/serviceWorker.js')
.then((registration) => {
registration.waiting.postMessage({ type: 'SKIP_WAITING' });
});
});
});
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ auth_check: "/auth-check", // HOC적용이 힘든 케이스에 사용할 검증
┃ ┣ 📜logo.svg # 미사용
┃ ┣ 📜react-app-env.d.ts # 미사용
┃ ┣ 📜reportWebVitals.ts # 미사용
┃ ┣ 📜my-sw.ts # PWA를 위한 서비스 워커
┃ ┣ 📜serviceWorker.ts # PWA를 위한 서비스 워커
┃ ┣ 📜serviceWorkerRegistration.ts # PWA를 위한 서비스 워커
┃ ┣ 📜setupTests.ts # 미사용
┃ ┗ 📜version.js # 버전 커밋
Expand Down
1 change: 1 addition & 0 deletions src/common/hooks/useUpdate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export function useUpdate() {
const { updateServiceWorker } = useRegisterSW({
onRegistered: (r) => {
if (!r) return;

r.update();
setInterval(() => {
r.update();
Expand Down
9 changes: 0 additions & 9 deletions src/my-sw.ts

This file was deleted.

16 changes: 16 additions & 0 deletions src/serviceWorker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { clientsClaim } from 'workbox-core';
import { cleanupOutdatedCaches, precacheAndRoute } from 'workbox-precaching';

declare let self: ServiceWorkerGlobalScope;

cleanupOutdatedCaches();

precacheAndRoute(self.__WB_MANIFEST);

self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
console.log('here');
self.skipWaiting();
clientsClaim();
}
});
2 changes: 1 addition & 1 deletion vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default defineConfig({
globPatterns: ['**/*.{js,css,html,ico,png,svg,woff2,otf}'],
},
srcDir: 'src',
filename: 'my-sw.ts',
filename: 'serviceWorker.ts',
manifestFilename: 'manifest.json',
}),
],
Expand Down

0 comments on commit 937bcb7

Please sign in to comment.