2

I have a React application in which after clicking on the buttons I change the categories and each category has its own layout with text and images.

The problem is that I conditionally render this layout using switch/case and every time the content is redrawn, the images that were already downloaded once are downloaded again.

I've tried using images created by webpack, I've tried using images from the public directory and I've also tried using Promise for asynchronous loading... but it doesn't seem to matter. The initiator in all cases is react-dom.development.js

I can use ServiceWorker for faster loading, but I would like to avoid re-downloading the images.

I use Hookstate for state management.

Here is a variant with asynchronous image loading:

Main component, where I conditionally render individual layouts (Content.jsx)

import { suspend, useHookstate } from "@hookstate/core";
import { imagesData } from "../state/images";
import { generalData } from "../state/store";

const Content = () => {

    const category = useHookstate(generalData);
    const images = useHookstate(imagesData);

    switch (category.selected.value) {
        case "category1":
            return (
                <div className="flex flex-col items-center md:px-10">
                    <img src={suspend(images) ?? images.img1.value} className="max-w-[120px] sm:max-w-[140px] w-full h-full rounded-3xl" alt="..." />
                    <p>Text 1</p>
                </div>
            )
        case "category2":
            return (
                <div className="flex flex-col items-center md:px-10">
                    <h1>Heading...</h1>
                    <img src={suspend(images) ?? images.img2.value} className="max-w-[120px] sm:max-w-[140px] w-full h-full rounded-3xl" alt="..." />
                    <img src={suspend(images) ?? images.img3.value} className="max-w-[120px] sm:max-w-[140px] w-full h-full rounded-3xl" alt="..." />
                    <p>Text 2</p>
                    <img src={suspend(images) ?? images.img4.value} className="max-w-[120px] sm:max-w-[140px] w-full h-full rounded-3xl" alt="..." />
                    <p>Text 3</p>
                </div>
            )
        case "category3":
            return (
                <div className="flex flex-col items-center md:px-10">
                    <p>Text 4</p>
                    <img src={suspend(images) ?? images.img5.value} className="max-w-[120px] sm:max-w-[140px] w-full h-full rounded-3xl" alt="..." />
                    <p>Text 5</p>
                    <img src={suspend(images) ?? images.img6.value} className="max-w-[120px] sm:max-w-[140px] w-full h-full rounded-3xl" alt="..." />
                </div>
            )

        default: return (
            <div className="flex flex-col items-center md:px-10">
                <p>Empty..</p>
            </div>
        )
    }
}

export default Content;

Global storage of images (images.jsx)

import { hookstate } from "@hookstate/core";

const getImages = () => new Promise((res) => {
    setTimeout(() => {
        res({
            img1: "/assets/images/img1.jpg",
            img2: "/assets/images/img2.jpg",
            img3: "/assets/images/img3.jpg",
            img4: "/assets/images/img4.jpg",
            img5: "/assets/images/img5.jpg",
            img6: "/assets/images/img6.jpg"
        });
    }, 100);
});

const imagesData = hookstate(getImages);

export {
    imagesData
}

I set individual categories in another component for example by onClick={() => general.selected.set("category3")}

The result I see in devtools: DevTools - Network - Repeated image downloads

Can you advise me how to solve this problem? Thank you in advance.

Martin Wsc
  • 21
  • 2
  • 1
    `the images that were already downloaded once are downloaded again` maybe fix the image headers to allow caching? Is this actually a real problem, or only when you have dev tools open with "disable cache" selected? Is it a real problem in production, or only in local development? This really doesn't have anything to do with react. Any effort trying to "solve" the problem with react is, likely (but not 100% certainly), misguided – Adam Jenkins Mar 19 '23 at 20:11
  • You are right, if I remove "disable cache", the images load quickly. Thank you. – Martin Wsc Mar 21 '23 at 12:07

0 Answers0