Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Check Which Image Loaded

In this lesson, you checked which of the hero’s thirteen files the browser actually chose, in the Network panel and with the image’s currentSrc, and saw Chrome keep a larger file when the window got narrower.

Key Points

  • The page can’t tell you. Apart from the square crop on small screens, the hero looks the same whichever file arrives, so your arithmetic is only a prediction. At 1280 pixels on a screen with a density of 1, the hero’s slot is 778 pixels wide. Current Chrome takes the AVIF source, and its smallest file that’s wide enough is plot-7-800.avif.
  • The Network panel lists every file the page downloads, with its name, its type and its size. It records while DevTools is open, and each reload starts the list afresh. Tick Disable cache first, so every reload fetches the files again, as a first visit would. Click Img to list images only. At 1280 pixels, one row appears for the hero: plot-7-800.avif, about 82 kB, the file your arithmetic picked.
  • currentSrc names the file the image is showing right now. In the Console, document is the page. document.querySelector(".hero img") finds the first element that matches the CSS selector .hero img, and its currentSrc is the address of the file that element shows. Reading it changes nothing on the page.
  • A narrowed window keeps the larger file. Drag device mode down to 768 pixels, and the slot shrinks to 446 pixels, so your arithmetic gives plot-7-480.avif. Will the browser download that file? No. No new row appears in the Network panel, and currentSrc still ends in plot-7-800.avif.
  • Chrome keeps the sharper file. The page already shows a file sharper than the slot needs. A smaller file would mean a second download for a softer photo, so Chrome keeps the one it has. That’s what it means for srcset to be a hint, not a command.
  • Load the page fresh to see what a screen gets. Reload at 768 pixels with the cache disabled, and the Network panel shows plot-7-480.avif, about 34 kB instead of 82, and currentSrc agrees.
  • Density works the same way. Device mode’s DPR menu, short for device pixel ratio, makes the page act as if the screen had that density. If it’s not in the toolbar, turn it on from the toolbar’s More options menu (Add device pixel ratio).
  • Changing the density needs a reload too. Set the DPR menu to 2, and nothing downloads, this time for a different reason. Chrome doesn’t fetch a new file when the density changes on an open page, so reload. After the reload, the slot needs 446 × 2 = 892 image pixels, and the Network panel shows plot-7-1200.avif.

The Rule

  • Load the page fresh, with the cache disabled, at each width and density you care about. The Network panel shows which file downloaded, and currentSrc shows which file the image displays.
  • Your arithmetic tells you what the browser is likely to pick. These two tell you what it did.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your hero photo now comes in thirteen files, and the browser picks one. Apart from the square crop on small screens, the page looks the same whichever it picks.

00:09 So how do you know it picked the file you planned? In this video, you’ll ask the browser itself. Start with what your arithmetic says. In device mode at 1280 pixels, on a screen with a density of 1, the hero’s slot is 778 pixels wide.

00:25 In current Chrome the AVIF source wins, and its smallest file that’s wide enough is the 800-pixel one.

00:34 In DevTools, click the Network tab. The Network panel lists every file the page downloads, with its name, its type and its size. It records while DevTools is open, and each reload starts the list afresh.

00:48 First, tick Disable cache. Then every reload fetches the files again, as a first visit would. And click Img, so the list shows images only. Now reload. One row appears for the hero: the 800-pixel AVIF file, about 82 kilobytes.

01:06 That’s the file your arithmetic picked.

01:10 The Network panel shows what was downloaded. To ask which file the image is showing right now, open the Console. Type a line that finds the hero’s image and reads one property.

01:22 querySelector, on the page’s document, takes a CSS selector, which is .hero img in this line, and gives back the first element it matches. currentSrc is the address of the file that element is showing.

01:35 Reading it changes nothing on the page. Press Enter. The address ends in plot-7-800.avif, the same file the Network panel listed.

01:48 Now narrow the window. Drag device mode down to 768 pixels. The slot shrinks to 446 pixels, so your arithmetic now gives the 480-pixel file. Your turn: pause the video and predict.

02:04 Will the browser download the 480-pixel file? No. The Network panel shows no new row. The up arrow and Enter run the line again, and the address still ends in plot-7-800.avif.

02:19 The page already shows a file sharper than this slot needs. A smaller file would mean a second download for a softer photo, so Chrome keeps the one it has.

02:28 That’s what it means for srcset to be a hint, not a command.

02:34 So to see what a 768-pixel screen gets, load the page fresh at that width. Reload. With the cache disabled, that’s a first visit. The Network panel shows the 480-pixel AVIF file, about 34 kilobytes instead of 82.

02:50 Run the line again, and currentSrc agrees.

02:54 Density works the same way. Device mode’s DPR menu, short for device pixel ratio, makes the page act as if the screen had that density. If it’s not in the toolbar, turn it on from the toolbar’s More options menu.

03:08 Set it to 2. Nothing downloads, and this time for a different reason. Chrome doesn’t fetch a new file when the density changes on an open page. Reload. Now the slot needs 446 times 2, which is 892 image pixels, and the Network panel shows the 1200-pixel file.

03:29 So, to check which file loaded, load the page fresh, with the cache disabled, at each width and density you care about. The Network panel shows which file downloaded, and currentSrc shows which file the image displays.

03:44 Your arithmetic tells you what the browser is likely to pick. These two tell you what it did.

03:51 Next, in Exercise: Serve the Garden’s Photos, you’ll give the events page’s photos their own widths and formats.

Become a Member to join the conversation.