CacheControl header for Static Assets on S3

JS
S
JavaScript

In this snippet we show the usage of CacheControl header (as metadata) to prevent browsers from caching assets. Additionally Expire header can be used with an old date. The Cache-Control general-header field is used to specify directives for caching mechanisms in both requests and responses. If the browser receives max-age=0, it will revalidate cache immediately. If a request includes the no-cache directive, it SHOULD NOT include min-fresh, max-stale, or max-age. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control

1const baseOptions = {
2  Bucket: bucket,
3  ACL: 'public-read',
4  ContentType: 'application/pdf',
5  CacheControl: 'max-age=0'
6};
7const upload = {
8  ...baseOptions,
9  Key: key,
10  Body: $stream,
11  ContentDisposition: 'attachment; filename="' + key + '"'
12};
13return new Promise(function createFolderPromise(resolve, reject) {
14  s3.upload(upload, function handleAWSresult(err, data) {
15    if (err) {
16      console.error(new Error('error from aws'), err);
17      return reject(err);
18    }
19    console.log({ msg: 'aws result' }, data);
20    return resolve(data);
21  });
22});

Created on 6/12/2018