Skip to content

Commit

Permalink
静止画像の場合はstaticでないURLにリダイレクトしてキャッシュヒットさせる
Browse files Browse the repository at this point in the history
  • Loading branch information
tamaina committed Feb 20, 2022
1 parent 3f0b914 commit db6acc9
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/backend/src/server/proxy/avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export async function proxyAvatar(ctx: Koa.Context) {
}
if (!user.avatarUrl) return ctx.redirect(Users.getIdenticonUrl(user));
if (ctx.query.url !== user.avatarUrl) {
logger.info(`redirect`);
// 最新の、キャッシュすべきURLへリダイレクト
logger.info(`redirect`);
const url = new URL(ctx.URL);
url.searchParams.set('url', user.avatarUrl);
return ctx.redirect(url.toString());
Expand All @@ -81,6 +81,20 @@ export async function proxyAvatar(ctx: Koa.Context) {
return ctx.redirect(Users.getIdenticonUrl(user));
}

//#region If image is not animated, redirect to non static url
const metadata = await sharp(path).metadata();
const isAnimated = metadata.pages && metadata.pages > 1;
if (ctx.query.static && !isAnimated) {
logger.info(`redirect to non static url`);
cleanup();
const url = new URL(ctx.URL);
url.searchParams.delete('static');
ctx.status = 301;
ctx.redirect(url.toString());
return;
}
//#endregion

ctx.set('Content-Type', 'image/webp');
ctx.set('Cache-Control', 'max-age=31536000, immutable');

Expand Down

0 comments on commit db6acc9

Please sign in to comment.