Bust avatar/banner cache so new image shows on all pages

After my previous fix the upload succeeds, but the avatar path is always
.../profile/avatar.<ext>, so its media URL never changes. Pages other than the
channel (which cache-busts with ?t=) kept serving the browser-cached OLD image
— the user "still saw the old avatar" on /videos.

- avatar_url / banner_url accessors append ?v=<updated_at timestamp>.
- updateAvatar / updateBanner now bump updated_at explicitly (the path is
  unchanged, so Eloquent wouldn't otherwise touch it), changing ?v so every
  page fetches the new image.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
ghassan 2026-07-29 00:39:47 +03:00
parent 5d4d21dcec
commit 8ea63a6720
2 changed files with 15 additions and 4 deletions

View File

@ -565,7 +565,11 @@ class UserController extends Controller
rename($tempPath, $destPath); rename($tempPath, $destPath);
} }
$user->update(['avatar' => $relPath]); // Bump updated_at even when $relPath is unchanged (avatar.png -> avatar.png)
// so avatar_url's ?v cache-buster changes and every page shows the new image.
$user->avatar = $relPath;
$user->updated_at = now();
$user->save();
if ($nas->isEnabled() && file_exists($destPath)) { if ($nas->isEnabled() && file_exists($destPath)) {
$nas->syncAvatar($user, $destPath); $nas->syncAvatar($user, $destPath);
@ -602,7 +606,11 @@ class UserController extends Controller
rename($tempPath, $destPath); rename($tempPath, $destPath);
} }
$user->update(['banner' => $relPath]); // Bump updated_at even when $relPath is unchanged so banner_url's ?v buster
// changes and the new banner shows everywhere (see updateAvatar).
$user->banner = $relPath;
$user->updated_at = now();
$user->save();
if ($nas->isEnabled() && file_exists($destPath)) { if ($nas->isEnabled() && file_exists($destPath)) {
$nas->syncCover($user, $destPath); $nas->syncCover($user, $destPath);

View File

@ -162,7 +162,10 @@ class User extends Authenticatable implements MustVerifyEmail
public function getAvatarUrlAttribute(): string public function getAvatarUrlAttribute(): string
{ {
if ($this->avatar) { if ($this->avatar) {
return route('media.avatar', $this->avatar); // ?v busts the browser cache when the avatar is replaced. The path is
// always .../profile/avatar.<ext>, so without a version every page would
// keep showing the previously cached image after an upload.
return route('media.avatar', $this->avatar).'?v='.($this->updated_at?->timestamp ?? '0');
} }
return 'https://i.pravatar.cc/150?u='.$this->id; return 'https://i.pravatar.cc/150?u='.$this->id;
@ -171,7 +174,7 @@ class User extends Authenticatable implements MustVerifyEmail
public function getBannerUrlAttribute(): ?string public function getBannerUrlAttribute(): ?string
{ {
if ($this->banner) { if ($this->banner) {
return route('media.banner', $this->banner); return route('media.banner', $this->banner).'?v='.($this->updated_at?->timestamp ?? '0');
} }
return null; return null;
} }