enhance(server): set Cache-Control header for some routes

This commit is contained in:
syuilo 2023-01-21 13:20:09 +09:00
parent 59748f07d1
commit 0b4a7e8166

View file

@ -82,13 +82,13 @@ export class ServerService {
fastify.get<{ Params: { path: string }; Querystring: { static?: any; }; }>('/emoji/:path(.*)', async (request, reply) => { fastify.get<{ Params: { path: string }; Querystring: { static?: any; }; }>('/emoji/:path(.*)', async (request, reply) => {
const path = request.params.path; const path = request.params.path;
reply.header('Cache-Control', 'public, max-age=86400');
if (!path.match(/^[a-zA-Z0-9\-_@\.]+?\.webp$/)) { if (!path.match(/^[a-zA-Z0-9\-_@\.]+?\.webp$/)) {
reply.code(404); reply.code(404);
return; return;
} }
reply.header('Cache-Control', 'public, max-age=86400');
const name = path.split('@')[0].replace('.webp', ''); const name = path.split('@')[0].replace('.webp', '');
const host = path.split('@')[1]?.replace('.webp', ''); const host = path.split('@')[1]?.replace('.webp', '');
@ -132,6 +132,8 @@ export class ServerService {
relations: ['avatar'], relations: ['avatar'],
}); });
reply.header('Cache-Control', 'public, max-age=86400');
if (user) { if (user) {
reply.redirect(this.userEntityService.getAvatarUrlSync(user)); reply.redirect(this.userEntityService.getAvatarUrlSync(user));
} else { } else {
@ -143,6 +145,7 @@ export class ServerService {
const [temp, cleanup] = await createTemp(); const [temp, cleanup] = await createTemp();
await genIdenticon(request.params.x, fs.createWriteStream(temp)); await genIdenticon(request.params.x, fs.createWriteStream(temp));
reply.header('Content-Type', 'image/png'); reply.header('Content-Type', 'image/png');
reply.header('Cache-Control', 'public, max-age=86400');
return fs.createReadStream(temp).on('close', () => cleanup()); return fs.createReadStream(temp).on('close', () => cleanup());
}); });