File tree 4 files changed +65
-1
lines changed
4 files changed +65
-1
lines changed Original file line number Diff line number Diff line change
1
+ import React , { Fragment } from "react" ;
2
+
3
+ const App = ( props ) => {
4
+ return (
5
+ < div > React!</ div >
6
+ )
7
+ }
8
+
9
+ export default App ;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change 8
8
"compression" : " ^1.7.2" ,
9
9
"cors" : " ^2.8.4" ,
10
10
"express" : " ^4.16.3" ,
11
+ "isomorphic-fetch" : " ^2.2.1" ,
11
12
"morgan" : " ^1.9.0" ,
12
13
"react" : " ^16.4.0" ,
13
14
"react-dom" : " ^16.4.0" ,
21
22
"build" : " npm-run-all --parallel build:**" ,
22
23
"build:app" : " react-scripts build" ,
23
24
"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" ,
24
27
"test" : " react-scripts test --env=jsdom"
25
28
},
26
29
"devDependencies" : {
27
30
"cross-env" : " ^5.2.0" ,
28
31
"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"
30
34
},
31
35
"proxy" : {
32
36
"/.netlify/functions" : {
You can’t perform that action at this time.
0 commit comments