Skip to content

Commit

Permalink
perf(imageproxy): do not set cookies to image proxy so CDNs can cache…
Browse files Browse the repository at this point in the history
… images (#3332)

CDNs such as Cloudflare bypass their cache if cookies are set in the response.
clearCookies
middleware removes the header before imageproxy serves the image.
  • Loading branch information
lunks committed Feb 12, 2023
1 parent 33e7691 commit 966639d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion server/index.ts
Expand Up @@ -17,6 +17,7 @@ import WebhookAgent from '@server/lib/notifications/agents/webhook';
import WebPushAgent from '@server/lib/notifications/agents/webpush';
import { getSettings } from '@server/lib/settings';
import logger from '@server/logger';
import clearCookies from '@server/middleware/clearcookies';
import routes from '@server/routes';
import imageproxy from '@server/routes/imageproxy';
import { getAppVersion } from '@server/utils/appVersion';
Expand Down Expand Up @@ -182,7 +183,8 @@ app
});
server.use('/api/v1', routes);

server.use('/imageproxy', imageproxy);
// Do not set cookies so CDNs can cache them
server.use('/imageproxy', clearCookies, imageproxy);

server.get('*', (req, res) => handle(req, res));
server.use(
Expand Down
6 changes: 6 additions & 0 deletions server/middleware/clearcookies.ts
@@ -0,0 +1,6 @@
const clearCookies: Middleware = (_req, res, next) => {
res.removeHeader('Set-Cookie');
next();
};

export default clearCookies;

0 comments on commit 966639d

Please sign in to comment.