import { saveToCache } from '../files.js'
import { logger } from '../logger.js'

export default async function (req, res, next) {
  try {
    if (!res.body) return
    if (res.cache === false) return
    saveToCache({
      version: res.version,
      path: req.path,
      body: res.body,
      headers: res.headers,
      sha256Sum: res.locals.sha256Sum
    }).catch(() => logger.error)
  } catch (err) {
    next(err)
  } finally {
    next()
  }
}