Skip to content

Commit deb7365

Browse files
author
sw-yx
committed
copy over CRA2 stuff
1 parent d5794f4 commit deb7365

File tree

10 files changed

+380
-2264
lines changed

10 files changed

+380
-2264
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
This project is based on [Create React App](https://github.com/facebookincubator/create-react-app). (For more information about Create react App, check their full [documentation](https://github.com/facebookincubator/create-react-app#create-react-app).)
1+
This project is based on [Create React App v2](https://github.com/facebookincubator/create-react-app) and [netlify-lambda v1](https://github.com/netlify/netlify-lambda). (For more information about Create react App, check their full [documentation](https://github.com/facebookincubator/create-react-app#create-react-app).)
22

33
The main addition is a new folder: `src/lambda`. Each JavaScript file in there will automatically be prepared for Lambda function deployment.
44

@@ -24,7 +24,7 @@ yarn start:lambda
2424

2525
This will open a local server running at `http://localhost:9000` serving your Lambda functions, updating as you make changes in the `src/lambda` folder.
2626

27-
You can then access your functions directly at `http://localhost:9000/{function_name}`, but to access them with the app, you'll need to start the app dev server.
27+
You can then access your functions directly at `http://localhost:9000/{function_name}`, but to access them with the app, you'll need to start the app dev server. Under the hood, this uses `react-scripts`' [advanced proxy feature](https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#configuring-the-proxy-manually) with the `setupProxy.js` file.
2828

2929
### Run the app dev server
3030

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
"not op_mini all"
2828
],
2929
"devDependencies": {
30+
"babel-loader": "^8.0.4",
3031
"http-proxy-middleware": "^0.19.0",
31-
"npm-run-all": "^4.1.3",
32-
"netlify-lambda": "^0.4.0"
32+
"netlify-lambda": "^1.0.1",
33+
"npm-run-all": "^4.1.3"
3334
}
3435
}

src/App.css

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,29 @@
44

55
.App-logo {
66
animation: App-logo-spin infinite 20s linear;
7-
height: 80px;
7+
height: 40vmin;
88
}
99

1010
.App-header {
11-
background-color: #222;
12-
height: 150px;
13-
padding: 20px;
11+
background-color: #282c34;
12+
min-height: 100vh;
13+
display: flex;
14+
flex-direction: column;
15+
align-items: center;
16+
justify-content: center;
17+
font-size: calc(10px + 2vmin);
1418
color: white;
1519
}
1620

17-
.App-title {
18-
font-size: 1.5em;
19-
}
20-
21-
.App-intro {
22-
font-size: large;
21+
.App-link {
22+
color: #61dafb;
2323
}
2424

2525
@keyframes App-logo-spin {
26-
from { transform: rotate(0deg); }
27-
to { transform: rotate(360deg); }
26+
from {
27+
transform: rotate(0deg);
28+
}
29+
to {
30+
transform: rotate(360deg);
31+
}
2832
}

src/App.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,30 @@ import './App.css';
55
class LambdaDemo extends Component {
66
constructor(props) {
77
super(props);
8-
this.state = {loading: false, msg: null};
8+
this.state = { loading: false, msg: null };
99
}
1010

11-
handleClick = (e) => {
11+
handleClick = e => {
1212
e.preventDefault();
1313

14-
this.setState({loading: true});
14+
this.setState({ loading: true });
1515
fetch('/.netlify/functions/hello')
1616
.then(response => response.json())
17-
.then(json => this.setState({loading: false, msg: json.msg}));
18-
}
17+
.then(json => this.setState({ loading: false, msg: json.msg }));
18+
};
1919

2020
render() {
21-
const {loading, msg} = this.state;
21+
const { loading, msg } = this.state;
2222

23-
return <p>
24-
<button onClick={this.handleClick}>{loading ? 'Loading...' : 'Call Lambda'}</button><br/>
25-
<span>{msg}</span>
26-
</p>
23+
return (
24+
<p>
25+
<button onClick={this.handleClick}>
26+
{loading ? 'Loading...' : 'Call Lambda'}
27+
</button>
28+
<br />
29+
<span>{msg}</span>
30+
</p>
31+
);
2732
}
2833
}
2934

@@ -33,12 +38,11 @@ class App extends Component {
3338
<div className="App">
3439
<header className="App-header">
3540
<img src={logo} className="App-logo" alt="logo" />
36-
<h1 className="App-title">Welcome to React</h1>
41+
<p>
42+
Edit <code>src/App.js</code> and save to reload.
43+
</p>
44+
<LambdaDemo />
3745
</header>
38-
<p className="App-intro">
39-
To get started, edit <code>src/App.js</code> and save to reload.
40-
</p>
41-
<LambdaDemo/>
4246
</div>
4347
);
4448
}

src/App.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ import App from './App';
55
it('renders without crashing', () => {
66
const div = document.createElement('div');
77
ReactDOM.render(<App />, div);
8+
ReactDOM.unmountComponentAtNode(div);
89
});

src/index.css

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
body {
22
margin: 0;
33
padding: 0;
4-
font-family: sans-serif;
4+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
5+
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
6+
sans-serif;
7+
-webkit-font-smoothing: antialiased;
8+
-moz-osx-font-smoothing: grayscale;
9+
}
10+
11+
code {
12+
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
13+
monospace;
514
}

src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import React from 'react';
22
import ReactDOM from 'react-dom';
33
import './index.css';
44
import App from './App';
5-
import registerServiceWorker from './registerServiceWorker';
5+
import * as serviceWorker from './serviceWorker';
66

77
ReactDOM.render(<App />, document.getElementById('root'));
8-
registerServiceWorker();
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: http://bit.ly/CRA-PWA
12+
serviceWorker.unregister();

src/lambda/hello.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
// show object spread works, i.e. babel works
2+
const obj = {
3+
foo: 'bar'
4+
};
15
export function handler(event, context, callback) {
2-
console.log(event)
6+
console.log(event);
37
callback(null, {
48
statusCode: 200,
5-
body: JSON.stringify({msg: "Hello, World!"})
6-
})
9+
body: JSON.stringify({ msg: 'Hello, World!', ...obj })
10+
});
711
}

