From 0b4a7e8166318329883ebeb61d8376948e557cbc Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 21 Jan 2023 13:20:09 +0900 Subject: [PATCH] enhance(server): set Cache-Control header for some routes --- packages/backend/src/server/ServerService.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/server/ServerService.ts b/packages/backend/src/server/ServerService.ts index 47d11ed0ae..eb6a3795eb 100644 --- a/packages/backend/src/server/ServerService.ts +++ b/packages/backend/src/server/ServerService.ts @@ -82,13 +82,13 @@ export class ServerService { fastify.get<{ Params: { path: string }; Querystring: { static?: any; }; }>('/emoji/:path(.*)', async (request, reply) => { const path = request.params.path; + reply.header('Cache-Control', 'public, max-age=86400'); + if (!path.match(/^[a-zA-Z0-9\-_@\.]+?\.webp$/)) { reply.code(404); return; } - reply.header('Cache-Control', 'public, max-age=86400'); - const name = path.split('@')[0].replace('.webp', ''); const host = path.split('@')[1]?.replace('.webp', ''); @@ -132,6 +132,8 @@ export class ServerService { relations: ['avatar'], }); + reply.header('Cache-Control', 'public, max-age=86400'); + if (user) { reply.redirect(this.userEntityService.getAvatarUrlSync(user)); } else { @@ -143,6 +145,7 @@ export class ServerService { const [temp, cleanup] = await createTemp(); await genIdenticon(request.params.x, fs.createWriteStream(temp)); reply.header('Content-Type', 'image/png'); + reply.header('Cache-Control', 'public, max-age=86400'); return fs.createReadStream(temp).on('close', () => cleanup()); });