Reactjs PWA突然停止安装-我应该改进缓存处理吗?怎么用?
我正在React中做一个PWA-我在这方面是一个noob,但PWA已经工作了好几个星期了,并且已经可以安装了。:) 突然我得到了这个信息: 无法安装站点:页面无法脱机工作。从Chrome93开始,可安装性标准正在改变,该网站将无法安装。有关更多信息,请参阅。 我想问题在于这家伙在这里写的是什么: 但我的技能太差,无法在我的服务人员中实施他的建议 如果你能看看下面我的服务人员,告诉我该换什么,我将不胜感激Reactjs PWA突然停止安装-我应该改进缓存处理吗?怎么用?,reactjs,caching,progressive-web-apps,service-worker,Reactjs,Caching,Progressive Web Apps,Service Worker,我正在React中做一个PWA-我在这方面是一个noob,但PWA已经工作了好几个星期了,并且已经可以安装了。:) 突然我得到了这个信息: 无法安装站点:页面无法脱机工作。从Chrome93开始,可安装性标准正在改变,该网站将无法安装。有关更多信息,请参阅。 我想问题在于这家伙在这里写的是什么: 但我的技能太差,无法在我的服务人员中实施他的建议 如果你能看看下面我的服务人员,告诉我该换什么,我将不胜感激 window.location.hostname === 'localhost' || w
window.location.hostname === 'localhost' ||
window.location.hostname === '[::1]' ||
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
)
);
export function register(config) {
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
if (publicUrl.origin !== window.location.origin) {
return;
}
window.addEventListener('load', () => {
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
if (isLocalhost) {
checkValidServiceWorker(swUrl, config);
navigator.serviceWorker.ready.then(() => {
console.log(
'This web app is being served cache-first by a service ' +
'worker. '
);
});
} else {
registerValidSW(swUrl, config);
}
});
}
}
function registerValidSW(swUrl, config) {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
return;
}
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
console.log(
'New content is available and will be used when all ' +
'tabs for this page are closed.'
);
if (config && config.onUpdate) {
config.onUpdate(registration);
}
} else {
console.log('Content is cached for offline use.');
if (config && config.onSuccess) {
config.onSuccess(registration);
}
}
}
};
};
})
.catch(error => {
console.error('Error during service worker registration:', error);
});
}
function checkValidServiceWorker(swUrl, config) {
fetch(swUrl, {
headers: { 'Service-Worker': 'script' }
})
.then(response => {
const contentType = response.headers.get('content-type');
if (
response.status === 404 ||
(contentType != null && contentType.indexOf('javascript') === -1)
) {
navigator.serviceWorker.ready.then(registration => {
registration.unregister().then(() => {
window.location.reload();
});
});
} else {
registerValidSW(swUrl, config);
}
})
.catch(() => {
console.log(
'No internet connection found. App is running in offline mode.'
);
});
}
export function unregister() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready
.then(registration => {
registration.unregister();
})
.catch(error => {
console.error(error.message);
});
}}
很抱歉这里的格式。不太擅长在Stackoverflow中粘贴代码