-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
68 lines (54 loc) · 1.45 KB
/
index.tsx
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { h, FC, renderSSR, Component } from 'nano-jsx'
import { createServer } from 'http'
const List: FC<{ data: string[] }> = ({ data }) => {
return (
<ul>
{data.map(d => (
<li>{d}</li>
))}
</ul>
)
}
class App extends Component {
render() {
return (
<div>
<h1>Nano JSX</h1>
LIST_PLACEHOLDER
</div>
)
}
}
// mock a data fetch with 1 second delay
const fetchData = async (): Promise<string[]> => {
const data = ['one', 'two']
return new Promise(resolve => {
setTimeout(() => {
resolve(data)
}, 1000)
})
}
const server = createServer(async (_req, res) => {
// send headers
res.setHeader('Content-Type', 'text/html')
// send first part to browser
res.write('<!DOCTYPE html><html lang="en"><body>')
// render the <App/> and split it where the <List/> component should be
const chunks = renderSSR(<App />).split('LIST_PLACEHOLDER')
// send first chunk of <App/> component
res.write(chunks[0])
// fetch the data for the <List/> component
const data = await fetchData()
// render and send the <List/> component
res.write(renderSSR(<List data={data} />))
// send second chunk of <App/> component
res.write(chunks[1])
// send rest of the html page
res.write('</body></html>')
// end the request
return res.end()
}).listen(8080, () => console.log('open http://localhost:8080'))
// graceful server shutdown
process.on('SIGTERM', () => {
server.close()
})