| 1 | +import crypto from 'node:crypto'; |
| 2 | +import { DEFAULT_USER } from '../constants.js'; |
| 3 | + |
| 1 | 4 | /** |
| 2 | 5 | * Middleware to bust the browser cache for the current user. |
| 3 | 6 | * @returns {import('express').RequestHandler} |
| 4 | 7 | */ |
| 5 | 8 | export default function getCacheBusterMiddleware() { |
| 6 | 9 | /** |
| 7 | 10 | * @type {Set<string>} Handles/User-Agents that have already been busted. |
| 8 | 11 | */ |
| 9 | 12 | const handleskeys = new Set(); |
| 10 | 13 | |
| 11 | 14 | return (request, response, next) => { |
| 12 | 15 | 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}`; |
| 13 | 19 | |
| 14 | 20 | if (!handle || handleskeys.has(handlekey)) { |
| 15 | 21 | return next(); |
| 16 | 22 | } |
| 17 | 23 | |
| 18 | 24 | handleskeys.add(handlekey); |
| 19 | 25 | response.setHeader('Clear-Site-Data', '"cache"'); |
| 20 | 26 | next(); |
| 21 | 27 | }; |