Commit d2e7c433 authored by Evan You's avatar Evan You

use renderToString

parent 4a7ac7f6
...@@ -65,6 +65,7 @@ app.use('/service-worker.js', serve('./dist/service-worker.js')) ...@@ -65,6 +65,7 @@ app.use('/service-worker.js', serve('./dist/service-worker.js'))
// 1-second microcache. // 1-second microcache.
const pageCache = LRU({ const pageCache = LRU({
max: 100,
maxAge: 1000 maxAge: 1000
}) })
...@@ -99,31 +100,25 @@ app.get('*', (req, res) => { ...@@ -99,31 +100,25 @@ app.get('*', (req, res) => {
if (cacheable) { if (cacheable) {
const hit = pageCache.get(req.url) const hit = pageCache.get(req.url)
if (hit) { if (hit) {
if (!isProd) {
console.log(`cache hit!`) console.log(`cache hit!`)
}
return res.end(hit) return res.end(hit)
} }
} }
const context = { url: req.url } renderer.renderToString({ url: req.url }, (err, html) => {
const stream = renderer.renderToStream(context) if (err) {
return handleError(err)
}
res.end(html)
if (cacheable) { if (cacheable) {
let content = '' pageCache.set(req.url, html)
stream
.on('data', chunk => {
content += chunk.toString()
})
.on('end', () => {
pageCache.set(req.url, content)
})
} }
if (!isProd) {
stream
.on('error', errorHandler)
.on('end', () => {
console.log(`whole request: ${Date.now() - s}ms`) console.log(`whole request: ${Date.now() - s}ms`)
}
}) })
.pipe(res)
}) })
const port = process.env.PORT || 8080 const port = process.env.PORT || 8080
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment