From c9375fb5eeb68ae7c95a1b9924f9dab533977116 Mon Sep 17 00:00:00 2001 From: PieterCK Date: Wed, 16 Oct 2024 16:11:43 +0700 Subject: [PATCH] storage: Hash system bots avatar files. This commit makes sure system bots avatar files are hashed when served as static files. This way, requests for these avatar files will be served with long-lived caching headers by our nginx (see #22275). We don't need to worry about stale caches for these files because they will only be used by system bots. Fixes #31458. --- zerver/lib/avatar.py | 2 ++ zerver/lib/storage.py | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/zerver/lib/avatar.py b/zerver/lib/avatar.py index 69c12cf77d..f33cdc6c22 100644 --- a/zerver/lib/avatar.py +++ b/zerver/lib/avatar.py @@ -14,6 +14,8 @@ from zerver.lib.url_encoding import append_url_query_string from zerver.models import UserProfile SYSTEM_BOTS_AVATAR_FILES = { + # This is also used in zerver/lib/storage.py to ensure + # these files are hashed when served as static files. settings.WELCOME_BOT: "images/welcome-bot.png", settings.NOTIFICATION_BOT: "images/logo/zulip-icon-square.svg", settings.EMAIL_GATEWAY_BOT: "images/email-gateway-bot.png", diff --git a/zerver/lib/storage.py b/zerver/lib/storage.py index 1710010065..956b62ed96 100644 --- a/zerver/lib/storage.py +++ b/zerver/lib/storage.py @@ -10,6 +10,8 @@ from django.core.files.base import File from django.core.files.storage import FileSystemStorage from typing_extensions import override +from zerver.lib.avatar import SYSTEM_BOTS_AVATAR_FILES + if settings.DEBUG: from django.contrib.staticfiles.finders import find @@ -38,6 +40,12 @@ class IgnoreBundlesManifestStaticFilesStorage(ManifestStaticFilesStorage): # use a no-op hash function for these already-hashed # assets. return name + if name in SYSTEM_BOTS_AVATAR_FILES.values(): + # For these avatar files, we want to make sure they are + # so they can hit our Nginx caching block for static files. + # We don't need to worry about stale caches since system bot + # avatars rarely change. + return super().hashed_name(name, content, filename) if name == "generated/emoji/emoji_api.json": # Unlike most .json files, we do want to hash this file; # its hashed URL is returned as part of the API. See