Add cache buster middleware to clear browser cache on server restart
| @@ -60,6 +60,7 @@ import basicAuthMiddleware from './src/middleware/basicAuth.js'; | ||
| 60 | 60 | import whitelistMiddleware from './src/middleware/whitelist.js'; |
| 61 | 61 | import multerMonkeyPatch from './src/middleware/multerMonkeyPatch.js'; |
| 62 | 62 | import initRequestProxy from './src/request-proxy.js'; |
| 63 | +import getCacheBusterMiddleware from './src/middleware/cacheBuster.js'; | |
| 63 | 64 | import { |
| 64 | 65 | getVersion, |
| 65 | 66 | getConfigValue, |
| @@ -515,7 +516,7 @@ if (!disableCsrf) { | ||
| 515 | 516 | |
| 516 | 517 | // Static files |
| 517 | 518 | // Host index page |
| 518 | 519 | app.get('/', getCacheBusterMiddleware(), (request, response) => { |
| 519 | 520 | if (shouldRedirectToLogin(request)) { |
| 520 | 521 | const query = request.url.split('?')[1]; |
| 521 | 522 | const redirectUrl = query ? `/login?${query}` : '/login'; |
| @@ -0,0 +1,22 @@ | ||
| 1 | +/** | |
| 2 | + * Middleware to bust the browser cache for the current user. | |
| 3 | + * @returns {import('express').RequestHandler} | |
| 4 | + */ | |
| 5 | +export default function getCacheBusterMiddleware() { | |
| 6 | + /** | |
| 7 | + * @type {Set<string>} Handles that have already been busted. | |
| 8 | + */ | |
| 9 | + const handles = new Set(); | |
| 10 | + | |
| 11 | + return (request, response, next) => { | |
| 12 | + const handle = request.user?.profile?.handle; | |
| 13 | + | |
| 14 | + if (!handle || handles.has(handle)) { | |
| 15 | + return next(); | |
| 16 | + } | |
| 17 | + | |
| 18 | + handles.add(handle); | |
| 19 | + response.setHeader('Clear-Site-Data', '"cache"'); | |
| 20 | + next(); | |
| 21 | + }; | |
| 22 | +} | |