registerServiceWorker創建的React項目中的registerServiceWorker作用?


1、安裝create-react-app:npm/cnpm installl create-react-app -g

2、創建項目:create-react-app my-first-app

3、此時項目里包含一個registerServiceWorker.js文件,作用是什么呢?

 

  service worker是在后台運行的一個線程,可以用來處理離線緩存、消息推送、后台自動更新等任務。registerServiceWorker就是為react項目注冊了一個service worker,用來做資源的緩存,這樣你下次訪問時,就可以更快的獲取資源。而且因為資源被緩存,所以即使在離線的情況下也可以訪問應用(此時使用的資源是之前緩存的資源)。注意,registerServiceWorker注冊的service worker 只在生產環境中生效(process.env.NODE_ENV === 'production'),所以開發的時候,可以注釋掉,當然生產模式下,也可以不用這個功能。

 

// In production, we register a service worker to serve assets from local cache.

// This lets the app load faster on subsequent visits in production, and gives
// it offline capabilities. However, it also means that developers (and users)
// will only see deployed updates on the "N+1" visit to a page, since previously
// cached resources are updated in the background.

// To learn more about the benefits of this model, read https://goo.gl/KwvDNy.
// This link also includes instructions on opting out of this behavior.

 

參考:

1、https://segmentfault.com/q/1010000010667462


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM