├── public ├── robots.txt ├── favicon.ico ├── logo192.png ├── logo512.png ├── manifest.json └── index.html ├── .vscode └── settings.json ├── src ├── setupTests.js ├── App.test.js ├── index.css ├── reportWebVitals.js ├── components │ ├── UnitControl.js │ ├── CardFooter.js │ └── UnitConverter.js ├── index.js ├── App.js ├── App.css ├── logo.svg └── serviceWorker.js ├── .gitignore ├── package.json ├── README.md ├── .eslintcache └── template.html /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "cSpell.words": [ 3 | "Mbps", 4 | "fontawesome", 5 | "fortawesome" 6 | ] 7 | } -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pjchender/learn-react-from-hooks-internet-speed-converter/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pjchender/learn-react-from-hooks-internet-speed-converter/HEAD/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pjchender/learn-react-from-hooks-internet-speed-converter/HEAD/public/logo512.png -------------------------------------------------------------------------------- /src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom/extend-expect'; 6 | -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { render } from '@testing-library/react'; 3 | import App from './App'; 4 | 5 | test('renders learn react link', () => { 6 | const { getByText } = render(); 7 | const linkElement = getByText(/learn react/i); 8 | expect(linkElement).toBeInTheDocument(); 9 | }); 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /src/reportWebVitals.js: -------------------------------------------------------------------------------- 1 | const reportWebVitals = onPerfEntry => { 2 | if (onPerfEntry && onPerfEntry instanceof Function) { 3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { 4 | getCLS(onPerfEntry); 5 | getFID(onPerfEntry); 6 | getFCP(onPerfEntry); 7 | getLCP(onPerfEntry); 8 | getTTFB(onPerfEntry); 9 | }); 10 | } 11 | }; 12 | 13 | export default reportWebVitals; 14 | -------------------------------------------------------------------------------- /src/components/UnitControl.js: -------------------------------------------------------------------------------- 1 | import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; 2 | import React from 'react'; 3 | 4 | const UnitControl = () => ( 5 |
6 |
Mbps
7 | 8 | 9 | 10 | 11 |
MB/s
12 |
13 | ); 14 | 15 | export default UnitControl; 16 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom/client'; 3 | import './index.css'; 4 | import App from './App'; 5 | import reportWebVitals from './reportWebVitals'; 6 | 7 | const root = ReactDOM.createRoot(document.getElementById('root')); 8 | root.render( 9 | 10 | 11 | 12 | ); 13 | 14 | // If you want to start measuring performance in your app, pass a function 15 | // to log results (for example: reportWebVitals(console.log)) 16 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals 17 | reportWebVitals(); 18 | -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /src/components/CardFooter.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | const CardFooter = (props) => { 4 | const { inputValue } = props; 5 | 6 | let criteria = {}; 7 | 8 | if (!inputValue) { 9 | criteria = { 10 | title: '---', 11 | backgroundColor: '#d3d8e2', 12 | }; 13 | } else if (inputValue < 15) { 14 | criteria = { 15 | title: 'SLOW', 16 | backgroundColor: '#ee362d', 17 | }; 18 | } else if (inputValue < 40) { 19 | criteria = { 20 | title: 'GOOD', 21 | backgroundColor: '#1b82f1', 22 | }; 23 | } else if (inputValue >= 40) { 24 | criteria = { 25 | title: 'FAST', 26 | backgroundColor: '#13d569', 27 | }; 28 | } 29 | 30 | return ( 31 |
37 | {criteria.title} 38 |
39 | ); 40 | }; 41 | 42 | export default CardFooter; 43 | -------------------------------------------------------------------------------- /src/components/UnitConverter.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; 3 | 4 | const UnitConverter = ({ handleInputChange, inputValue }) => ( 5 |
6 |
7 |
Set
8 | 15 |
16 | 17 | 18 | 19 |
20 |
Show
21 | 27 |
28 |
29 | ); 30 | 31 | export default UnitConverter; 32 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import './App.css'; 2 | 3 | import { useState } from 'react'; 4 | 5 | import CardFooter from './components/CardFooter'; 6 | import UnitControl from './components/UnitControl'; 7 | import UnitConverter from './components/UnitConverter'; 8 | import { fab } from '@fortawesome/free-brands-svg-icons'; 9 | import { far } from '@fortawesome/free-regular-svg-icons'; 10 | import { fas } from '@fortawesome/free-solid-svg-icons'; 11 | import { library } from '@fortawesome/fontawesome-svg-core'; 12 | 13 | library.add(fab, fas, far); 14 | 15 | function App() { 16 | const [inputValue, setInputValue] = useState(0); 17 | 18 | const handleInputChange = (e) => { 19 | const { value } = e.target; 20 | setInputValue(value); 21 | }; 22 | 23 | return ( 24 |
25 |
Network Speed Converter
26 |
27 | 28 | 32 |
33 | 34 |
35 | ); 36 | } 37 | 38 | export default App; 39 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "internet-speed-converter", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@fortawesome/fontawesome-free": "^5.15.1", 7 | "@fortawesome/fontawesome-svg-core": "^1.2.32", 8 | "@fortawesome/free-brands-svg-icons": "^5.15.1", 9 | "@fortawesome/free-regular-svg-icons": "^5.15.1", 10 | "@fortawesome/free-solid-svg-icons": "^5.15.1", 11 | "@fortawesome/react-fontawesome": "^0.1.14", 12 | "@testing-library/jest-dom": "^5.16.5", 13 | "@testing-library/react": "^13.3.0", 14 | "@testing-library/user-event": "^13.5.0", 15 | "react": "^18.2.0", 16 | "react-dom": "^18.2.0", 17 | "react-scripts": "5.0.1", 18 | "web-vitals": "^2.1.4" 19 | }, 20 | "scripts": { 21 | "start": "react-scripts start", 22 | "build": "react-scripts build", 23 | "test": "react-scripts test", 24 | "eject": "react-scripts eject" 25 | }, 26 | "eslintConfig": { 27 | "extends": "react-app" 28 | }, 29 | "browserslist": { 30 | "production": [ 31 | ">0.2%", 32 | "not dead", 33 | "not op_mini all" 34 | ], 35 | "development": [ 36 | "last 1 chrome version", 37 | "last 1 firefox version", 38 | "last 1 safari version" 39 | ] 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 網速傻傻分不清楚 Mbps? MB/s? 來寫個單位換算器吧 2 | 3 | ## 網速的概念 4 | 5 | 在開始實作前,我們要先來了解一下網速的概念。其中,`Mbps` 是用來計算網路頻寬最常見的單位,自從大家升級到 4G 行動網路,甚至即將邁入的 5G,如果你不是使用吃到飽的使用者,勢必會看到「降速」或「限速」這兩個詞,可是你有想過最常見的降速 `20Mbps`、或者限速 `5Mbps` 是什麼意思嗎? 6 | 7 | 但因為最常接觸到的單位常常是 `MB`,因為這常用在傳輸檔案或儲存空間上。因此有些不太清楚的店員可能會跟你說 `20Mbps` 就是每秒鐘有 `20MB` 的網路傳輸速度,但真的是這樣嗎?錯!**20Mbps 完全不等於每秒鐘有 20MB 的網路傳輸量**。 8 | 9 | ![imgur](https://i.imgur.com/H9dBJNF.png) 10 | 11 | 又或者你現在想要追一下最近很紅的韓劇,怕網速太慢影響看劇的興致,於是打開 Netflix 提供的測速網站 [fast.com](https://fast.com/) 想要測一下網速: 12 | 13 | ![imgur](https://i.imgur.com/87jdVjv.png) 14 | 15 | 測完發現有 `300Mbps`,可是你有想過,這個 `300Mbps` 是什麼意思嗎?這絕對不是表示每秒鐘有 300MB 的網路頻寬。 16 | 17 | `Mbps` 和 `MB/s` 雖然不同,但這兩個單位之間是可以轉換的。那麼多少 `Mbps` 才會等於 `1 MB/s` 呢? 18 | 19 | ## Mbps 或 MB/s 20 | 21 | 要做網速單位換算器之前,我們要先來了解 `Mbps` 到底是什麼意思,它又要怎麼轉換成 `MB/s`。 22 | 23 | 實際上 `Mbps` 中的第一個 `M` 是英文的 million,也就是「百萬」; `小寫 b` 是 `bit` 的意思,中文稱作「**位元**」;後面的 `ps` 則是 per second 的意思,也就是「每秒」。綜合起來,**`Mbps` 指的是「每秒鐘可以傳輸多少百萬位元(Million bits per second)」**。 24 | 25 | 那 `MB/s` 呢?這裡第一個 `M` 一樣式 million 百萬的意思;但 `B` 則是大寫的 `B` ,**大寫的 B 和小寫的 b 在意思上是完全不同的**,`大寫 B` 是指 `Byte` ,中文稱作「**位元組**」。**一個位元組(Byte)需要由 8 個位元(bit)所組成的**。 26 | 27 | 所以實際上 **`Mbps` 的值需要「除以 8 」後才會是指每秒鐘可以有多少 MB 的傳輸量**。也就是說至少要到 `8Mbps` 以上,才表示你的網路速度每秒鐘可以傳輸 `1MB` 以上: 28 | 29 | ![imgur](https://i.imgur.com/4Uglubs.png) 30 | 31 | 在這個章節中就讓我們來做一個網速單位換算器吧! 32 | 33 | ![imgur](https://i.imgur.com/YuQvUMP.png) 34 | 35 | ## 網速單位換算器 UI 36 | 37 | [https://codepen.io/PJCHENder/pen/LYGbzxz](https://codepen.io/PJCHENder/pen/LYGbzxz) 38 | 39 | ## 安裝 FontAwesome 40 | 41 | ```bash 42 | $ npm i --save @fortawesome/fontawesome-svg-core @fortawesome/react- fontawesome @fortawesome/free-regular-svg-icons @fortawesome/free- brands-svg-icons @fortawesome/free-solid-svg-icons 43 | ``` 44 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | /** 2 | * Design Credit: 3 | * https://dribbble.com/shots/4241423-Exchanger-landing-page 4 | **/ 5 | 6 | html, 7 | body { 8 | margin: 0; 9 | padding: 0; 10 | height: 100%; 11 | width: 100%; 12 | display: flex; 13 | align-items: center; 14 | justify-content: center; 15 | font-family: Verdana; 16 | background: #1b6ca8; 17 | background: linear-gradient(237deg, #134c76 48%, #175c8f 85%, #186197 100%); 18 | } 19 | 20 | .container { 21 | display: flex; 22 | flex-direction: column; 23 | box-shadow: 1px 1px 10px 1px #134c76; 24 | border-radius: 7px; 25 | background: white; 26 | min-width: 440px; 27 | min-height: 320px; 28 | } 29 | 30 | .card-header { 31 | display: flex; 32 | align-items: center; 33 | justify-content: center; 34 | min-height: 50px; 35 | border-top-left-radius: 6px; 36 | border-top-right-radius: 6px; 37 | background-color: #0a97b0; 38 | color: white; 39 | font-size: 20px; 40 | } 41 | 42 | .card-body { 43 | flex: 1; 44 | padding: 30px; 45 | } 46 | 47 | .unit-control { 48 | display: flex; 49 | align-items: center; 50 | justify-content: space-between; 51 | margin-bottom: 45px; 52 | } 53 | 54 | .unit { 55 | display: inline-flex; 56 | background-color: #0a97b0; 57 | align-items: center; 58 | justify-content: center; 59 | width: 75px; 60 | height: 35px; 61 | color: white; 62 | border-radius: 3px; 63 | } 64 | 65 | .exchange-icon { 66 | color: #6886c5; 67 | } 68 | 69 | .angle-icon { 70 | color: #6886c5; 71 | } 72 | 73 | .converter { 74 | display: flex; 75 | align-items: center; 76 | justify-content: space-between; 77 | } 78 | 79 | .converter-title { 80 | color: #6886c5; 81 | font-weight: bold; 82 | margin-bottom: 12px; 83 | } 84 | 85 | .card-footer { 86 | min-height: 40px; 87 | margin: 5px; 88 | border-bottom-left-radius: 6px; 89 | border-bottom-right-radius: 6px; 90 | background-color: #0a97b0; 91 | display: flex; 92 | align-items: center; 93 | justify-content: center; 94 | color: white; 95 | } 96 | 97 | .input-number { 98 | font-size: 32px; 99 | max-width: 170px; 100 | flex: 1; 101 | display: inline-block; 102 | background: transparent; 103 | border: none; 104 | outline: none; 105 | font-family: Courier; 106 | color: #0a97b0; 107 | font-weight: bold; 108 | } 109 | 110 | .text-right { 111 | text-align: right; 112 | } 113 | 114 | .flex-1 { 115 | flex: 1; 116 | } 117 | -------------------------------------------------------------------------------- /.eslintcache: -------------------------------------------------------------------------------- 1 | [{"/Users/pjchender/Projects/learn-react-from-hooks/learn-react-from-hooks-internet-speed-converter/src/index.js":"1","/Users/pjchender/Projects/learn-react-from-hooks/learn-react-from-hooks-internet-speed-converter/src/App.js":"2","/Users/pjchender/Projects/learn-react-from-hooks/learn-react-from-hooks-internet-speed-converter/src/serviceWorker.js":"3","/Users/pjchender/Projects/learn-react-from-hooks/learn-react-from-hooks-internet-speed-converter/src/components/CardFooter.js":"4","/Users/pjchender/Projects/learn-react-from-hooks/learn-react-from-hooks-internet-speed-converter/src/components/UnitControl.js":"5","/Users/pjchender/Projects/learn-react-from-hooks/learn-react-from-hooks-internet-speed-converter/src/components/UnitConverter.js":"6"},{"size":503,"mtime":1609902657777,"results":"7","hashOfConfig":"8"},{"size":1035,"mtime":1609902657776,"results":"9","hashOfConfig":"8"},{"size":5086,"mtime":1609902657777,"results":"10","hashOfConfig":"8"},{"size":754,"mtime":1609902657776,"results":"11","hashOfConfig":"8"},{"size":490,"mtime":1609902657776,"results":"12","hashOfConfig":"8"},{"size":845,"mtime":1609902657776,"results":"13","hashOfConfig":"8"},{"filePath":"14","messages":"15","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"kwzf9d",{"filePath":"16","messages":"17","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"18","messages":"19","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"20","messages":"21","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"22","messages":"23","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"24","messages":"25","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/Users/pjchender/Projects/learn-react-from-hooks/learn-react-from-hooks-internet-speed-converter/src/index.js",[],"/Users/pjchender/Projects/learn-react-from-hooks/learn-react-from-hooks-internet-speed-converter/src/App.js",[],"/Users/pjchender/Projects/learn-react-from-hooks/learn-react-from-hooks-internet-speed-converter/src/serviceWorker.js",[],"/Users/pjchender/Projects/learn-react-from-hooks/learn-react-from-hooks-internet-speed-converter/src/components/CardFooter.js",[],"/Users/pjchender/Projects/learn-react-from-hooks/learn-react-from-hooks-internet-speed-converter/src/components/UnitControl.js",[],"/Users/pjchender/Projects/learn-react-from-hooks/learn-react-from-hooks-internet-speed-converter/src/components/UnitConverter.js",[]] -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /template.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Document 8 | 129 | 130 | 131 | 132 | 133 |
134 |
Network Speed Converter
135 |
136 |
137 |
Mbps
138 | 139 | 140 | 141 | 142 |
MB/s
143 |
144 |
145 |
146 |
Set
147 | 148 |
149 | 150 | 151 | 152 |
153 |
Show
154 | 155 |
156 |
157 |
158 | 159 |
160 | 161 | 162 | 163 | -------------------------------------------------------------------------------- /src/serviceWorker.js: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.0/8 are considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then(registration => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | if (installingWorker == null) { 64 | return; 65 | } 66 | installingWorker.onstatechange = () => { 67 | if (installingWorker.state === 'installed') { 68 | if (navigator.serviceWorker.controller) { 69 | // At this point, the updated precached content has been fetched, 70 | // but the previous service worker will still serve the older 71 | // content until all client tabs are closed. 72 | console.log( 73 | 'New content is available and will be used when all ' + 74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 75 | ); 76 | 77 | // Execute callback 78 | if (config && config.onUpdate) { 79 | config.onUpdate(registration); 80 | } 81 | } else { 82 | // At this point, everything has been precached. 83 | // It's the perfect time to display a 84 | // "Content is cached for offline use." message. 85 | console.log('Content is cached for offline use.'); 86 | 87 | // Execute callback 88 | if (config && config.onSuccess) { 89 | config.onSuccess(registration); 90 | } 91 | } 92 | } 93 | }; 94 | }; 95 | }) 96 | .catch(error => { 97 | console.error('Error during service worker registration:', error); 98 | }); 99 | } 100 | 101 | function checkValidServiceWorker(swUrl, config) { 102 | // Check if the service worker can be found. If it can't reload the page. 103 | fetch(swUrl, { 104 | headers: { 'Service-Worker': 'script' }, 105 | }) 106 | .then(response => { 107 | // Ensure service worker exists, and that we really are getting a JS file. 108 | const contentType = response.headers.get('content-type'); 109 | if ( 110 | response.status === 404 || 111 | (contentType != null && contentType.indexOf('javascript') === -1) 112 | ) { 113 | // No service worker found. Probably a different app. Reload the page. 114 | navigator.serviceWorker.ready.then(registration => { 115 | registration.unregister().then(() => { 116 | window.location.reload(); 117 | }); 118 | }); 119 | } else { 120 | // Service worker found. Proceed as normal. 121 | registerValidSW(swUrl, config); 122 | } 123 | }) 124 | .catch(() => { 125 | console.log( 126 | 'No internet connection found. App is running in offline mode.' 127 | ); 128 | }); 129 | } 130 | 131 | export function unregister() { 132 | if ('serviceWorker' in navigator) { 133 | navigator.serviceWorker.ready 134 | .then(registration => { 135 | registration.unregister(); 136 | }) 137 | .catch(error => { 138 | console.error(error.message); 139 | }); 140 | } 141 | } 142 | --------------------------------------------------------------------------------