-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
50 lines (46 loc) · 1.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
import React from "react";
import ReactDOMServer from "react-dom/server";
import SSRApp from "../SSRApp";
import config from "../config.json";
import axios from "axios";
const indexFile = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<div>Rendered on Server</div>
</body>
</html>`;
const handler = async function (event) {
try {
const url = config.SSRApiStack.apiurl;
const result = await axios.get(url);
const app = ReactDOMServer.renderToString(<SSRApp data={result.data} />);
const html = indexFile.replace(
'<div id="root"></div>',
`<div id="root">${app}</div>`
);
return {
statusCode: 200,
headers: { "Content-Type": "text/html" },
body: html,
};
} catch (error) {
console.log(`Error ${error.message}`);
return `Error ${error}`;
}
};
export { handler };