From 31fa3dcf26c6710866935f461ca2c2db8d581d63 Mon Sep 17 00:00:00 2001
From: ekzyis <ek@stacker.news>
Date: Thu, 26 Oct 2023 16:38:52 +0200
Subject: [PATCH] Add avatar cache busting using random query param

---
 components/user-header.js | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/components/user-header.js b/components/user-header.js
index 92fc6ad4..7974fe65 100644
--- a/components/user-header.js
+++ b/components/user-header.js
@@ -71,14 +71,22 @@ function HeaderPhoto ({ user, isMe }) {
             }
           }
         })
+      },
+      onCompleted ({ setPhoto: photoId }) {
+        // add random query param for cache busting
+        const randomParam = (Math.random() + 1).toString(16).substring(2)
+        const src = `https://${process.env.NEXT_PUBLIC_AWS_UPLOAD_BUCKET}.s3.amazonaws.com/${user.photoId}?r=${randomParam}`
+        setSrc(src)
       }
     }
   )
+  const initialSrc = user.photoId ? `https://${process.env.NEXT_PUBLIC_AWS_UPLOAD_BUCKET}.s3.amazonaws.com/${user.photoId}` : '/dorian400.jpg'
+  const [src, setSrc] = useState(initialSrc)
 
   return (
     <div className='position-relative align-self-start' style={{ width: 'fit-content' }}>
       <Image
-        src={user.photoId ? `https://${process.env.NEXT_PUBLIC_AWS_UPLOAD_BUCKET}.s3.amazonaws.com/${user.photoId}` : '/dorian400.jpg'} width='135' height='135'
+        src={src} width='135' height='135'
         className={styles.userimg}
       />
       {isMe &&