Add cache buster middleware to clear browser cache on server restart

This commit is contained in:
Cohee 2025-02-15 12:56:43 +02:00
parent 96d6a6df07
commit bae02a44ed
2 changed files with 24 additions and 1 deletions

View File

@ -60,6 +60,7 @@ import basicAuthMiddleware from './src/middleware/basicAuth.js';
import whitelistMiddleware from './src/middleware/whitelist.js';
import multerMonkeyPatch from './src/middleware/multerMonkeyPatch.js';
import initRequestProxy from './src/request-proxy.js';
import getCacheBusterMiddleware from './src/middleware/cacheBuster.js';
import {
getVersion,
getConfigValue,
@ -515,7 +516,7 @@ if (!disableCsrf) {
// Static files
// Host index page
app.get('/', (request, response) => {
app.get('/', getCacheBusterMiddleware(), (request, response) => {
if (shouldRedirectToLogin(request)) {
const query = request.url.split('?')[1];
const redirectUrl = query ? `/login?${query}` : '/login';

View File

@ -0,0 +1,22 @@
/**
* Middleware to bust the browser cache for the current user.
* @returns {import('express').RequestHandler}
*/
export default function getCacheBusterMiddleware() {
/**
* @type {Set<string>} Handles that have already been busted.
*/
const handles = new Set();
return (request, response, next) => {
const handle = request.user?.profile?.handle;
if (!handle || handles.has(handle)) {
return next();
}
handles.add(handle);
response.setHeader('Clear-Site-Data', '"cache"');
next();
};
}