Create-react-app에는 서비스 작업자를 등록하는 코드가 포함 된 registerServiceWorker.js 파일이 함께 제공됩니다. 나는 그것이 어떻게 작동하는지 조금은 혼란 스럽다. 문제의 코드는 다음과 같습니다이 서비스 작업자 코드가 정확히 실행되도록 트리거 할 항목은 무엇입니까?
첫 콘솔 로그, 표시 하나 일 필요가 무엇function registerValidSW(swUrl) {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
registration.onupdatefound =() => {
const installingWorker = registration.installing;
installingWorker.onstatechange =() => {
if (installingWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
// At this point, the old content will have been purged and
// the fresh content will have been added to the cache.
// It's the perfect time to display a "New content is
// available; please refresh." message in your web app.
console.log('New content is available; please refresh.');
} else {
// At this point, everything has been precached.
// It's the perfect time to display a
// "Content is cached for offline use." message.
console.log('Content is cached for offline use.');
}
}
};
};
})
.catch(error => {
console.error('Error during service worker registration:', error);
});
}
, 표시 "새로운 콘텐츠를 사용할 수 있습니다, 새로 고치십시오"?
특히 스크립트 파일 이름이 변경되는 경우 index.html이 변경 될 때이 코드를 실행하여 어떻게 실행할 수 있습니까?
위대한 설명. 그래서 나는 이것에 대해 잘못된 길을 가고있는 것입니까? 'index.html'에서 변경을 감지 한 결과를 어떻게 얻을 수 있습니까? – NewbieX2
index.html에 대한 캐시 우선 접근 방식을 사용하지 않거나 인덱스가 변경 될 때마다 sw.js가 업데이트되도록하십시오. 또는 App Shell 아키텍처를 적용하여 index.html 파일을 자주 변경할 필요가 없다고 생각하는 것이 좋습니다. – NOtherDev
그래서 나는'registration.onupdatefound'가 완전히 새로운 신청서가 서비스 작업자로 등록되었을 때만 실행된다는 것을 읽고 있습니까? 이전에 사용한 파일이 변경된 경우가 아닙니까? 24 시간마다 "버전 변경"에 대해 무엇이 있었습니까? 캐시 된 것이고 캐시가 24 시간마다 만료된다고 말하는 것입니까? – bplittle