1
0
mirror of synced 2025-12-22 03:16:52 -05:00
Files
docs/middleware/favicon.js
Peter Bengtsson 6361591c45 serve a /favicon.ico (#24935)
* serve a /favicon.ico

* fix exception

* avoid res.sendFile() bcause CodeQL is worried
2022-02-03 00:11:59 +00:00

28 lines
1.0 KiB
JavaScript

// We actually don't rely and use favicon.ico but it's nevertheless a
// very common request.
// Because we store our images, including those not for the Markdown text,
// in the `assets/images/site` directory, we will use a custom
// solution to serve this directly.
import fs from 'fs'
import { SURROGATE_ENUMS, setFastlySurrogateKey } from './set-fastly-surrogate-key.js'
import { cacheControlFactory } from './cache-control.js'
const cacheControl = cacheControlFactory(60 * 60 * 24 * 7)
const faviconPayloadBuffer = fs.readFileSync('assets/images/site/favicon.ico')
export default function assetPreprocessing(req, res, next) {
if (req.path !== '/favicon.ico') return next()
// This makes sure the CDN caching survives each production deployment.
setFastlySurrogateKey(res, SURROGATE_ENUMS.MANUAL)
// Manually settings a Cache-Control because no other middleware
// will get a chance to do this later since we terminate here.
cacheControl(res)
res.set('content-type', 'image/x-icon')
res.send(faviconPayloadBuffer)
}