├── src ├── react-app-env.d.ts ├── setupTests.ts ├── App.test.tsx ├── index.css ├── index.tsx ├── App.css ├── utils │ └── data.ts ├── TodoList.tsx ├── App.tsx ├── logo.svg ├── TodoInput.tsx └── serviceWorker.ts ├── public ├── favicon.ico ├── logo192.png ├── logo512.png ├── robots.txt ├── manifest.json └── index.html ├── .gitignore ├── config-overrides.js ├── tsconfig.json ├── package.json └── README.md /src/react-app-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tuture-dev/typescript-tea/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tuture-dev/typescript-tea/HEAD/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tuture-dev/typescript-tea/HEAD/public/logo512.png -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /src/setupTests.ts: -------------------------------------------------------------------------------- 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.tsx: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /.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 | 25 | # Tuture-related files 26 | 27 | .tuture 28 | tuture-build -------------------------------------------------------------------------------- /src/index.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import * as serviceWorker from './serviceWorker'; 6 | 7 | ReactDOM.render(, document.getElementById('root')); 8 | 9 | // If you want your app to work offline and load faster, you can change 10 | // unregister() to register() below. Note this comes with some pitfalls. 11 | // Learn more about service workers: https://bit.ly/CRA-PWA 12 | serviceWorker.unregister(); 13 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | display: flex; 3 | flex-direction: column; 4 | align-items: center; 5 | padding-top: 60px; 6 | } 7 | 8 | .container { 9 | width: 600px; 10 | } 11 | 12 | .header { 13 | text-align: center; 14 | margin-bottom: 56px; 15 | } 16 | 17 | .header img { 18 | width: 160px; 19 | height: 160px; 20 | margin-bottom: 24px; 21 | } 22 | 23 | .todoInput { 24 | display: flex; 25 | flex-direction: row; 26 | align-items: center; 27 | height: 40px; 28 | border: 1px solid #434343; 29 | } 30 | 31 | .todoInput input { 32 | border: none; 33 | } 34 | -------------------------------------------------------------------------------- /config-overrides.js: -------------------------------------------------------------------------------- 1 | const { override, fixBabelImports, addLessLoader } = require("customize-cra"); 2 | const darkThemeVars = require("antd/dist/dark-theme"); 3 | 4 | module.exports = override( 5 | fixBabelImports("import", { 6 | libraryName: "antd", 7 | libraryDirectory: "es", 8 | style: true 9 | }), 10 | addLessLoader({ 11 | javascriptEnabled: true, 12 | modifyVars: { 13 | hack: `true;@import "${require.resolve( 14 | "antd/lib/style/color/colorPalette.less" 15 | )}";`, 16 | ...darkThemeVars, 17 | "@primary-color": "#02b875" 18 | } 19 | }) 20 | ); 21 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "dom", 6 | "dom.iterable", 7 | "esnext" 8 | ], 9 | "allowJs": true, 10 | "skipLibCheck": true, 11 | "esModuleInterop": true, 12 | "allowSyntheticDefaultImports": true, 13 | "strict": true, 14 | "forceConsistentCasingInFileNames": true, 15 | "module": "esnext", 16 | "moduleResolution": "node", 17 | "resolveJsonModule": true, 18 | "isolatedModules": true, 19 | "noEmit": true, 20 | "jsx": "react" 21 | }, 22 | "include": [ 23 | "src" 24 | ] 25 | } 26 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "typescript-tea", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@ant-design/icons": "^4.0.2", 7 | "@testing-library/jest-dom": "^4.2.4", 8 | "@testing-library/react": "^9.3.2", 9 | "@testing-library/user-event": "^7.1.2", 10 | "@types/jest": "^24.0.0", 11 | "@types/node": "^12.0.0", 12 | "@types/react": "^16.9.0", 13 | "@types/react-dom": "^16.9.0", 14 | "antd": "^4.0.0", 15 | "babel-plugin-import": "^1.13.0", 16 | "customize-cra": "^0.9.1", 17 | "less": "^3.11.1", 18 | "less-loader": "^5.0.0", 19 | "moment": "^2.24.0", 20 | "react": "^16.13.0", 21 | "react-app-rewired": "^2.1.5", 22 | "react-dom": "^16.13.0", 23 | "react-scripts": "3.4.0", 24 | "typescript": "~3.7.2" 25 | }, 26 | "scripts": { 27 | "start": "react-app-rewired start", 28 | "build": "react-app-rewired build", 29 | "test": "react-app-rewired test", 30 | "eject": "react-scripts eject" 31 | }, 32 | "eslintConfig": { 33 | "extends": "react-app" 34 | }, 35 | "browserslist": { 36 | "production": [ 37 | ">0.2%", 38 | "not dead", 39 | "not op_mini all" 40 | ], 41 | "development": [ 42 | "last 1 chrome version", 43 | "last 1 firefox version", 44 | "last 1 safari version" 45 | ] 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /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/utils/data.ts: -------------------------------------------------------------------------------- 1 | export interface Todo { 2 | id: string; 3 | user: string; 4 | date: string; 5 | content: string; 6 | isCompleted: boolean; 7 | } 8 | 9 | export interface User { 10 | id: string; 11 | name: string; 12 | avatar: string; 13 | } 14 | 15 | export function getUserById(userId: string) { 16 | return userList.filter(user => user.id === userId)[0]; 17 | } 18 | 19 | export const todoListData: Todo[] = [ 20 | { 21 | id: "1", 22 | content: "图雀社区:汇聚精彩的免费实战教程", 23 | user: "23410977", 24 | date: "2020年3月2日 19:34", 25 | isCompleted: false 26 | }, 27 | { 28 | id: "2", 29 | content: "图雀社区:汇聚精彩的免费实战教程", 30 | user: "23410976", 31 | date: "2020年3月2日 19:34", 32 | isCompleted: false 33 | }, 34 | { 35 | id: "3", 36 | content: "图雀社区:汇聚精彩的免费实战教程", 37 | user: "58352313", 38 | date: "2020年3月2日 19:34", 39 | isCompleted: false 40 | }, 41 | { 42 | id: "4", 43 | content: "图雀社区:汇聚精彩的免费实战教程", 44 | user: "25455350", 45 | date: "2020年3月2日 19:34", 46 | isCompleted: false 47 | }, 48 | { 49 | id: "5", 50 | content: "图雀社区:汇聚精彩的免费实战教程", 51 | user: "12345678", 52 | date: "2020年3月2日 19:34", 53 | isCompleted: true 54 | } 55 | ]; 56 | 57 | export const userList: User[] = [ 58 | { 59 | id: "666666666", 60 | name: "图雀社区", 61 | avatar: "https://avatars0.githubusercontent.com/u/39240800?s=60&v=4" 62 | }, 63 | { 64 | id: "23410977", 65 | name: "mRcfps", 66 | avatar: "https://avatars0.githubusercontent.com/u/23410977?s=96&v=4" 67 | }, 68 | { 69 | id: "25455350", 70 | name: "crxk", 71 | avatar: "https://avatars1.githubusercontent.com/u/25455350?s=96&v=4" 72 | }, 73 | { 74 | id: "23410976", 75 | name: "pftom", 76 | avatar: "https://avatars1.githubusercontent.com/u/26423749?s=88&v=4" 77 | }, 78 | { 79 | id: "58352313", 80 | name: "holy", 81 | avatar: "https://avatars0.githubusercontent.com/u/58352313?s=96&v=4" 82 | }, 83 | { 84 | id: "12345678", 85 | name: "pony", 86 | avatar: "https://avatars3.githubusercontent.com/u/25010151?s=96&v=4" 87 | } 88 | ]; 89 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `yarn start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `yarn test` 16 | 17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `yarn build` 21 | 22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | ### `yarn eject` 31 | 32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 33 | 34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 35 | 36 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 37 | 38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 39 | 40 | ## Learn More 41 | 42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 43 | 44 | To learn React, check out the [React documentation](https://reactjs.org/). 45 | -------------------------------------------------------------------------------- /src/TodoList.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { List, Avatar, Menu, Dropdown } from "antd"; 3 | import { DownOutlined } from "@ant-design/icons"; 4 | import { ClickParam } from "antd/lib/menu"; 5 | 6 | import { Todo, getUserById } from "./utils/data"; 7 | 8 | export type MenuKey = "complete" | "delete"; 9 | 10 | interface ActionProps { 11 | onClick: (key: MenuKey) => void; 12 | isCompleted: boolean; 13 | } 14 | 15 | function Action({ onClick, isCompleted }: ActionProps) { 16 | const handleActionClick = ({ key }: ClickParam) => { 17 | if (key === "complete") { 18 | onClick("complete"); 19 | } else if (key === "delete") { 20 | onClick("delete"); 21 | } 22 | }; 23 | 24 | return ( 25 | 26 | {isCompleted ? "重做" : "完成"} 27 | 删除 28 | 29 | ); 30 | } 31 | 32 | interface TodoListProps { 33 | todoList: Todo[]; 34 | onClick: (todoId: string, key: MenuKey) => void; 35 | } 36 | 37 | function TodoList({ todoList, onClick }: TodoListProps) { 38 | return ( 39 | { 44 | const user = getUserById(item.user); 45 | 46 | return ( 47 | ( 52 | onClick(item.id, key)} 55 | /> 56 | )} 57 | > 58 | 59 | 操作 60 | 61 | 62 | ]} 63 | > 64 | } 66 | title={{user.name}} 67 | description={item.date} 68 | /> 69 |
74 | {item.content} 75 |
76 |
77 | ); 78 | }} 79 | /> 80 | ); 81 | } 82 | 83 | export default TodoList; 84 | -------------------------------------------------------------------------------- /src/App.tsx: -------------------------------------------------------------------------------- 1 | import React, { useRef, useState } from "react"; 2 | import { Button, Typography, Form, Tabs } from "antd"; 3 | 4 | import TodoInput from "./TodoInput"; 5 | import TodoList from "./TodoList"; 6 | 7 | import { todoListData } from "./utils/data"; 8 | import { MenuKey } from "./TodoList"; 9 | 10 | import "./App.css"; 11 | import logo from "./logo.svg"; 12 | 13 | const { Title } = Typography; 14 | const { TabPane } = Tabs; 15 | 16 | function App() { 17 | const [todoList, setTodoList] = useState(todoListData); 18 | 19 | const callback = () => {}; 20 | 21 | const onFinish = (values: any) => { 22 | const newTodo = { ...values.todo, isCompleted: false }; 23 | setTodoList(todoList.concat(newTodo)); 24 | }; 25 | const ref = useRef(null); 26 | 27 | const activeTodoList = todoList.filter(todo => !todo.isCompleted); 28 | const completedTodoList = todoList.filter(todo => todo.isCompleted); 29 | 30 | const onClick = (todoId: string, key: MenuKey) => { 31 | if (key === "complete") { 32 | const newTodoList = todoList.map(todo => { 33 | if (todo.id === todoId) { 34 | return { ...todo, isCompleted: !todo.isCompleted }; 35 | } 36 | 37 | return todo; 38 | }); 39 | 40 | setTodoList(newTodoList); 41 | } else if (key === "delete") { 42 | const newTodoList = todoList.filter(todo => todo.id !== todoId); 43 | setTodoList(newTodoList); 44 | } 45 | }; 46 | 47 | return ( 48 |
49 |
50 | 51 | 图雀社区:汇聚精彩的免费实战教程 52 |
53 |
54 |
55 | 56 | 57 | 58 | 59 | 62 | 63 |
64 |
65 |
66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 |
78 |
79 | ); 80 | } 81 | 82 | export default App; 83 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Group Copy 5 | Created with Sketch. 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/TodoInput.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { Input, Select, DatePicker } from "antd"; 3 | import { Moment } from "moment"; 4 | 5 | import { userList } from "./utils/data"; 6 | 7 | const { Option } = Select; 8 | 9 | enum UserId { 10 | tuture = "666666666", 11 | mRcfps = "23410977", 12 | crxk = "25455350", 13 | pftom = "23410976", 14 | holy = "58352313" 15 | } 16 | 17 | export interface TodoValue { 18 | content?: string; 19 | user?: UserId; 20 | date?: string; 21 | } 22 | 23 | interface TodoInputProps { 24 | value?: TodoValue; 25 | onChange?: (value: TodoValue) => void; 26 | } 27 | 28 | interface TodoInputState { 29 | content: string; 30 | user: UserId; 31 | date: string; 32 | } 33 | 34 | class TodoInput extends React.Component { 35 | state = { 36 | content: "", 37 | user: UserId.tuture, 38 | date: "" 39 | }; 40 | 41 | private triggerChange = (changedValue: TodoValue) => { 42 | const { content, user, date } = this.state; 43 | const { value, onChange } = this.props; 44 | 45 | if (onChange) { 46 | onChange({ content, user, date, ...value, ...changedValue }); 47 | } 48 | }; 49 | 50 | private onContentChange = (e: React.ChangeEvent) => { 51 | const { value = {} } = this.props; 52 | 53 | if (!("content" in value!)) { 54 | console.log("hello"); 55 | this.setState({ 56 | content: e.target.value 57 | }); 58 | } 59 | 60 | this.triggerChange({ content: e.target.value }); 61 | }; 62 | 63 | private onUserChange = (selectValue: UserId) => { 64 | const { value = {} } = this.props; 65 | 66 | if (!("user" in value!)) { 67 | this.setState({ 68 | user: selectValue 69 | }); 70 | } 71 | 72 | this.triggerChange({ user: selectValue }); 73 | }; 74 | 75 | private onDateOk = (date: Moment) => { 76 | const { value = {} } = this.props; 77 | if (!("date" in value!)) { 78 | this.setState({ 79 | date: date.format("YYYY-MM-DD HH:mm") 80 | }); 81 | } 82 | 83 | this.triggerChange({ date: date.format("YYYY-MM-DD HH:mm") }); 84 | }; 85 | 86 | public render() { 87 | const { value } = this.props; 88 | const { content, user } = this.state; 89 | return ( 90 |
91 | 97 | 108 | 114 |
115 | ); 116 | } 117 | } 118 | 119 | export default TodoInput; 120 | -------------------------------------------------------------------------------- /src/serviceWorker.ts: -------------------------------------------------------------------------------- 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 | type Config = { 24 | onSuccess?: (registration: ServiceWorkerRegistration) => void; 25 | onUpdate?: (registration: ServiceWorkerRegistration) => void; 26 | }; 27 | 28 | export function register(config?: Config) { 29 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 30 | // The URL constructor is available in all browsers that support SW. 31 | const publicUrl = new URL( 32 | process.env.PUBLIC_URL, 33 | window.location.href 34 | ); 35 | if (publicUrl.origin !== window.location.origin) { 36 | // Our service worker won't work if PUBLIC_URL is on a different origin 37 | // from what our page is served on. This might happen if a CDN is used to 38 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 39 | return; 40 | } 41 | 42 | window.addEventListener('load', () => { 43 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 44 | 45 | if (isLocalhost) { 46 | // This is running on localhost. Let's check if a service worker still exists or not. 47 | checkValidServiceWorker(swUrl, config); 48 | 49 | // Add some additional logging to localhost, pointing developers to the 50 | // service worker/PWA documentation. 51 | navigator.serviceWorker.ready.then(() => { 52 | console.log( 53 | 'This web app is being served cache-first by a service ' + 54 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 55 | ); 56 | }); 57 | } else { 58 | // Is not localhost. Just register service worker 59 | registerValidSW(swUrl, config); 60 | } 61 | }); 62 | } 63 | } 64 | 65 | function registerValidSW(swUrl: string, config?: Config) { 66 | navigator.serviceWorker 67 | .register(swUrl) 68 | .then(registration => { 69 | registration.onupdatefound = () => { 70 | const installingWorker = registration.installing; 71 | if (installingWorker == null) { 72 | return; 73 | } 74 | installingWorker.onstatechange = () => { 75 | if (installingWorker.state === 'installed') { 76 | if (navigator.serviceWorker.controller) { 77 | // At this point, the updated precached content has been fetched, 78 | // but the previous service worker will still serve the older 79 | // content until all client tabs are closed. 80 | console.log( 81 | 'New content is available and will be used when all ' + 82 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 83 | ); 84 | 85 | // Execute callback 86 | if (config && config.onUpdate) { 87 | config.onUpdate(registration); 88 | } 89 | } else { 90 | // At this point, everything has been precached. 91 | // It's the perfect time to display a 92 | // "Content is cached for offline use." message. 93 | console.log('Content is cached for offline use.'); 94 | 95 | // Execute callback 96 | if (config && config.onSuccess) { 97 | config.onSuccess(registration); 98 | } 99 | } 100 | } 101 | }; 102 | }; 103 | }) 104 | .catch(error => { 105 | console.error('Error during service worker registration:', error); 106 | }); 107 | } 108 | 109 | function checkValidServiceWorker(swUrl: string, config?: Config) { 110 | // Check if the service worker can be found. If it can't reload the page. 111 | fetch(swUrl, { 112 | headers: { 'Service-Worker': 'script' } 113 | }) 114 | .then(response => { 115 | // Ensure service worker exists, and that we really are getting a JS file. 116 | const contentType = response.headers.get('content-type'); 117 | if ( 118 | response.status === 404 || 119 | (contentType != null && contentType.indexOf('javascript') === -1) 120 | ) { 121 | // No service worker found. Probably a different app. Reload the page. 122 | navigator.serviceWorker.ready.then(registration => { 123 | registration.unregister().then(() => { 124 | window.location.reload(); 125 | }); 126 | }); 127 | } else { 128 | // Service worker found. Proceed as normal. 129 | registerValidSW(swUrl, config); 130 | } 131 | }) 132 | .catch(() => { 133 | console.log( 134 | 'No internet connection found. App is running in offline mode.' 135 | ); 136 | }); 137 | } 138 | 139 | export function unregister() { 140 | if ('serviceWorker' in navigator) { 141 | navigator.serviceWorker.ready 142 | .then(registration => { 143 | registration.unregister(); 144 | }) 145 | .catch(error => { 146 | console.error(error.message); 147 | }); 148 | } 149 | } 150 | --------------------------------------------------------------------------------