fix: set max-age to 3600 for html (#989)

another attempt to address #985
This commit is contained in:
Nolan Lawson 2019-02-14 21:26:20 -08:00 committed by GitHub
parent d947f819ab
commit a97600d4a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -10,6 +10,8 @@ import { sapperInlineScriptChecksums } from './server/sapperInlineScriptChecksum
const { PORT = 4002 } = process.env const { PORT = 4002 } = process.env
const app = express() const app = express()
const MAX_AGE = 3600
// this allows us to do e.g. `fetch('/_api/blog')` on the server // this allows us to do e.g. `fetch('/_api/blog')` on the server
global.fetch = (url, opts) => { global.fetch = (url, opts) => {
if (url[0] === '/') { if (url[0] === '/') {
@ -50,14 +52,26 @@ app.use(coreHtmlFilesOnly(helmet({
app.use(serveStatic('static', { app.use(serveStatic('static', {
setHeaders: (res) => { setHeaders: (res) => {
res.setHeader('Cache-Control', 'public,max-age=3600') res.setHeader('Cache-Control', `public,max-age=${MAX_AGE}`)
} }
})) }))
app.use(express.static('__sapper__/build/client/report.html')) app.use(express.static('__sapper__/build/client/report.html'))
app.use(express.static('__sapper__/build/client/stats.json')) app.use(express.static('__sapper__/build/client/stats.json'))
app.use(sapper.middleware()) // TODO: hack to override Sapper's default max-age of 600 for HTML files
function overrideSetHeader (req, res, next) {
const origSetHeader = res.setHeader
res.setHeader = function (key, value) {
if (key === 'Cache-Control' && value === 'max-age=600') {
return origSetHeader.apply(this, ['Cache-Control', `max-age=${MAX_AGE}`])
}
return origSetHeader.apply(this, arguments)
}
return next()
}
app.use(overrideSetHeader, sapper.middleware())
app.listen(PORT, () => { app.listen(PORT, () => {
console.log(`listening on port ${PORT}`) console.log(`listening on port ${PORT}`)