├── .firebaserc ├── public ├── robots.txt ├── favicon.ico ├── logo192.png ├── logo512.png ├── manifest.json └── index.html ├── src ├── App.css ├── SidebarOption.js ├── App.js ├── index.css ├── SidebarOption.css ├── index.js ├── firebase.js ├── Sidebar.css ├── Feed.css ├── Widgets.css ├── Post.css ├── TweetBox.css ├── Feed.js ├── Widgets.js ├── Sidebar.js ├── Post.js ├── TweetBox.js └── serviceWorker.js ├── firebase.json ├── .gitignore ├── package.json ├── .firebase └── hosting.YnVpbGQ.cache └── README.md /.firebaserc: -------------------------------------------------------------------------------- 1 | { 2 | "projects": { 3 | "default": "twitter-clone-1faac" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CleverProgrammers/twitter-clone/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CleverProgrammers/twitter-clone/HEAD/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CleverProgrammers/twitter-clone/HEAD/public/logo512.png -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | body { 2 | --twitter-color: #50b7f5; 3 | --twitter-background: #e6ecf0; 4 | } 5 | 6 | .app { 7 | display: flex; 8 | height: 100vh; 9 | max-width: 1300px; 10 | margin-left: auto; 11 | margin-right: auto; 12 | padding: 0 10px; 13 | } 14 | -------------------------------------------------------------------------------- /firebase.json: -------------------------------------------------------------------------------- 1 | { 2 | "hosting": { 3 | "public": "build", 4 | "ignore": [ 5 | "firebase.json", 6 | "**/.*", 7 | "**/node_modules/**" 8 | ], 9 | "rewrites": [ 10 | { 11 | "source": "**", 12 | "destination": "/index.html" 13 | } 14 | ] 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/SidebarOption.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import "./SidebarOption.css"; 3 | 4 | function SidebarOption({ active, text, Icon }) { 5 | return ( 6 |
7 | 8 |

{text}

9 |
10 | ); 11 | } 12 | 13 | export default SidebarOption; 14 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import Sidebar from "./Sidebar"; 3 | import Feed from "./Feed"; 4 | import Widgets from "./Widgets"; 5 | import "./App.css"; 6 | 7 | function App() { 8 | return ( 9 | // BEM 10 |
11 | 12 | 13 | 14 |
15 | ); 16 | } 17 | 18 | export default App; 19 | -------------------------------------------------------------------------------- /.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 | * { 2 | margin: 0; 3 | } 4 | 5 | body { 6 | margin: 0; 7 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", 8 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", 9 | sans-serif; 10 | -webkit-font-smoothing: antialiased; 11 | -moz-osx-font-smoothing: grayscale; 12 | } 13 | 14 | code { 15 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", 16 | monospace; 17 | } 18 | -------------------------------------------------------------------------------- /src/SidebarOption.css: -------------------------------------------------------------------------------- 1 | .sidebarOption { 2 | display: flex; 3 | align-items: center; 4 | cursor: pointer; 5 | } 6 | 7 | .sidebarOption:hover { 8 | background-color: #e8f5fe; 9 | border-radius: 30px; 10 | color: var(--twitter-color); 11 | transition: color 100ms ease-out; 12 | } 13 | 14 | .sidebarOption > .MuiSvgIcon-root { 15 | padding: 20px; 16 | } 17 | 18 | .sidebarOption > h2 { 19 | font-weight: 800; 20 | font-size: 20px; 21 | margin-right: 20px; 22 | } 23 | 24 | .sidebarOption--active { 25 | color: var(--twitter-color); 26 | } 27 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 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( 8 | 9 | 10 | , 11 | document.getElementById('root') 12 | ); 13 | 14 | // If you want your app to work offline and load faster, you can change 15 | // unregister() to register() below. Note this comes with some pitfalls. 16 | // Learn more about service workers: https://bit.ly/CRA-PWA 17 | serviceWorker.unregister(); 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/firebase.js: -------------------------------------------------------------------------------- 1 | import firebase from "firebase"; 2 | 3 | const firebaseConfig = { 4 | apiKey: "AIzaSyCw-li0IscS2rJr68dSjoJL3KM3eFq5PfE", 5 | authDomain: "twitter-clone-1faac.firebaseapp.com", 6 | databaseURL: "https://twitter-clone-1faac.firebaseio.com", 7 | projectId: "twitter-clone-1faac", 8 | storageBucket: "twitter-clone-1faac.appspot.com", 9 | messagingSenderId: "316208638057", 10 | appId: "1:316208638057:web:75a37bf24fab32ff145af8", 11 | measurementId: "G-HGFP2LDXP9", 12 | }; 13 | 14 | const firebaseApp = firebase.initializeApp(firebaseConfig); 15 | 16 | const db = firebaseApp.firestore(); 17 | 18 | export default db; 19 | -------------------------------------------------------------------------------- /src/Sidebar.css: -------------------------------------------------------------------------------- 1 | .sidebar { 2 | border-right: 1px solid var(--twitter-background); 3 | flex: 0.3; 4 | /* min-width: 250px; */ 5 | margin-top: 20px; 6 | padding-left: 20px; 7 | padding-right: 20px; 8 | } 9 | 10 | .sidebar__twitterIcon { 11 | color: var(--twitter-color); 12 | font-size: 30px !important; 13 | margin-left: 20px; 14 | margin-bottom: 20px; 15 | } 16 | 17 | .sidebar__tweet { 18 | background-color: var(--twitter-color) !important; 19 | border: none !important; 20 | color: white !important; 21 | font-weight: 900 !important; 22 | text-transform: inherit !important; 23 | border-radius: 30px !important; 24 | height: 50px !important; 25 | margin-top: 20px !important; 26 | } 27 | -------------------------------------------------------------------------------- /src/Feed.css: -------------------------------------------------------------------------------- 1 | .feed { 2 | flex: 0.4; 3 | border-right: 1px solid var(--twitter-background); 4 | min-width: fit-content; 5 | overflow-y: scroll; 6 | } 7 | 8 | /* Hide scrollbar for Chrome, Safari and Opera */ 9 | .feed::-webkit-scrollbar { 10 | display: none; 11 | } 12 | 13 | /* Hide scrollbar for IE, Edge and Firefox */ 14 | .feed { 15 | -ms-overflow-style: none; /* IE and Edge */ 16 | scrollbar-width: none; /* Firefox */ 17 | } 18 | 19 | .feed__header { 20 | position: sticky; 21 | top: 0; 22 | background-color: white; 23 | z-index: 100; 24 | border: 1px solid var(--twitter-background); 25 | padding: 15px 20px; 26 | } 27 | 28 | .feed__header > h2 { 29 | font-size: 20px; 30 | font-weight: 800; 31 | } 32 | -------------------------------------------------------------------------------- /src/Widgets.css: -------------------------------------------------------------------------------- 1 | .widgets { 2 | flex: 0.3; 3 | } 4 | 5 | .widgets__input { 6 | display: flex; 7 | align-items: center; 8 | background-color: var(--twitter-background); 9 | padding: 10px; 10 | border-radius: 20px; 11 | margin-top: 10px; 12 | margin-left: 20px; 13 | } 14 | 15 | .widgets__input > input { 16 | border: none; 17 | background-color: var(--twitter-background); 18 | } 19 | 20 | .widgets__searchIcon { 21 | color: gray; 22 | } 23 | 24 | .widgets__widgetContainer { 25 | margin-top: 15px; 26 | margin-left: 20px; 27 | padding: 20px; 28 | background-color: #f5f8fa; 29 | border-radius: 20px; 30 | } 31 | 32 | .widgets__widgetContainer > h2 { 33 | font-size: 18px; 34 | font-weight: 800; 35 | } 36 | -------------------------------------------------------------------------------- /src/Post.css: -------------------------------------------------------------------------------- 1 | .post { 2 | display: flex; 3 | align-items: flex-start; 4 | border-bottom: 1px solid var(--twitter-background); 5 | padding-bottom: 10px; 6 | } 7 | 8 | .post__body { 9 | flex: 1; 10 | padding: 10px; 11 | } 12 | 13 | .post__body > img { 14 | border-radius: 20px; 15 | } 16 | 17 | .post__footer { 18 | display: flex; 19 | justify-content: space-between; 20 | margin-top: 20px; 21 | } 22 | 23 | .post__headerDescription { 24 | margin-bottom: 10px; 25 | font-size: 15px; 26 | } 27 | 28 | .post__headerText > h3 { 29 | font-size: 15px; 30 | margin-bottom: 5px; 31 | } 32 | 33 | .post__badge { 34 | font-size: 14px !important; 35 | color: var(--twitter-color); 36 | } 37 | 38 | .post__headerSpecial { 39 | font-weight: 600; 40 | font-size: 12px; 41 | color: gray; 42 | } 43 | 44 | .post__avatar { 45 | padding: 20px; 46 | } 47 | -------------------------------------------------------------------------------- /src/TweetBox.css: -------------------------------------------------------------------------------- 1 | .tweetBox { 2 | padding-bottom: 10px; 3 | border-bottom: 8px solid var(--twitter-background); 4 | padding-right: 10px; 5 | } 6 | 7 | .tweetBox > form { 8 | display: flex; 9 | flex-direction: column; 10 | } 11 | 12 | .tweetBox__input { 13 | padding: 20px; 14 | display: flex; 15 | } 16 | 17 | .tweetBox__input > input { 18 | flex: 1; 19 | margin-left: 20px; 20 | font-size: 20px; 21 | border: none; 22 | } 23 | 24 | .tweetBox__imageInput { 25 | border: none; 26 | padding: 10px; 27 | } 28 | 29 | .tweetBox__tweetButton { 30 | background-color: var(--twitter-color) !important; 31 | border: none !important; 32 | color: white !important; 33 | font-weight: 900 !important; 34 | text-transform: inherit !important; 35 | border-radius: 30px !important; 36 | width: 80px; 37 | height: 40px !important; 38 | margin-top: 20px !important; 39 | margin-left: auto !important; 40 | } 41 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "twitter-clone", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@material-ui/core": "^4.11.0", 7 | "@material-ui/icons": "^4.9.1", 8 | "@testing-library/jest-dom": "^4.2.4", 9 | "@testing-library/react": "^9.5.0", 10 | "@testing-library/user-event": "^7.2.1", 11 | "firebase": "^7.17.2", 12 | "react": "^16.13.1", 13 | "react-dom": "^16.13.1", 14 | "react-flip-move": "^3.0.4", 15 | "react-scripts": "3.4.1", 16 | "react-twitter-embed": "^3.0.3" 17 | }, 18 | "scripts": { 19 | "start": "react-scripts start", 20 | "build": "react-scripts build", 21 | "test": "react-scripts test", 22 | "eject": "react-scripts eject" 23 | }, 24 | "eslintConfig": { 25 | "extends": "react-app" 26 | }, 27 | "browserslist": { 28 | "production": [ 29 | ">0.2%", 30 | "not dead", 31 | "not op_mini all" 32 | ], 33 | "development": [ 34 | "last 1 chrome version", 35 | "last 1 firefox version", 36 | "last 1 safari version" 37 | ] 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Feed.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | import TweetBox from "./TweetBox"; 3 | import Post from "./Post"; 4 | import "./Feed.css"; 5 | import db from "./firebase"; 6 | import FlipMove from "react-flip-move"; 7 | 8 | function Feed() { 9 | const [posts, setPosts] = useState([]); 10 | 11 | useEffect(() => { 12 | db.collection("posts").onSnapshot((snapshot) => 13 | setPosts(snapshot.docs.map((doc) => doc.data())) 14 | ); 15 | }, []); 16 | 17 | return ( 18 |
19 |
20 |

Home

21 |
22 | 23 | 24 | 25 | 26 | {posts.map((post) => ( 27 | 36 | ))} 37 | 38 |
39 | ); 40 | } 41 | 42 | export default Feed; 43 | -------------------------------------------------------------------------------- /src/Widgets.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import "./Widgets.css"; 3 | import { 4 | TwitterTimelineEmbed, 5 | TwitterShareButton, 6 | TwitterTweetEmbed, 7 | } from "react-twitter-embed"; 8 | import SearchIcon from "@material-ui/icons/Search"; 9 | 10 | function Widgets() { 11 | return ( 12 |
13 |
14 | 15 | 16 |
17 | 18 |
19 |

What's happening

20 | 21 | 22 | 23 | 28 | 29 | 33 |
34 |
35 | ); 36 | } 37 | 38 | export default Widgets; 39 | -------------------------------------------------------------------------------- /src/Sidebar.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import "./Sidebar.css"; 3 | import TwitterIcon from "@material-ui/icons/Twitter"; 4 | import SidebarOption from "./SidebarOption"; 5 | import HomeIcon from "@material-ui/icons/Home"; 6 | import SearchIcon from "@material-ui/icons/Search"; 7 | import NotificationsNoneIcon from "@material-ui/icons/NotificationsNone"; 8 | import MailOutlineIcon from "@material-ui/icons/MailOutline"; 9 | import BookmarkBorderIcon from "@material-ui/icons/BookmarkBorder"; 10 | import ListAltIcon from "@material-ui/icons/ListAlt"; 11 | import PermIdentityIcon from "@material-ui/icons/PermIdentity"; 12 | import MoreHorizIcon from "@material-ui/icons/MoreHoriz"; 13 | import { Button } from "@material-ui/core"; 14 | 15 | function Sidebar() { 16 | return ( 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | {/* Button -> Tweet */} 30 | 33 |
34 | ); 35 | } 36 | 37 | export default Sidebar; 38 | -------------------------------------------------------------------------------- /src/Post.js: -------------------------------------------------------------------------------- 1 | import React, { forwardRef } from "react"; 2 | import "./Post.css"; 3 | import { Avatar } from "@material-ui/core"; 4 | import VerifiedUserIcon from "@material-ui/icons/VerifiedUser"; 5 | import ChatBubbleOutlineIcon from "@material-ui/icons/ChatBubbleOutline"; 6 | import RepeatIcon from "@material-ui/icons/Repeat"; 7 | import FavoriteBorderIcon from "@material-ui/icons/FavoriteBorder"; 8 | import PublishIcon from "@material-ui/icons/Publish"; 9 | 10 | const Post = forwardRef( 11 | ({ displayName, username, verified, text, image, avatar }, ref) => { 12 | return ( 13 |
14 |
15 | 16 |
17 |
18 |
19 |
20 |

21 | {displayName}{" "} 22 | 23 | {verified && } @ 24 | {username} 25 | 26 |

27 |
28 |
29 |

{text}

30 |
31 |
32 | 33 |
34 | 35 | 36 | 37 | 38 |
39 |
40 |
41 | ); 42 | } 43 | ); 44 | 45 | export default Post; 46 | -------------------------------------------------------------------------------- /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/TweetBox.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from "react"; 2 | import "./TweetBox.css"; 3 | import { Avatar, Button } from "@material-ui/core"; 4 | import db from "./firebase"; 5 | 6 | function TweetBox() { 7 | const [tweetMessage, setTweetMessage] = useState(""); 8 | const [tweetImage, setTweetImage] = useState(""); 9 | 10 | const sendTweet = (e) => { 11 | e.preventDefault(); 12 | 13 | db.collection("posts").add({ 14 | displayName: "Rafeh Qazi", 15 | username: "cleverqazi", 16 | verified: true, 17 | text: tweetMessage, 18 | image: tweetImage, 19 | avatar: 20 | "https://kajabi-storefronts-production.global.ssl.fastly.net/kajabi-storefronts-production/themes/284832/settings_images/rLlCifhXRJiT0RoN2FjK_Logo_roundbackground_black.png", 21 | }); 22 | 23 | setTweetMessage(""); 24 | setTweetImage(""); 25 | }; 26 | 27 | return ( 28 |
29 |
30 |
31 | 32 | setTweetMessage(e.target.value)} 34 | value={tweetMessage} 35 | placeholder="What's happening?" 36 | type="text" 37 | /> 38 |
39 | setTweetImage(e.target.value)} 42 | className="tweetBox__imageInput" 43 | placeholder="Optional: Enter image URL" 44 | type="text" 45 | /> 46 | 47 | 54 |
55 |
56 | ); 57 | } 58 | 59 | export default TweetBox; 60 | -------------------------------------------------------------------------------- /.firebase/hosting.YnVpbGQ.cache: -------------------------------------------------------------------------------- 1 | asset-manifest.json,1596835268469,46e893f974ed2aea3513babdd2adafc77cca74ac34e61da855bcfff9b7e4077e 2 | index.html,1596835268469,b99b357341d349d59ef8b92f8b77e6ee242337ceeaa3829bebe6097a8287ac36 3 | precache-manifest.d40dd128850b73d0d3e39c39dbce01dd.js,1596835268469,9b5cad4d256e7c16d061082821a9fe7ffd434979a58e53a68f5f2dc2307fe234 4 | favicon.ico,1596835200532,a08fa4488c3ecef62d9effd03b3a989929bdcbecf5e905941f9034a15bd3dba3 5 | manifest.json,1596835200535,341d52628782f8ac9290bbfc43298afccb47b7cbfcee146ae30cf0f46bc30900 6 | service-worker.js,1596835268469,791a754ce7b78159cc316a8128c0426fcecbf9b34871984ef108558d4207510e 7 | static/css/main.8b421d16.chunk.css,1596835268470,62112d613e216666483282ce89ba7a4a91e6b18e19e0cb36cf8318b0c0884ac9 8 | static/css/main.8b421d16.chunk.css.map,1596835268496,bfb5cbd31c58dd8afb4e4cd752a883d8e8ee4639d3bd3a74653edfc4870285e3 9 | robots.txt,1596835200536,391d14b3c2f8c9143a27a28c7399585142228d4d1bdbe2c87ac946de411fa9a2 10 | static/js/2.8fd13db8.chunk.js.LICENSE.txt,1596835268496,59ffe26fda47020698022adb16bd8593b4f4a5f25c0267801af22cf36a78372c 11 | logo192.png,1596835200533,caff018b7f1e8fd481eb1c50d75b0ef236bcd5078b1d15c8bb348453fee30293 12 | logo512.png,1596835200534,191fc21360b4ccfb1cda11a1efb97f489ed22672ca83f4064316802bbfdd750e 13 | static/js/runtime-main.562f75cf.js,1596835268489,f6bd66bfbc25fd3cf1988d07c562c1b6d4af590d74a5cc764a4db8b9dcf482b7 14 | static/js/main.78688353.chunk.js,1596835268500,1ae12e1502f3fcbfd7f84bbeb3e756d32f6e2d4775bb07f7ab981cb865ed75ea 15 | static/js/runtime-main.562f75cf.js.map,1596835268500,3481fed8d26125a7439b123367ce69b87510589bbe0fd8be947eeb8ca19b1b19 16 | static/js/main.78688353.chunk.js.map,1596835268496,2264fdc3d752656e9e49595a3b1a973c7c69090c265803abcfd26aa3dada241f 17 | static/js/2.8fd13db8.chunk.js,1596835268501,3242da13de48975320b16e597ea9621541ff672794673d83eb8901866f185686 18 | static/js/2.8fd13db8.chunk.js.map,1596835268500,1d35e3e07ce2b0b23b51b7405722e119e8595629927d84b7ff88d73008af6886 19 | -------------------------------------------------------------------------------- /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 | ### `npm 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 | ### `npm 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 | ### `npm run 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 | ### `npm run 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 | 46 | ### Code Splitting 47 | 48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting 49 | 50 | ### Analyzing the Bundle Size 51 | 52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size 53 | 54 | ### Making a Progressive Web App 55 | 56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app 57 | 58 | ### Advanced Configuration 59 | 60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration 61 | 62 | ### Deployment 63 | 64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment 65 | 66 | ### `npm run build` fails to minify 67 | 68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify 69 | -------------------------------------------------------------------------------- /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 | --------------------------------------------------------------------------------