Skip to content
Simple caching for streamed HTML.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
README.md
index.js
package.json

README.md

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

You can’t perform that action at this time.