File size: 855 Bytes
519a20c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import crypto from 'node:crypto';
import { DEFAULT_USER } from '../constants.js';

/**
 * Middleware to bust the browser cache for the current user.
 * @returns {import('express').RequestHandler}
 */
export default function getCacheBusterMiddleware() {
    /**
     * @type {Set<string>} Handles/User-Agents that have already been busted.
     */
    const keys = new Set();

    return (request, response, next) => {
        const handle = request.user?.profile?.handle || DEFAULT_USER.handle;
        const userAgent = request.headers['user-agent'] || '';
        const hash = crypto.createHash('sha256').update(userAgent).digest('hex');
        const key = `${handle}-${hash}`;

        if (keys.has(key)) {
            return next();
        }

        keys.add(key);
        response.setHeader('Clear-Site-Data', '"cache"');
        next();
    };
}