Skip to content

Commit 7842a68

Browse files
author
Roman Zanettin
committed
upgraded react-scripts to 1.0.6, added latest PWA support of CRA
1 parent 75b05cc commit 7842a68

File tree

9 files changed

+1956
-1005
lines changed

9 files changed

+1956
-1005
lines changed

.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.0.0

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"eslint.enable": false
3+
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"private": true,
55
"devDependencies": {
66
"babel-cli": "^6.24.1",
7-
"react-scripts": "0.8.5"
7+
"babel-preset-env": "^1.5.1",
8+
"react-scripts": "1.0.6"
89
},
910
"dependencies": {
1011
"body-parser": "^1.17.1",

public/index.html

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
<head>
44
<meta charset="utf-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1">
6-
<link rel="shortcut icon" href="%PUBLIC_URL%/favicons/favicon.ico">
6+
<meta name="theme-color" content="#000000">
77
<!--
8-
Notice the use of %PUBLIC_URL% in the tag above.
8+
manifest.json provides metadata used when your web app is added to the
9+
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
10+
-->
11+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
12+
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
13+
<!--
14+
Notice the use of %PUBLIC_URL% in the tags above.
915
It will be replaced with the URL of the `public` folder during the build.
1016
Only files inside the `public` folder can be referenced from the HTML.
1117

public/manifest.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"short_name": "SSR React App",
3+
"name": "SSR Create React App Sample",
4+
"icons": [
5+
{
6+
"src": "favicons/favicon.ico",
7+
"sizes": "192x192",
8+
"type": "image/png"
9+
}
10+
],
11+
"start_url": "./index.html",
12+
"display": "standalone",
13+
"theme_color": "#000000",
14+
"background_color": "#ffffff"
15+
}

server/app.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ const morgan = require('morgan')
66
const path = require('path')
77
const fs = require('fs')
88

9-
require('babel-register')({ ignore: /\/(build|node_modules)\//, presets: ['react-app'] })
9+
require('babel-register')({
10+
ignore: /\/(build|node_modules)\//,
11+
presets: ['env', 'react-app']
12+
})
1013

1114
// routes
1215
const index = require('./routes/index')
@@ -19,7 +22,7 @@ const app = express()
1922
// Support Gzip
2023
app.use(compression())
2124

22-
// Suport post requests with body data (doesn't support multipart, use multer)
25+
// Support post requests with body data (doesn't support multipart, use multer)
2326
app.use(bodyParser.json())
2427
app.use(bodyParser.urlencoded({ extended: false }))
2528

src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { BrowserRouter } from 'react-router-dom'
66
import configureStore from './store'
77
import './index.css'
88
import App from './containers/App'
9+
import registerServiceWorker from './registerServiceWorker'
910

1011
// Let the reducers handle initial state
1112
const initialState = {}
@@ -19,5 +20,4 @@ ReactDOM.render(
1920
</Provider>
2021
, document.getElementById('root')
2122
)
22-
23-
23+
registerServiceWorker()

src/registerServiceWorker.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// In production, we register a service worker to serve assets from local cache.
2+
3+
// This lets the app load faster on subsequent visits in production, and gives
4+
// it offline capabilities. However, it also means that developers (and users)
5+
// will only see deployed updates on the "N+1" visit to a page, since previously
6+
// cached resources are updated in the background.
7+
8+
// To learn more about the benefits of this model, read https://goo.gl/KwvDNy.
9+
// This link also includes instructions on opting out of this behavior.
10+
11+
export default function register() {
12+
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
13+
window.addEventListener('load', () => {
14+
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
15+
navigator.serviceWorker
16+
.register(swUrl)
17+
.then(registration => {
18+
registration.onupdatefound = () => {
19+
const installingWorker = registration.installing;
20+
installingWorker.onstatechange = () => {
21+
if (installingWorker.state === 'installed') {
22+
if (navigator.serviceWorker.controller) {
23+
// At this point, the old content will have been purged and
24+
// the fresh content will have been added to the cache.
25+
// It's the perfect time to display a "New content is
26+
// available; please refresh." message in your web app.
27+
console.log('New content is available; please refresh.');
28+
} else {
29+
// At this point, everything has been precached.
30+
// It's the perfect time to display a
31+
// "Content is cached for offline use." message.
32+
console.log('Content is cached for offline use.');
33+
}
34+
}
35+
};
36+
};
37+
})
38+
.catch(error => {
39+
console.error('Error during service worker registration:', error);
40+
});
41+
});
42+
}
43+
}
44+
45+
export function unregister() {
46+
if ('serviceWorker' in navigator) {
47+
navigator.serviceWorker.ready.then(registration => {
48+
registration.unregister();
49+
});
50+
}
51+
}

0 commit comments

Comments
 (0)