admin管理员组文章数量:1336320
Studying Progressive Web App using reactjs and I know that to create a new app there is a decent way to do that for making a new app using the below:
npx create-react-app my-app --template cra-template-pwa
Then, I read through the docs of crate-react-app and I know service-worker.js is opt-in due to adding difficulties to debuug...but at this point, I am wondering are there any ways to add the service-worker.js afterwards/to existing react app that do not contain it.
I am also curious whether I could create a new app, grab that service-worker.js file and then plug to any other react apps. In addition, is there any further configuration is needed in this way?
Thanks.
Studying Progressive Web App using reactjs and I know that to create a new app there is a decent way to do that for making a new app using the below:
npx create-react-app my-app --template cra-template-pwa
Then, I read through the docs of crate-react-app and I know service-worker.js is opt-in due to adding difficulties to debuug...but at this point, I am wondering are there any ways to add the service-worker.js afterwards/to existing react app that do not contain it.
I am also curious whether I could create a new app, grab that service-worker.js file and then plug to any other react apps. In addition, is there any further configuration is needed in this way?
Thanks.
Share Improve this question edited Nov 16, 2022 at 7:03 lunaayase asked Nov 16, 2022 at 7:00 lunaayaselunaayase 751 gold badge1 silver badge8 bronze badges 01 Answer
Reset to default 4if existing app has been create by create-react-app you can simply add service-worker.js file beside index.js because cra(create-react-app) by default searching for service-worker.js to add that to build directory and in index.js you can register your service-worker in this way :
if ('serviceWorker' in navigator) {
// Register a service worker hosted at the root of the
// site using the default scope.
navigator.serviceWorker.register(`${process.env.PUBLIC_URL}/service-worker.js`).then(
registration => {
console.log('Service worker registration succeeded:', registration);
},
/*catch*/ error => {
console.error(`Service worker registration failed: ${error}`);
}
);
} else {
console.error('Service workers are not supported.');
}
本文标签:
版权声明:本文标题:javascript - Is it possible to add service-worker.js to existing react app, if yes, how? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742241994a2438893.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论