src/registerServiceWorker.js renamed to src/serviceWorker.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,41 @@ const isLocalhost = Boolean(
1818
)
1919
);
2020

21-
export default function register() {
21+
export function register(config) {
2222
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
2323
// The URL constructor is available in all browsers that support SW.
2424
const publicUrl = new URL(process.env.PUBLIC_URL, window.location);
2525
if (publicUrl.origin !== window.location.origin) {
2626
// Our service worker won't work if PUBLIC_URL is on a different origin
2727
// from what our page is served on. This might happen if a CDN is used to
28-
// serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374
28+
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
2929
return;
3030
}
3131

3232
window.addEventListener('load', () => {
3333
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
3434

3535
if (isLocalhost) {
36-
// This is running on localhost. Lets check if a service worker still exists or not.
37-
checkValidServiceWorker(swUrl);
36+
// This is running on localhost. Let's check if a service worker still exists or not.
37+
checkValidServiceWorker(swUrl, config);
38+
39+
// Add some additional logging to localhost, pointing developers to the
40+
// service worker/PWA documentation.
41+
navigator.serviceWorker.ready.then(() => {
42+
console.log(
43+
'This web app is being served cache-first by a service ' +
44+
'worker. To learn more, visit https://goo.gl/SC7cgQ'
45+
);
46+
});
3847
} else {
3948
// Is not local host. Just register service worker
40-
registerValidSW(swUrl);
49+
registerValidSW(swUrl, config);
4150
}
4251
});
4352
}
4453
}
4554

46-
function registerValidSW(swUrl) {
55+
function registerValidSW(swUrl, config) {
4756
navigator.serviceWorker
4857
.register(swUrl)
4958
.then(registration => {
@@ -57,11 +66,21 @@ function registerValidSW(swUrl) {
5766
// It's the perfect time to display a "New content is
5867
// available; please refresh." message in your web app.
5968
console.log('New content is available; please refresh.');
69+
70+
// Execute callback
71+
if (config && config.onUpdate) {
72+
config.onUpdate(registration);
73+
}
6074
} else {
6175
// At this point, everything has been precached.
6276
// It's the perfect time to display a
6377
// "Content is cached for offline use." message.
6478
console.log('Content is cached for offline use.');
79+
80+
// Execute callback
81+
if (config && config.onSuccess) {
82+
config.onSuccess(registration);
83+
}
6584
}
6685
}
6786
};
@@ -72,7 +91,7 @@ function registerValidSW(swUrl) {
7291
});
7392
}
7493

75-
function checkValidServiceWorker(swUrl) {
94+
function checkValidServiceWorker(swUrl, config) {
7695
// Check if the service worker can be found. If it can't reload the page.
7796
fetch(swUrl)
7897
.then(response => {
@@ -89,7 +108,7 @@ function checkValidServiceWorker(swUrl) {
89108
});
90109
} else {
91110
// Service worker found. Proceed as normal.
92-
registerValidSW(swUrl);
111+
registerValidSW(swUrl, config);
93112
}
94113
})
95114
.catch(() => {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy