JSPM

chunk-cache

0.0.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 4
  • Score
    100M100P100Q60350F
  • License MIT

Simple cache for streamed HTML responses

Package Exports

  • chunk-cache

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (chunk-cache) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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