Skip to content

Instantly share code, notes, and snippets.

@chux0519
Created May 22, 2018 07:04
Show Gist options
  • Save chux0519/4a3790e6b5e7b17735c34d44a8366264 to your computer and use it in GitHub Desktop.
Save chux0519/4a3790e6b5e7b17735c34d44a8366264 to your computer and use it in GitHub Desktop.
superstatic, etag
const superstatic = require('superstatic').server
const path = require('path')
const fsProvider = require('superstatic/lib/providers/fs')
// Remove etag, cancel the browser's cache, avoid update issues
function noCacheProvider (options) {
const handler = fsProvider(options)
return (req, pathname) => handler(req, pathname).then(res => {
if (res) { // res can be null
res.modified = false
res.etag = false
}
return res
})
}
const config = {
'rewrites': [
{'source': '/*', 'destination': 'index.html'}
]
}
const port = Number(process.env.PORT) || 8080
const distPath = path.join(__dirname, './dist')
const app = superstatic({
provider: noCacheProvider({cwd: distPath, ...config}),
port,
cwd: distPath,
config,
compression: true
})
app.listen(() => {
console.log(`Serving on port ${port}...`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment