gpt4 book ai didi

reactjs - 如何将事件监听器添加到 create-react-app 默认 sw.js 文件

转载 作者:行者123 更新时间:2023-12-03 14:11:38 24 4
gpt4 key购买 nike

我想使用 Create-React-App 模块的默认 service-worker (sw.js) 文件。

sw.js 文件类似于以下代码:

const isLocalhost = Boolean(
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) {
console.log('[Service Worker] Service Worker Registered!');
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(
'hello'
);
});
} 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(
'link'
);

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)
.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();
});
}
}

我想要的是向该文件添加更多事件监听器,我已经尝试了上述代码的不同部分来添加监听器,但它不起作用!作为示例,我添加了以下内容,但我不知道它应该放在哪里才能正常工作:

window.addEventListener('fetch', (e){
console.log('[service worker] fetch')
})

其他事件有installactivatebeforeinstallprompt等。

My main purpose of asking this question is to understand how to add install banner to my react project!

最佳答案

您无法在不弹出的情况下修改 create-react-app 中生成的服务工作人员文件。作为解决方法,您可以创建一个 sw-epilog.js 文件,在其中添加所有服务工作人员特定代码并在package.json 将该文件附加到生成的服务工作线程文件中,如 https://github.com/facebook/create-react-app/issues/5890#issuecomment-450915616 中所述。

我在这里有一个要点来演示这一点 https://gist.github.com/khaledosman/de3535c8873831153efdf6c10a4b4080查看最后两个文件

// sw-epilog.js
// Add a listener to receive messages from clients
self.addEventListener('message', function(event) {
// Force SW upgrade (activation of new installed SW version)
if ( event.data === 'skipWaiting' ) {
self.skipWaiting();
}
});
//package.json
"scripts": {
"build": "rm -rf build/ && react-scripts build && npm run-script sw-epilog",
"sw-epilog": "cat src/sw-epilog.js >> build/service-worker.js",
},

对于完整的实现,您还可以查看 https://github.com/khaledosman/create-react-pwa

关于reactjs - 如何将事件监听器添加到 create-react-app 默认 sw.js 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57185722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com