Skip to content

Commit 407db83

Browse files
committed
build react ssr?
1 parent 5d8ddb9 commit 407db83

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

functions/react-app/App.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React,{Fragment} from "react";
2+
3+
const App = (props) => {
4+
return (
5+
<div>React!</div>
6+
)
7+
}
8+
9+
export default App;

functions/react-app/usersData.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import React from 'react';
2+
import fetch from 'isomorphic-fetch';
3+
4+
export default function Data() {
5+
return fetch('https://jsonplaceholder.typicode.com/users').then(data => data.json())
6+
}

functions/react-ssr.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import serverless from "serverless-http"
2+
import express from "express"
3+
import cors from "cors"
4+
import bodyParser from "body-parser"
5+
import React from "react"
6+
import { renderToString } from "react-dom/server"
7+
import App from "./react-app/App"
8+
import Data from "./react-app/usersData"
9+
import fs from "fs"
10+
import path from "path"
11+
12+
const app = express()
13+
14+
app.use(cors())
15+
app.use(bodyParser.json())
16+
app.use(bodyParser.urlencoded({ extended: false }))
17+
// app.use(express.static(path.resolve(__dirname, "./Browser")))
18+
19+
20+
const markup = `<!DOCTYPE html>
21+
<html lang="en">
22+
<head>
23+
<meta charset="UTF-8">
24+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
25+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
26+
<title>SSR- React</title>
27+
<link rel="stylesheet" href="/dev/bundle.css">
28+
</head>
29+
<body>
30+
<div id="root"><!--App--></div>
31+
32+
<script src="/dev/bundle.js"></script>
33+
</body>
34+
</html>`
35+
36+
const routerBasePath = (process.env.NODE_ENV === 'dev') ? `/${functionName}` : `/.netlify/functions/${functionName}/`
37+
38+
app.get(routerBasePath, (req, res) => {
39+
Data().then(users => {
40+
const html = renderToString(<App data={users} />)
41+
res.send(markup.replace("<!--App-->", html))
42+
})
43+
})
44+
45+
exports.handler = serverless(app)

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"compression": "^1.7.2",
99
"cors": "^2.8.4",
1010
"express": "^4.16.3",
11+
"isomorphic-fetch": "^2.2.1",
1112
"morgan": "^1.9.0",
1213
"react": "^16.4.0",
1314
"react-dom": "^16.4.0",
@@ -21,12 +22,15 @@
2122
"build": "npm-run-all --parallel build:**",
2223
"build:app": "react-scripts build",
2324
"build:functions": "netlify-lambda build functions",
25+
"postbuild": "npm run react",
26+
"react": "cross-env BABEL_ENV=server parcel build functions/react-ssr.js -d functions-build --public-url /functions-build -o react-test.js --no-minify",
2427
"test": "react-scripts test --env=jsdom"
2528
},
2629
"devDependencies": {
2730
"cross-env": "^5.2.0",
2831
"netlify-lambda": "^0.4.0",
29-
"npm-run-all": "^4.1.3"
32+
"npm-run-all": "^4.1.3",
33+
"parcel-bundler": "^1.9.4"
3034
},
3135
"proxy": {
3236
"/.netlify/functions": {

0 commit comments

Comments
 (0)