chunk-cache

0.0.3 • Public • Published

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

Package Sidebar

Install

npm i chunk-cache

Weekly Downloads

1

Version

0.0.3

License

MIT

Unpacked Size

2.5 kB

Total Files

3

Last publish

Collaborators

  • estrattonbailey