next/image conditional cache update – How can I conditionally update the a next/image cache values for a set URL?

Same URL + user activity that updates data = update (not turn off) next/image cache values for same URL.

When the user uploads a new profile image I want to replace their previous image without having to use an entirely new URL. I tried using a cache busting query param and setting a timer to give Cloudinary time to process the upload before refreshing the component but then realized that next/image built in caching functionality is getting in the way.

In the following code, newPhoto is a boolean prop that I want to use to conditionally update the cache values by checking the current value of the actual src endpoint rather than just referring to it’s associated caches.

The noCloudinaryImage state is used to render a fallback image instead of alt text if the cloudinary src doesn’t point to an image.

The userId props is used to specify which Cloudinary image should be retrieved.

The cache buster was an attempt to only override the cache when the user uploads a new image instead of always overriding cache but it doesn’t work as a standalone.

import Link from "next/link";
import Image from "next/image";
import { useState, useEffect } from "react";

export default function ProfilePhoto(props) {
  const [noCloudinaryImage, setNoClouidnaryImage] = useState(false);
  const [cacheBuster, setCacheBuster] = useState('originalValue');
  const userId = props.userId;
  const newPhoto = props.newPhoto;
  const resetNewPhotoState = props.resetNewPhotoState;

  // The cloudinary url will be attempted again when the user successfully uploads a new image. To prevent an infinite loop in cases where image is uploaded but error is thrown during retrieval, I reset the newPhoto state back to initial. I don't reset to initial in the parent component because it would have potential to reset back to default before child has chance to register prop change. Resetting back to initial state is also good for case where user decides they don't like the uploaded photo and upload a new photo before component re-renders (and by default returns to initial state values).

  useEffect(() => {
    if (newPhoto) {
      setNoClouidnaryImage(false);
      resetNewPhotoState();
      setCacheBuster('updateCache');
    }
  }, [newPhoto, resetNewPhotoState]);

  //If the user isn't authenticated, the instance of this component that is used for the navbar will navigate user to create account page because there isn't a profile page associated with an unknown identity.
  let profilePagePath = props.profilePagePath
    ? `/profile/${props.profilePagePath}`
    : "/create-account-login";
  
  let photoSrc = `${process.env.NEXT_PUBLIC_CLOUDINARY_PROFILE_IMAGE_BASE_URL}${userId}?updateCache=${cacheBuster}`;

  return (
    <Link href={profilePagePath}>
      {!noCloudinaryImage && (
        <Image
          width={props.photoStyle.width}
          height={props.photoStyle.height}
          style={{ borderRadius: "50%" }}
          src={photoSrc}
          onError={() => {
            setNoClouidnaryImage(true);
          }}
          alt="Profile Photo"
        />
      )}
      {noCloudinaryImage && (
        <Image
          width={props.photoStyle.width}
          height={props.photoStyle.height}
          style={{ borderRadius: "50%" }}
          src={"/profile-photo/default-profile-photo.jpg"}
          alt="Profile Photo"
        />
      )}
    </Link>
  );
}

I attempted a cache busting query param. I attempted setting a timeout between upload and re-render. I did some research and found that next/image caching can be turned off but didn’t see a solution for just updating it’s value based on user interaction.

I expect that I may need to store completely new and unique URLs in my MongoDB user document and replace that url anytime the user updates their profile image. But I’d like avoid the overhead and code complexity of that approach if I can.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật