mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Evict cache per user agent
This commit is contained in:
@@ -1,21 +1,27 @@
|
|||||||
|
import crypto from 'node:crypto';
|
||||||
|
import { DEFAULT_USER } from '../constants.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Middleware to bust the browser cache for the current user.
|
* Middleware to bust the browser cache for the current user.
|
||||||
* @returns {import('express').RequestHandler}
|
* @returns {import('express').RequestHandler}
|
||||||
*/
|
*/
|
||||||
export default function getCacheBusterMiddleware() {
|
export default function getCacheBusterMiddleware() {
|
||||||
/**
|
/**
|
||||||
* @type {Set<string>} Handles that have already been busted.
|
* @type {Set<string>} Handles/User-Agents that have already been busted.
|
||||||
*/
|
*/
|
||||||
const handles = new Set();
|
const keys = new Set();
|
||||||
|
|
||||||
return (request, response, next) => {
|
return (request, response, next) => {
|
||||||
const handle = request.user?.profile?.handle;
|
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 (!handle || handles.has(handle)) {
|
if (keys.has(key)) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
handles.add(handle);
|
keys.add(key);
|
||||||
response.setHeader('Clear-Site-Data', '"cache"');
|
response.setHeader('Clear-Site-Data', '"cache"');
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user