chunk-cache
v0.0.3
Published
Simple cache for streamed HTML responses
Downloads
50
Maintainers
Readme
chunk-cache
Simple caching for streamed HTML.
Install
npm i chunk-cache --save
Usage
const app = require('connect')()
const router = require('router')()
const cache = require('chunk-cache')
router.get('*', (req, res) => {
const hit = cache(req.originalUrl)
if (hit) {
res.writeHead(200, { 'Content-Type': 'text/html' })
res.write(hit)
res.end()
}
const cacheStream = cache(req.originalUrl, 60 * 60 * 1000)
cacheStream.pipe(res)
res.writeHead(200, { 'Content-Type': 'text/html' })
cacheStream.write(`<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>chunk-cache</title>
</head>
<body>
<div id='root'>`
)
const renderStream = renderToStream(/* render something */)
renderStream.pipe(cacheStream, { end: false })
renderStream.on('end', () => {
cacheStream.write(`</div>
</body>
</html>
`)
cacheStream.end()
})
})
Inspiration
License
MIT License © Eric Bailey