简体   繁体   中英

Within React is registerServiceWorker.js Required?

As a newbie to React I'd like to know if registerServiceWorker.js is required.

I ask because I noticed (after spending countless hours looking) that it conflicts with what is being rendered on my DOM. Within my dev environment everything works. In production though things aren't as rosey. However, if I remove registerServiceWorker() then everything works in production.

Based on someone else's question I understand that it helps cache my assets.

I imagine you're using create-react-app? As far as I'm aware, it is required for now, but they are moving away from it, as per Dan Abranov's twitter .

They are definitely optional!

If you comment out registerServiceWorker() or remove it entirely from index.js it won't be included and you'll have a standard web app.

This means that you won't get the progressive enhancement--meaning (simply put) that your app will throw 500's in offline mode and will overall be a tad less snappy. Generally speaking turning it on is not great practice until the project you're working on is relatively stable as it caches assets so changes aren't always noticed immediately by users after a deploy

If you want to read up on the whole discourse of it being a default in create-react-app it was slightly controversial: https://github.com/facebook/create-react-app/issues/2398

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM