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
1 changed files with 16 additions and 2 deletions

View File

@ -10,6 +10,8 @@ import { sapperInlineScriptChecksums } from './server/sapperInlineScriptChecksum
const { PORT = 4002 } = process.env
const app = express()
const MAX_AGE = 3600
// this allows us to do e.g. `fetch('/_api/blog')` on the server
global.fetch = (url, opts) => {
if (url[0] === '/') {
@ -50,14 +52,26 @@ app.use(coreHtmlFilesOnly(helmet({
app.use(serveStatic('static', {
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/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, () => {
console.log(`listening on port ${PORT}`)