-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (48 loc) · 1.64 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
51
52
53
54
55
56
57
58
59
import http from 'http'
import fs from 'fs'
import React from 'react'
import ReactDOM from 'react-dom/server'
import { App } from './App.js'
import { StaticRouter } from './router.js'
const h = React.createElement
const Document = ({ children }) => (
h('html', {},
h('head', {},
h('meta', { charSet: 'utf-8' }),
h('meta', { name: 'viewport', content: 'width=device-width' })
),
h('body', {},
h('div', { id: 'root' }, children),
h('script', { type: 'importmap', dangerouslySetInnerHTML: {
__html: JSON.stringify({ imports: {
'react': 'https://esm.sh/[email protected]?dev',
'react-dom/client': 'https://esm.sh/[email protected]/client?dev'
} })
} }),
h('script', { type: 'module', src: '/client.js' })
)
)
)
const renderApp = ({ location }) => h(Document, {}, h(StaticRouter, { location } , h(App)))
const server = http.createServer((req, res) => {
console.log()
console.log(req.method, req.url, req.headers['user-agent'])
const url = new URL(req.url, `http://${req.headers.host}`)
if (url.pathname.endsWith('.js')) {
res.setHeader('Content-Type', 'text/javascript')
fs.createReadStream(`./${url.pathname.slice(1)}`).pipe(res)
return
}
const app = renderApp({ location: url.pathname })
res.setHeader('Content-Type', 'text/html')
const emulateBot = url.search === '?bot'
const isBot = ['bot', 'Bot'].some(e => req.headers['user-agent'].includes(e))
if (!isBot && !emulateBot) return res.end('<!DOCTYPE html>' + ReactDOM.renderToString(app))
const { startWriting } = ReactDOM.pipeToNodeWritable(app, res, {
onReadyToStream() {
res.write('<!DOCTYPE html>')
startWriting()
}
})
})
server.listen(4000)