Skip to content

Commit

Permalink
cache-control private
Browse files Browse the repository at this point in the history
  • Loading branch information
mhuebert committed May 25, 2024
1 parent 1e9af43 commit f8225f9
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const getExtension = (path) => {
const serveHtml = async (res, path) => {
const htmlFile = bucket.file(path);
res.setHeader('Content-Type', 'text/html');
res.setHeader('Cache-Control', `public, max-age=${HTML_MAX_AGE}`);
res.setHeader('Cache-Control', `private, max-age=${HTML_MAX_AGE}`);
return new Promise((resolve, reject) => {
htmlFile.createReadStream()
.on('error', reject)
Expand All @@ -54,7 +54,7 @@ const serveHtml = async (res, path) => {
const pipeFile = async (res, path) => {
const { mime, stream } = await getMimeType(bucket.file(path).createReadStream(), { filename: path });
res.setHeader('Content-Type', mime);
res.setHeader('Cache-Control', `public, max-age=${HTML_MAX_AGE}`);
res.setHeader('Cache-Control', `private, max-age=${HTML_MAX_AGE}`);
return stream.pipe(res);
};

Expand All @@ -65,11 +65,11 @@ const redirectFile = async (res, path) => {
// in practice, IAP (Identity Aware Proxy) is adding cache-busting headers to
// this redirect because it is behind an auth wall. I haven't figured out a way
// to circumvent them for these static assets.

const [signedUrl, expires] = await generateSignedUrl(path);
const maxAge = (expires - Date.now()) / 1000; // Calculate max-age in seconds
res.setHeader('Expires', new Date(expires).toUTCString());
res.setHeader('Cache-Control', `public, max-age=${maxAge}`);
res.setHeader('Cache-Control', `private, max-age=${maxAge}`);
return res.redirect(302, signedUrl);
};

Expand Down

0 comments on commit f8225f9

Please sign in to comment.