Skip to content

Commit

Permalink
set custom cache control in hoster
Browse files Browse the repository at this point in the history
  • Loading branch information
suren-atoyan committed Jun 21, 2020
1 parent 5db2fc2 commit 3813e73
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion hoster/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,27 @@ const port = process.env.PORT || 8089;

const indexTmplPath = path.join(__dirname, '../build/index.html');

function setCustomCacheControl(res, path) {
const hashRegExp = new RegExp('\\.[0-9a-f]{8}\\.');

if (path.endsWith('.html')) {
// Never cache index.html or other .html files
res.setHeader('Cache-Control', 'no-cache');
} else if (hashRegExp.test(path)) {
// Always cache files with "fingerprint", they will never change
// like 0.c2572769.chunk.js
res.setHeader('Cache-Control', 'max-age=31536000');
}
}

async function init() {
const app = express();

app.use(express.static(path.join(__dirname, '../build/')));
app.use(express.static(path.join(__dirname, '../build/'), {
etag: true,
lastModified: true,
setHeaders: setCustomCacheControl,
}));

app.get('*', (req, res) => {
res.sendFile(indexTmplPath);
Expand Down

0 comments on commit 3813e73

Please sign in to comment.