Skip to content

Commit

Permalink
Chore: set last modified header for apps using express to serve front…
Browse files Browse the repository at this point in the history
…end assets [INTEG-2309] (#9256)

* typeform last modified header

* set last modified for ai image tagging
  • Loading branch information
sarahlessner authored Nov 21, 2024
1 parent 043cec5 commit 519faed
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
24 changes: 23 additions & 1 deletion apps/ai-image-tagging/lambda/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,34 @@ const app = express();

const FRONTEND = path.dirname(require.resolve('@contentful/ai-image-tagging-frontend'));

const computeLastModifiedTime = () => {
const deployTime = process.env.DEPLOY_TIME_UNIX;
if (typeof deployTime === 'undefined') {
throw new Error('Missing DEPLOY_TIME_UNIX env var');
}

// JS uses number of _milliseconds_ since epoch, whereas unix generates number in
// _seconds_ since epoch. So we have to convert
const epochTime = Number(deployTime) * 1000;
return new Date(epochTime).toUTCString();
};

app.use('/tags', async (req, res) => {
const { status, body } = await handle(req.method, req.path, deps);
res.status(status).json(body);
});

app.use('/frontend', express.static(FRONTEND));
const lastModified = computeLastModifiedTime();

app.use(
'/frontend',
express.static(FRONTEND, {
lastModified,
setHeaders: (res) => {
res.setHeader('Last-Modified', lastModified);
},
})
);
app.use((_req, res) => res.status(404).send('Not found'));

module.exports = app;
12 changes: 10 additions & 2 deletions apps/typeform/lambda/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const computeLastModifiedTime = () => {
// JS uses number of _milliseconds_ since epoch, whereas unix generates number in
// _seconds_ since epoch. So we have to convert
const epochTime = Number(deployTime) * 1000;
return new Date(epochTime).toGMTString();
return new Date(epochTime).toUTCString();
};

app.use('/forms', async (req, res) => {
Expand Down Expand Up @@ -64,7 +64,15 @@ app.use('/callback', async (req, res) => {
});

const lastModified = computeLastModifiedTime();
app.use('/frontend', express.static(FRONTEND, { lastModified }));
app.use(
'/frontend',
express.static(FRONTEND, {
lastModified,
setHeaders: (res) => {
res.setHeader('Last-Modified', lastModified);
},
})
);

app.use((_req, res) => res.status(404).send('Not found'));

Expand Down

0 comments on commit 519faed

Please sign in to comment.