├── .gitignore
├── README.md
├── package-lock.json
├── package.json
├── public
├── assets
│ ├── img.png
│ ├── img1.png
│ ├── img2.png
│ ├── img3.png
│ └── img4.png
├── favicon.ico
├── img
│ ├── 1.jpeg
│ ├── 10.jpeg
│ ├── 11.jpeg
│ ├── 12.jpeg
│ ├── 13.jpeg
│ ├── 14.jpeg
│ ├── 15.jpeg
│ ├── 2.jpeg
│ ├── 3.jpeg
│ ├── 4.jpeg
│ ├── 5.jpeg
│ ├── 6.jpeg
│ ├── 7.jpeg
│ ├── 8.jpeg
│ ├── 9.jpeg
│ └── profileImage.jpeg
├── index.html
├── logo192.png
├── logo512.png
├── manifest.json
└── robots.txt
└── src
├── App.css
├── App.js
├── App.test.js
├── Gallery
└── Gallery.js
├── Modal
├── Modal.js
└── PostGrid.js
├── Posts
└── index.js
├── Profile
├── ProfileImage.js
└── UserGrid.js
├── index.css
├── index.js
├── logo.svg
└── serviceWorker.js
/.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 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Instagram UI Clone
2 |
3 | ## Motivation
4 |
5 | - Practice the following:
6 | - CSS Grids
7 | - JavaScript & React.js
8 | - Style Components
9 |
10 | ## Features Implemented
11 |
12 | - Gallery Page of various layouts
13 | - Modal
14 |
15 | 
16 | 
17 |
18 |
19 | ### Screens
20 |
21 | #### Square View
22 |
23 | 
24 |
25 | #### Cascade View
26 | 
27 |
28 |
29 | #### Responsive View
30 | 
31 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-instagram",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "react": "^16.11.0",
7 | "react-dom": "^16.11.0",
8 | "react-router-dom": "^5.1.2",
9 | "react-scripts": "3.2.0",
10 | "styled-components": "^4.4.1"
11 | },
12 | "scripts": {
13 | "start": "react-scripts start",
14 | "build": "react-scripts build",
15 | "test": "react-scripts test",
16 | "eject": "react-scripts eject"
17 | },
18 | "eslintConfig": {
19 | "extends": "react-app"
20 | },
21 | "browserslist": {
22 | "production": [
23 | ">0.2%",
24 | "not dead",
25 | "not op_mini all"
26 | ],
27 | "development": [
28 | "last 1 chrome version",
29 | "last 1 firefox version",
30 | "last 1 safari version"
31 | ]
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/public/assets/img.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/assets/img.png
--------------------------------------------------------------------------------
/public/assets/img1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/assets/img1.png
--------------------------------------------------------------------------------
/public/assets/img2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/assets/img2.png
--------------------------------------------------------------------------------
/public/assets/img3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/assets/img3.png
--------------------------------------------------------------------------------
/public/assets/img4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/assets/img4.png
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/favicon.ico
--------------------------------------------------------------------------------
/public/img/1.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/img/1.jpeg
--------------------------------------------------------------------------------
/public/img/10.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/img/10.jpeg
--------------------------------------------------------------------------------
/public/img/11.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/img/11.jpeg
--------------------------------------------------------------------------------
/public/img/12.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/img/12.jpeg
--------------------------------------------------------------------------------
/public/img/13.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/img/13.jpeg
--------------------------------------------------------------------------------
/public/img/14.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/img/14.jpeg
--------------------------------------------------------------------------------
/public/img/15.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/img/15.jpeg
--------------------------------------------------------------------------------
/public/img/2.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/img/2.jpeg
--------------------------------------------------------------------------------
/public/img/3.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/img/3.jpeg
--------------------------------------------------------------------------------
/public/img/4.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/img/4.jpeg
--------------------------------------------------------------------------------
/public/img/5.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/img/5.jpeg
--------------------------------------------------------------------------------
/public/img/6.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/img/6.jpeg
--------------------------------------------------------------------------------
/public/img/7.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/img/7.jpeg
--------------------------------------------------------------------------------
/public/img/8.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/img/8.jpeg
--------------------------------------------------------------------------------
/public/img/9.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/img/9.jpeg
--------------------------------------------------------------------------------
/public/img/profileImage.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/img/profileImage.jpeg
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
14 |
18 |
19 |
28 | React App
29 |
30 |
31 |
32 |
33 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/public/logo192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/logo192.png
--------------------------------------------------------------------------------
/public/logo512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheJSGirl/instagram-profile-ui-clone/76a60bb3b69581868567fd8fb9b214782d89f18e/public/logo512.png
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 |
--------------------------------------------------------------------------------
/src/App.css:
--------------------------------------------------------------------------------
1 | .App {
2 | text-align: center;
3 | }
4 |
5 | .App-logo {
6 | height: 40vmin;
7 | }
8 |
9 | .App-header {
10 | background-color: #282c34;
11 | min-height: 100vh;
12 | display: flex;
13 | flex-direction: column;
14 | align-items: center;
15 | justify-content: center;
16 | font-size: calc(10px + 2vmin);
17 | color: white;
18 | }
19 |
20 | .App-link {
21 | color: #09d3ac;
22 | }
23 |
--------------------------------------------------------------------------------
/src/App.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 | import {
3 | BrowserRouter as Router,
4 | Switch,
5 | Route,
6 | Link,
7 | useLocation,
8 | useParams
9 | } from "react-router-dom";
10 | import styled, {css} from 'styled-components';
11 | import {Modal} from './Modal/Modal';
12 | import Posts from './Posts';
13 | import {Gallery} from './Gallery/Gallery';
14 | // This example shows how to render two different screens
15 | // (or the same screen in a different context) at the same URL,
16 | // depending on how you got there.
17 | //
18 | // Click the "featured images" and see them full screen. Then
19 | // "visit the gallery" and click on the colors. Note the URL and
20 | // the component are the same as before but now we see them
21 | // inside a modal on top of the gallery screen.
22 |
23 | export default function ModalGalleryExample() {
24 | return (
25 |
26 |
27 |
28 | );
29 | }
30 |
31 | function ModalSwitch() {
32 | let location = useLocation();
33 |
34 | // This piece of state is set when one of the
35 | // gallery links is clicked. The `background` state
36 | // is the location that we were at when one of
37 | // the gallery links was clicked. If it's there,
38 | // use it as the location for the so
39 | // we show the gallery in the background, behind
40 | // the modal.
41 | let background = location.state && location.state.background;
42 |
43 | return (
44 |
45 |
46 | } />
47 | } />
48 | } />
49 |
50 |
51 | {/* Show the modal when a background page is set */}
52 | {background && } />}
53 |
54 | );
55 | }
56 |
57 | export const Image = styled.div`
58 | width: 305px;
59 | height: 305px;
60 | background: no-repeat center/150% url(/img/${({index}) => index}.jpeg);
61 | ${({isModal}) => !isModal && css`
62 | :hover {
63 | opacity: .7
64 | }
65 | `}
66 | @media (max-width: 990px){
67 | width: 100%;
68 | }
69 |
70 | `;
71 |
72 |
73 |
74 | function Home() {
75 | return (
76 |
77 |
Visit the Gallery
78 |
Featured Images
79 |
80 | -
81 | Tomato
82 |
83 | -
84 | Crimson
85 |
86 |
87 |
88 | );
89 | }
90 |
91 |
92 |
--------------------------------------------------------------------------------
/src/App.test.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom';
3 | import App from './App';
4 |
5 | it('renders without crashing', () => {
6 | const div = document.createElement('div');
7 | ReactDOM.render(, div);
8 | ReactDOM.unmountComponentAtNode(div);
9 | });
10 |
--------------------------------------------------------------------------------
/src/Gallery/Gallery.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import styled, {css} from 'styled-components';
3 | import UserGrid from '../Profile/UserGrid';
4 | import Posts from '../Posts';
5 | import {
6 | Link,
7 | useLocation
8 | } from "react-router-dom";
9 |
10 |
11 | export const ImageLink = styled(Link)`
12 | background: no-repeat center/150% url(/img/${({index}) => index}.jpeg);
13 | background-size: cover;
14 | :hover {
15 | opacity: .7
16 | }
17 | ${({cascade}) => cascade && css`
18 | background-size: cover;
19 | &:nth-of-type(2n) {
20 | grid-row-start: span 2;
21 | }
22 | `}
23 |
24 | `;
25 |
26 | const PhotoGrid = styled.div`
27 | display: grid;
28 | grid-template-columns: repeat(3, 305px);
29 | justify-content: center;
30 | gap:20px;
31 | grid-auto-rows: 305px;
32 | ${({cascade}) => cascade && css`
33 | grid-auto-rows: 200px;
34 | `}
35 | @media (max-width: 990px){
36 | gap: 5px;
37 | padding-left: 25px;
38 | grid-template-columns: repeat(3, 1fr);
39 | grid-auto-rows: calc(33vw - 10px);
40 | }
41 | `;
42 |
43 | const LinkGrid = styled.div`
44 | display: grid;
45 | grid-template-columns: auto auto;
46 | justify-content: center;
47 | gap: 20px;
48 | margin-bottom: 20px;
49 | `;
50 | const TabLink = styled(Link)`
51 | text-decoration: none;
52 | color: #ccc;
53 | text-transform: uppercase;
54 | letter-spacing: 3px;
55 | ${({selected}) => selected && 'color: black;'}
56 | `;
57 |
58 | export function Gallery() {
59 | let location = useLocation();
60 | const cascade = location.search === '?type=cascade';
61 | return (
62 |
63 |
64 |
65 | Square
66 | Cascade
67 |
68 |
69 | {Posts.map(i => (
70 |
81 | {i.title}
82 |
83 | ))}
84 |
85 |
86 | );
87 | }
88 |
--------------------------------------------------------------------------------
/src/Modal/Modal.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import Posts from '../Posts';
3 | import {Image} from '../App';
4 | import {
5 | useHistory,
6 | useParams
7 | } from "react-router-dom";
8 | import styled, {createGlobalStyle} from 'styled-components';
9 | import { PostGrid, InfoGrid} from './PostGrid';
10 | import {MiniUserGrid} from '../Profile/UserGrid';
11 | import {ProfileImage} from '../Profile/ProfileImage';
12 |
13 | const ModalStyled = styled.div`
14 | position: absolute;
15 | background: #fff;
16 | top: ${({top}) => top}px;
17 | left: 25%;
18 | right: 25%;
19 | padding: 15px;
20 | border: 2px solid #444;
21 | @media (max-width: 990px){
22 | left: 0;
23 | right: 0;
24 | top: ${({top}) => top}px;
25 | width: auto;
26 | }
27 |
28 | `;
29 |
30 | const OverflowHidden = createGlobalStyle`
31 | body {
32 | overflow: hidden;
33 | }
34 | `;
35 |
36 | export function Modal() {
37 | let history = useHistory();
38 | let { id } = useParams();
39 | let image = Posts[parseInt(id, 10)];
40 |
41 | if (!image) return null;
42 |
43 | let back = e => {
44 | e.stopPropagation();
45 | history.goBack();
46 | };
47 |
48 | return (
49 |
61 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 | GridGallary
72 |
73 |
74 |
{image.title}
75 |
76 | 45 Likes
77 |
78 |
79 |
80 |
81 | );
82 | }
--------------------------------------------------------------------------------
/src/Modal/PostGrid.js:
--------------------------------------------------------------------------------
1 | import styled from 'styled-components';
2 |
3 | export const PostGrid = styled.div`
4 | display: grid;
5 | grid-template-columns: 1fr 1fr;
6 | @media (max-width: 990px){
7 | grid-template-columns: 1fr;
8 | }
9 | `;
10 |
11 | export const InfoGrid = styled.div`
12 | padding: 20px;
13 | display: grid;
14 | grid-template-rows: 60px auto 40px;
15 | `;
--------------------------------------------------------------------------------
/src/Posts/index.js:
--------------------------------------------------------------------------------
1 | export default [
2 | { id: 1, title: "Lime Green", color: "LimeGreen" },
3 | { id: 2, title: "Tomato", color: "Tomato" },
4 | { id: 3, title: "Seven Ate Nine", color: "#789" },
5 | { id: 5, title: "Crimson", color: "Crimson" },
6 | { id: 6, title: "Crimson", color: "Crimson" },
7 | { id: 7, title: "Crimson", color: "Crimson" },
8 | { id: 8, title: "Crimson", color: "Crimson" },
9 | { id: 9, title: "Seven Ate Nine", color: "#789" },
10 | { id: 10, title: "Crimson", color: "#789" },
11 | { id: 11, title: "Tomato", color: "#789" },
12 | { id: 12, title: "Seven Ate Nine", color: "#789" },
13 | { id: 13, title: "Seven Ate Nine", color: "#789" },
14 | { id: 14, title: "Seven Ate Nine", color: "#789" },
15 | { id: 15, title: "Seven Ate Nine", color: "#789" },
16 | ];
--------------------------------------------------------------------------------
/src/Profile/ProfileImage.js:
--------------------------------------------------------------------------------
1 | import styled, {css} from 'styled-components';
2 |
3 | export const ProfileImage = styled.div`
4 | width: 200px;
5 | height: 200px;
6 | margin: 40px;
7 | @media (max-width: 990px){
8 | margin: 20px;
9 | width: 120px;
10 | height: 120px;
11 | }
12 | ${({mini}) => mini && css`
13 | width: 50px;
14 | height: 50px;
15 | margin: 5px;
16 | @media (max-width: 990px){
17 | margin: 5px;
18 | width: 30px;
19 | height: 50px;
20 | }
21 |
22 | `};
23 | background: no-repeat center/170% url(/img/profileImage.jpeg);
24 | border-radius: 100%;
25 |
26 | `;
--------------------------------------------------------------------------------
/src/Profile/UserGrid.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import styled from 'styled-components';
3 | import {ProfileImage} from './ProfileImage';
4 |
5 | const UserGridStyled = styled.div`
6 | display: grid;
7 | justify-content: center;
8 | margin-top: 80px;
9 | margin-bottom: 50px;
10 | gap: 15px;
11 | grid-template-areas: "photo name"
12 | "photo label"
13 | "photo description";
14 | @media (max-width: 990px){
15 | grid-template-areas: "photo name"
16 | "label ."
17 | "description .";
18 | }
19 | `;
20 |
21 | export const MiniUserGrid = styled.div`
22 | display: grid;
23 | justify-content: left;
24 | grid-template-columns: auto auto;
25 | gap: 10px;
26 | `;
27 |
28 | const Photo = styled.div`
29 | grid-area: photo;
30 | `;
31 |
32 | const Name = styled.div`
33 | grid-area: name;
34 | font-size: 35px;
35 | align-self: center;
36 | `;
37 |
38 | const Label = styled.div`
39 | grid-area: label;
40 | @media (max-width: 990px){
41 | padding-left: 25px;
42 | }
43 | `;
44 |
45 | const Description = styled.div`
46 | grid-area: description;
47 | max-width: 400px;
48 | @media (max-width: 990px){
49 | grid-column: 1/3;
50 | padding-left: 25px;
51 | }
52 | `;
53 |
54 | export default function() {
55 | return
56 |
57 | GridGallary
58 |
59 | Pitchfork tilde lomo chillwave keytar, tofu chartreuse letterpress mumblecore.
60 | mixtape palo santo kitsch sustainable food truck asymmetrical microdosing pok pok.
61 | ;
62 | }
--------------------------------------------------------------------------------
/src/index.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin: 0;
3 | -webkit-font-smoothing: antialiased;
4 | -moz-osx-font-smoothing: grayscale;
5 | font-family: 'Special Elite', cursive;
6 |
7 | }
8 |
9 | code {
10 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
11 | monospace;
12 | }
13 |
--------------------------------------------------------------------------------
/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(, 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/logo.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/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.1/8 is 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 | .then(response => {
105 | // Ensure service worker exists, and that we really are getting a JS file.
106 | const contentType = response.headers.get('content-type');
107 | if (
108 | response.status === 404 ||
109 | (contentType != null && contentType.indexOf('javascript') === -1)
110 | ) {
111 | // No service worker found. Probably a different app. Reload the page.
112 | navigator.serviceWorker.ready.then(registration => {
113 | registration.unregister().then(() => {
114 | window.location.reload();
115 | });
116 | });
117 | } else {
118 | // Service worker found. Proceed as normal.
119 | registerValidSW(swUrl, config);
120 | }
121 | })
122 | .catch(() => {
123 | console.log(
124 | 'No internet connection found. App is running in offline mode.'
125 | );
126 | });
127 | }
128 |
129 | export function unregister() {
130 | if ('serviceWorker' in navigator) {
131 | navigator.serviceWorker.ready.then(registration => {
132 | registration.unregister();
133 | });
134 | }
135 | }
136 |
--------------------------------------------------------------------------------