Add cache buster middleware to clear browser cache on server restart
| @@ -60,6 +60,7 @@ import basicAuthMiddleware from './src/middleware/basicAuth.js'; | |||
| 60 | import whitelistMiddleware from './src/middleware/whitelist.js'; | 60 | import whitelistMiddleware from './src/middleware/whitelist.js'; |
| 61 | import multerMonkeyPatch from './src/middleware/multerMonkeyPatch.js'; | 61 | import multerMonkeyPatch from './src/middleware/multerMonkeyPatch.js'; |
| 62 | import initRequestProxy from './src/request-proxy.js'; | 62 | import initRequestProxy from './src/request-proxy.js'; |
| 63 | import getCacheBusterMiddleware from './src/middleware/cacheBuster.js'; | ||
| 63 | import { | 64 | import { |
| 64 | getVersion, | 65 | getVersion, |
| 65 | getConfigValue, | 66 | getConfigValue, |
| @@ -515,7 +516,7 @@ if (!disableCsrf) { | |||
| 515 | 516 | ||
| 516 | // Static files | 517 | // Static files |
| 517 | // Host index page | 518 | // Host index page |
| 518 | app.get('/', (request, response) => { | 519 | app.get('/', getCacheBusterMiddleware(), (request, response) => { |
| 519 | if (shouldRedirectToLogin(request)) { | 520 | if (shouldRedirectToLogin(request)) { |
| 520 | const query = request.url.split('?')[1]; | 521 | const query = request.url.split('?')[1]; |
| 521 | const redirectUrl = query ? `/login?${query}` : '/login'; | 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 | } | ||