0

I have an app which can switch between views on different subsets of a medium number of images (well below an acceptable amount to cache). I'm finding that when switching views, the browser sometimes asks the server if images are up-to-date before using the cached copy. The logic for doing so seems complex, and though I am interested in it, that isn't the focus of the question.

What ends up happening is the server sends back 304 (not modified), but the delay causes image pop-in. The effect is something like changing these cats:

changing cats

It's no problem for me to keep all of my images loaded. It seems that this used to be possible with display: none but is no longer. However, hidden seems to do the trick. Look at those smooth cats now!

smooth cats

So, what is the mid-2022 way to do this? Or am I being completely wrongheaded about the whole thing and should be thinking in an entirely different way about the problem?

EDIT: This question has been floated as containing much useful information, and while that's definitely true, I wanted to highlight how the linked sandbox is addressing the problem, namely:

// webpack/react style image import
import cat001 from "./images/cat001.png";
import cat002 from "./images/cat002.png";
...
const CATS = [ cat001, cat002, ... ];
...
function App() {
  return (
    <>
      {/* Keep all the cats loaded all the time! */}
      {CATS.map((cat) => (
        <img key={cat} src={cat} alt="a cat" hidden />
      ))}
    ...
    </>
  );
}

This seems like a nice JSX/TSX solution, whereas it feels less idiomatic to me to load up Images in a random array somewhere, so the question might be more specific to the react domain than I initially indicated.

thisisrandy
  • 2,660
  • 2
  • 12
  • 25
  • it really depends on how you're getting the new set of images ... I'd be inclined to do something like what done in https://stackoverflow.com/questions/3646036/preloading-images-with-javascript ... – Jaromanda X Jul 09 '22 at 01:34
  • That seems sensible. How about in the `import imgName from "imageFile"` webpack via react case that's used in the sandboxes I linked? – thisisrandy Jul 09 '22 at 02:18
  • I can see how you might say "the same way," but I guess I'm asking is there a preferred `TSX`-ey way like I used with ``. – thisisrandy Jul 09 '22 at 02:26
  • I know you tagged the question "reactjs" and "typescript" ... but there was no mention of it in the question, and I don't usually follow links in questions ... so, in short, I have no idea how to do it in reactjs since I don't use reactjs – Jaromanda X Jul 09 '22 at 02:28
  • Understood. I'll edit for clarity. – thisisrandy Jul 09 '22 at 02:29
  • note: `CATS` is just an array of url's ... so, I imagine the code I linked to applies – Jaromanda X Jul 09 '22 at 02:30
  • I am kind of coming around to that... Why not just add another constant that keeps them loaded, right? I'll see if anyone chimes in about ``, but this might be a dup after all. – thisisrandy Jul 09 '22 at 02:40
  • I don't think it's a dupe - you probably have to do something special since it's react – Jaromanda X Jul 09 '22 at 02:49

0 Answers0