Fix avatar not refreshing when re-uploaded with same filename
What does this MR do and why?
- Fix stale avatar display when re-uploading with the same filename
- Add a
?v=<updated_at_timestamp>cache-busting query parameter to avatar URLs - Affects projects, groups, and users — any model using the
Avatarableconcern
Details
Root cause: Avatar URLs are constructed as /{base_dir}/{model_type}/avatar/{id}/{filename}. When uploading a new avatar with the same filename, the URL is identical, so the browser serves the cached image (5-minute Cache-Control for projects/groups). No ETag or Last-Modified headers are set to help the browser detect content changes.
Fix: Append a version query parameter (?v=<updated_at_timestamp>) to avatar URLs. When the avatar is re-uploaded, the model's updated_at changes, producing a new URL that bypasses the browser cache. Unpersisted objects (without updated_at) get no version parameter, preserving existing behavior. The width parameter is also preserved when present (e.g., ?v=1234567890&width=32).
References
Avatar doesn't get refreshed (#393608 - closed)
How to validate manually
- Go to any project's Settings > General
- Upload an image as the avatar (e.g.,
logo.png) and save - Confirm the avatar displays correctly
- Prepare a different image but save it with the same filename (
logo.png) - Go back to Settings > General, upload this new
logo.pngas the avatar and save - Before this fix: the old avatar still shows — requires hard refresh to see the new one
- After this fix: the new avatar displays immediately without any manual cache clearing
References
- Resolves #393608 (closed)