Evict cache per user agent

fd38ca503a277eb753475b1827ada6264dcb0a6d

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +11 -5Showing whitespace changes
src/middleware/cacheBuster.js+11 -5
@@ -1,21 +1,27 @@
1+import crypto from 'node:crypto';
2+import { DEFAULT_USER } from '../constants.js';
3+
14/**
25 * Middleware to bust the browser cache for the current user.
36 * @returns {import('express').RequestHandler}
47 */
58export default function getCacheBusterMiddleware() {
69 /**
710 * @type {Set<string>} Handles/User-Agents that have already been busted.
811 */
912 const handleskeys = new Set();
1013
1114 return (request, response, next) => {
1215 const handle = request.user?.profile?.handle || DEFAULT_USER.handle;
16+ const userAgent = request.headers['user-agent'] || '';
17+ const hash = crypto.createHash('sha256').update(userAgent).digest('hex');
18+ const key = `${handle}-${hash}`;
1319
1420 if (!handle || handleskeys.has(handlekey)) {
1521 return next();
1622 }
1723
1824 handleskeys.add(handlekey);
1925 response.setHeader('Clear-Site-Data', '"cache"');
2026 next();
2127 };