├── src
├── pages
│ ├── Me
│ │ ├── index.js
│ │ ├── styles.js
│ │ └── Me.js
│ ├── Work
│ │ ├── index.js
│ │ ├── styles.js
│ │ └── Work.js
│ ├── Projects
│ │ ├── index.js
│ │ ├── styles.js
│ │ └── Projects.js
│ ├── Education
│ │ ├── index.js
│ │ ├── styles.js
│ │ └── Education.js
│ └── index.js
├── components
│ ├── Layout
│ │ ├── index.js
│ │ ├── styles.js
│ │ └── Layout.js
│ ├── Sidebar
│ │ ├── index.js
│ │ ├── styles.js
│ │ └── Sidebar.js
│ ├── MobileNav
│ │ ├── index.js
│ │ ├── styles.js
│ │ └── MobileNav.js
│ └── UserHeader
│ │ ├── index.js
│ │ ├── styles.js
│ │ └── UserHeader.js
├── App.test.js
├── reportWebVitals.js
├── App.js
├── styles.js
├── index.js
├── index.scss
└── serviceWorker.js
├── public
├── robots.txt
├── favicon.ico
├── logo192.png
├── logo512.png
├── manifest.json
└── index.html
├── resume.png
├── .gitignore
├── README.md
└── package.json
/src/pages/Me/index.js:
--------------------------------------------------------------------------------
1 | import Me from './Me'
2 | export default Me
3 |
--------------------------------------------------------------------------------
/src/pages/Work/index.js:
--------------------------------------------------------------------------------
1 | import Work from './Work'
2 | export default Work
3 |
--------------------------------------------------------------------------------
/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 |
--------------------------------------------------------------------------------
/src/components/Layout/index.js:
--------------------------------------------------------------------------------
1 | import Layout from './Layout'
2 | export default Layout
3 |
--------------------------------------------------------------------------------
/src/pages/Projects/index.js:
--------------------------------------------------------------------------------
1 | import Projects from './Projects'
2 | export default Projects
3 |
--------------------------------------------------------------------------------
/resume.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/treyhuffine/portfolio-create-react-app/HEAD/resume.png
--------------------------------------------------------------------------------
/src/components/Sidebar/index.js:
--------------------------------------------------------------------------------
1 | import Sidebar from "./Sidebar"
2 | export default Sidebar
3 |
--------------------------------------------------------------------------------
/src/pages/Education/index.js:
--------------------------------------------------------------------------------
1 | import Education from './Education'
2 | export default Education
3 |
--------------------------------------------------------------------------------
/src/components/MobileNav/index.js:
--------------------------------------------------------------------------------
1 | import MobileNav from './MobileNav'
2 | export default MobileNav
3 |
--------------------------------------------------------------------------------
/src/components/UserHeader/index.js:
--------------------------------------------------------------------------------
1 | import UserHeader from './UserHeader'
2 | export default UserHeader
3 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/treyhuffine/portfolio-create-react-app/HEAD/public/favicon.ico
--------------------------------------------------------------------------------
/public/logo192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/treyhuffine/portfolio-create-react-app/HEAD/public/logo192.png
--------------------------------------------------------------------------------
/public/logo512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/treyhuffine/portfolio-create-react-app/HEAD/public/logo512.png
--------------------------------------------------------------------------------
/src/pages/Me/styles.js:
--------------------------------------------------------------------------------
1 | import styled from 'styled-components'
2 |
3 | export const ProfileLink = styled.li`
4 | margin-right: 0.5rem;
5 | margin-bottom: 0.5rem;
6 | display: inline-block;
7 | font-size: 18px;
8 | `
9 |
--------------------------------------------------------------------------------
/src/components/Sidebar/styles.js:
--------------------------------------------------------------------------------
1 | import styled from 'styled-components'
2 | import { SideNav } from 'carbon-components-react/lib/components/UIShell'
3 |
4 | export const StyledSideNav = styled(SideNav)`
5 | @media (max-width: 640px) {
6 | display: none;
7 | }
8 | `
9 |
--------------------------------------------------------------------------------
/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/components/Layout/styles.js:
--------------------------------------------------------------------------------
1 | import styled from 'styled-components'
2 | import { Content } from 'carbon-components-react/lib/components/UIShell'
3 |
4 | export const StyledContent = styled(Content)`
5 | min-height: 100vh;
6 |
7 | @media (max-width: 640px) {
8 | margin-left: 0 !important;
9 | }
10 | `
11 |
--------------------------------------------------------------------------------
/.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/pages/Projects/styles.js:
--------------------------------------------------------------------------------
1 | import styled from 'styled-components'
2 | import { white } from '@carbon/colors'
3 |
4 | export const ProjectItem = styled.li`
5 | margin-top: 1rem;
6 | padding-bottom: 1rem;
7 | border-bottom: 1px solid ${white};
8 | `
9 |
10 | export const ProjectTitle = styled.h4`
11 | font-weight: bold;
12 | `
13 |
14 | export const SkillContainer = styled.div`
15 | margin-top: 1.2rem;
16 | `
17 |
--------------------------------------------------------------------------------
/src/pages/Work/styles.js:
--------------------------------------------------------------------------------
1 | import styled from 'styled-components'
2 | import { white } from '@carbon/colors'
3 |
4 | export const WorkItem = styled.li`
5 | margin-top: 1rem;
6 | padding-bottom: 1rem;
7 | border-bottom: 1px solid ${white};
8 | `
9 |
10 | export const WorkTitle = styled.h4`
11 | font-weight: bold;
12 | `
13 |
14 | export const JobTitle = styled.p`
15 | font-weight: bold;
16 | display: inline-block;
17 | `
18 |
--------------------------------------------------------------------------------
/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/pages/Education/styles.js:
--------------------------------------------------------------------------------
1 | import styled from 'styled-components'
2 | import { white } from '@carbon/colors'
3 |
4 | export const EducationItem = styled.li`
5 | margin-top: 1rem;
6 | padding-bottom: 1rem;
7 | border-bottom: 1px solid ${white};
8 | `
9 |
10 | export const Institution = styled.h4`
11 | font-weight: bold;
12 | `
13 |
14 | export const Degree = styled.p`
15 | font-weight: bold;
16 | display: inline-block;
17 | `
18 |
--------------------------------------------------------------------------------
/src/components/Layout/Layout.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import Sidebar from '../Sidebar';
3 | import UserHeader from '../UserHeader';
4 | import MobileNav from '../MobileNav';
5 |
6 | import { StyledContent } from './styles';
7 |
8 | const Layout = ({ user, children }) => {
9 | return (
10 | <>
11 |
12 |
13 |
14 |
15 | {children}
16 |
17 | >
18 | );
19 | };
20 |
21 | export default Layout;
22 |
--------------------------------------------------------------------------------
/src/App.js:
--------------------------------------------------------------------------------
1 | import React, { useState, useEffect } from 'react';
2 | import Pages from './pages';
3 |
4 | function App() {
5 | const [user, setUser] = useState(null);
6 | useEffect(() => {
7 | // NOTE: Use your username below
8 | fetch('https://gitconnected.com/v1/portfolio/richard-hendricks-demo')
9 | .then((res) => res.json())
10 | .then((user) => {
11 | setUser(user);
12 | });
13 | }, []);
14 |
15 | if (!user) {
16 | return
;
17 | }
18 |
19 | return ;
20 | }
21 |
22 | export default App;
23 |
--------------------------------------------------------------------------------
/src/styles.js:
--------------------------------------------------------------------------------
1 | import styled from 'styled-components';
2 | import { blue } from '@carbon/colors';
3 |
4 | export const SectionTitle = styled.h3`
5 | margin-top: 1.5rem;
6 | margin-bottom: 0.5rem;
7 | `;
8 |
9 | export const Paragraph = styled.p`
10 | white-space: pre-wrap;
11 | `;
12 |
13 | export const Pill = styled.span`
14 | display: inline-block;
15 | margin-right: 0.75rem;
16 | margin-bottom: 0.75rem;
17 | padding: 0.5rem 1rem;
18 | background-color: ${blue[20]};
19 | color: ${blue[70]};
20 | border-radius: 2px;
21 | font-weight: bold;
22 | `;
23 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom';
3 | import './index.scss';
4 | import App from './App';
5 | import reportWebVitals from './reportWebVitals';
6 |
7 | ReactDOM.render(
8 |
9 |
10 | ,
11 | document.getElementById('root'),
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/pages/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
3 | import Me from './Me';
4 | import Projects from './Projects';
5 | import Work from './Work';
6 | import Education from './Education';
7 |
8 | const Pages = ({ user }) => {
9 | return (
10 |
11 |
12 | } />
13 | } />
14 | } />
15 | } />
16 |
17 |
18 | );
19 | };
20 |
21 | export default Pages;
22 |
--------------------------------------------------------------------------------
/src/components/MobileNav/styles.js:
--------------------------------------------------------------------------------
1 | import styled from 'styled-components';
2 | import { Link } from 'react-router-dom';
3 | import Button from 'carbon-components-react/lib/components/Button';
4 |
5 | export const Container = styled.div`
6 | display: none;
7 |
8 | @media (max-width: 640px) {
9 | display: block;
10 | }
11 | `;
12 |
13 | export const Spacer = styled.div`
14 | height: 48px;
15 | `;
16 |
17 | export const NavWrapper = styled.div`
18 | display: flex;
19 | justify-content: center;
20 | position: fixed;
21 | width: 100vw;
22 | top: 0;
23 | left: 0;
24 | z-index: 1;
25 | `;
26 |
27 | export const NavLink = styled(Link)`
28 | width: 25%;
29 | `;
30 |
31 | export const NavButton = styled(Button)`
32 | width: 100%;
33 | justify-content: center;
34 | `;
35 |
--------------------------------------------------------------------------------
/src/index.scss:
--------------------------------------------------------------------------------
1 | @import '@carbon/themes/scss/themes';
2 | $carbon--theme: $carbon--theme--g100;
3 | @include carbon--theme();
4 |
5 | @import 'carbon-components/scss/globals/scss/styles.scss';
6 |
7 | a {
8 | color: $text-04;
9 | }
10 |
11 | .bx--side-nav__navigation {
12 | background-color: $ui-background;
13 | }
14 |
15 | a.bx--side-nav__link--current,
16 | a.bx--side-nav__link--current > span.bx--side-nav__link-text {
17 | background-color: $ui-01;
18 | color: $text-04;
19 | }
20 |
21 | .bx--side-nav__item:not(.bx--side-nav__item--active)
22 | > .bx--side-nav__link:hover {
23 | background-color: $ui-01;
24 | color: $text-04;
25 | }
26 |
27 | a.bx--side-nav__link > .bx--side-nav__link-text,
28 | .bx--side-nav__item:not(.bx--side-nav__item--active)
29 | > .bx--side-nav__link:hover
30 | > span {
31 | color: $text-04;
32 | }
33 |
--------------------------------------------------------------------------------
/src/pages/Projects/Projects.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import Layout from '../../components/Layout';
3 | import { SectionTitle, Pill } from '../../styles';
4 | import { ProjectItem, ProjectTitle, SkillContainer } from './styles';
5 |
6 | const Projects = ({ user }) => {
7 | return (
8 |
9 |
10 |
Projects
11 |
12 | {user.projects.map((project, i) => (
13 |
14 | {project.name}
15 | {project.summary}
16 |
17 | {[...project.languages, ...project.libraries].map((item, j) => (
18 | {item}
19 | ))}
20 |
21 |
22 | ))}
23 |
24 |
25 |
26 | );
27 | };
28 |
29 | export default Projects;
30 |
--------------------------------------------------------------------------------
/src/pages/Work/Work.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import Layout from '../../components/Layout';
3 | import { SectionTitle, Paragraph } from '../../styles';
4 | import { WorkItem, WorkTitle, JobTitle } from './styles';
5 |
6 | const Work = ({ user }) => {
7 | return (
8 |
9 |
27 |
28 | );
29 | };
30 |
31 | export default Work;
32 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## NEW! - Automatically Generated Resumes
2 | ```
3 | https://gitconnected.com//resume
4 | ```
5 | Ex. **[My Resume →](https://gitconnected.com/treyhuffine/resume)** (https://gitconnected.com/treyhuffine/resume)
6 | 
7 |
8 | ## Built using the gitconnected.com user portfolio API
9 | Data is supplied by the gitconnected.com API and fetches your newest data on each page load. Make updates to your portfolio website without ever needing to change code or re-deploy. Use your API endpiont now:
10 |
11 | ```
12 | https://gitconnected.com/v1/portfolio/
13 | ```
14 |
15 | [Tutorial Article >](https://levelup.gitconnected.com/build-an-awesome-developer-portfolio-website-using-react-667abd7bab4d?source=friends_link&sk=128b34f902f9363ef9f6f18125e58b06)
16 |
17 | ## Instructions
18 | ```
19 | git clone git@github.com:treyhuffine/portfolio-create-react-app.git
20 | cd portfolio-create-react-app
21 | yarn install
22 | yarn start
23 | ```
24 |
25 | ## Packages
26 | - React (using create-react-app)
27 | - Styled Components
28 | - Carbon Design
29 |
30 |
--------------------------------------------------------------------------------
/src/components/UserHeader/styles.js:
--------------------------------------------------------------------------------
1 | import styled from 'styled-components';
2 |
3 | export const HeaderContainer = styled.div`
4 | display: flex;
5 | justify-content: space-between;
6 |
7 | @media (max-width: 640px) {
8 | display: ${({ isHome }) => (!isHome ? 'none' : 'flex')};
9 | flex-direction: column;
10 | }
11 | `;
12 |
13 | export const Header = styled.div`
14 | display: flex;
15 |
16 | @media (max-width: 640px) {
17 | flex-direction: column;
18 | }
19 | `;
20 |
21 | export const Image = styled.img`
22 | width: 200px;
23 | margin-right: 1rem;
24 | border-radius: 2px;
25 | `;
26 |
27 | export const ViewResumeLink = styled.a`
28 | display: flex;
29 | text-decoration: none;
30 | padding: 0.75rem 1.5rem;
31 | font-weight: bold;
32 | align-items: center;
33 | margin-top: 1rem;
34 | border: 2px solid #2ecc40;
35 | background-color: rgba(46, 204, 64, 0.3);
36 | transition: background-color 250ms ease;
37 |
38 | &:hover {
39 | background-color: #2ecc40;
40 | }
41 |
42 | svg {
43 | fill: white;
44 | margin-left: 8px;
45 | }
46 | `;
47 |
--------------------------------------------------------------------------------
/src/pages/Education/Education.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import Layout from '../../components/Layout';
3 | import { SectionTitle, Paragraph } from '../../styles';
4 | import { EducationItem, Institution, Degree } from './styles';
5 |
6 | const Education = ({ user }) => {
7 | return (
8 |
9 |
29 |
30 | );
31 | };
32 |
33 | export default Education;
34 |
--------------------------------------------------------------------------------
/src/components/Sidebar/Sidebar.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { Link, useLocation } from 'react-router-dom';
3 | import { SideNavItems, SideNavLink } from 'carbon-components-react/lib/components/UIShell';
4 |
5 | import { StyledSideNav } from './styles';
6 |
7 | const items = [
8 | { name: 'Me', path: '/' },
9 | { name: 'Projects', path: '/projects' },
10 | { name: 'Work', path: '/work' },
11 | { name: 'Education', path: '/education' },
12 | ];
13 |
14 | const Sidebar = () => {
15 | const location = useLocation();
16 |
17 | return (
18 |
19 |
20 | {items.map(i => (
21 |
29 | {i.name}
30 |
31 | ))}
32 |
33 |
34 | );
35 | };
36 |
37 | export default Sidebar;
38 |
--------------------------------------------------------------------------------
/src/pages/Me/Me.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import Layout from '../../components/Layout';
3 | import { SectionTitle, Paragraph, Pill } from '../../styles';
4 | import { ProfileLink } from './styles';
5 |
6 | const Me = ({ user }) => {
7 | return (
8 |
9 |
10 |
About Me
11 |
{user.basics.summary}
12 |
13 |
14 |
Skills
15 |
16 | {user.skills.map(skill => (
17 |
{skill.name}
18 | ))}
19 |
20 |
21 |
34 |
35 | );
36 | };
37 |
38 | export default Me;
39 |
--------------------------------------------------------------------------------
/src/components/MobileNav/MobileNav.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import { User32, Code32, Portfolio32, Education32 } from '@carbon/icons-react'
3 |
4 | import { Container, Spacer, NavWrapper, NavButton, NavLink } from './styles'
5 |
6 | const MobileNav = () => {
7 | return (
8 |
9 |
10 |
11 |
12 |
18 |
19 |
20 |
26 |
27 |
28 |
34 |
35 |
36 |
42 |
43 |
44 |
45 | )
46 | }
47 |
48 | export default MobileNav
49 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "portfolio-create-react-app",
3 | "version": "0.1.0",
4 | "private": "false",
5 | "license": "MIT",
6 | "dependencies": {
7 | "@carbon/icons-react": "^10.44.0",
8 | "@carbon/themes": "^10.48.0",
9 | "@testing-library/jest-dom": "^5.16.1",
10 | "@testing-library/react": "^12.1.2",
11 | "@testing-library/user-event": "^13.5.0",
12 | "carbon-components": "^10.50.0",
13 | "carbon-components-react": "^7.50.0",
14 | "react": "^17.0.2",
15 | "react-dom": "^17.0.2",
16 | "react-router-dom": "^6.2.1",
17 | "react-scripts": "5.0.0",
18 | "sass": "^1.45.1",
19 | "styled-components": "^5.3.3",
20 | "web-vitals": "^2.1.2"
21 | },
22 | "devDependencies": {
23 | "gh-pages": "^2.1.1"
24 | },
25 | "scripts": {
26 | "start": "react-scripts start",
27 | "build": "react-scripts build",
28 | "test": "react-scripts test",
29 | "eject": "react-scripts eject",
30 | "predeploy": "npm run build",
31 | "deploy": "gh-pages -d build"
32 | },
33 | "eslintConfig": {
34 | "extends": [
35 | "react-app",
36 | "react-app/jest"
37 | ]
38 | },
39 | "browserslist": {
40 | "production": [
41 | ">0.2%",
42 | "not dead",
43 | "not op_mini all"
44 | ],
45 | "development": [
46 | "last 1 chrome version",
47 | "last 1 firefox version",
48 | "last 1 safari version"
49 | ]
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/components/UserHeader/UserHeader.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { useLocation } from 'react-router-dom';
3 | import { ArrowRight16 } from '@carbon/icons-react';
4 |
5 | import { HeaderContainer, Header, Image, ViewResumeLink } from './styles';
6 |
7 | const UserHeader = ({ user }) => {
8 | const location = useLocation();
9 |
10 | return (
11 |
12 |
37 |
38 |
43 | View Résumé
44 |
45 |
46 |
47 |
48 | );
49 | };
50 |
51 | export default UserHeader;
52 |
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 | React App
28 |
29 |
30 | You need to enable JavaScript to run this app.
31 |
32 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------