slop audit
Blog post 83 / Performance and accessibility

Broken images on a website: find and fix them

Find and fix broken images on a website by tracing the selected image URL, checking the deployed response, and testing mobile and desktop in Playwright.

By Slop Audit · 6 min read

To fix broken images on a website, reproduce the problem on the deployed page and find the exact image URL the browser selected. If that URL returns an error or HTML, fix the asset path or server response. If the image decodes and the page still shows a blank space, inspect the layout. For a mobile-only failure, check the responsive source selected on mobile before editing the desktop image.

How to find broken images on a website

  1. Open the failing route in a fresh, signed-out browser session if the page is public. Set the failing viewport before loading it.
  2. Open DevTools, switch to Network, enable Disable cache, then reload. Scroll to or open the section that should display the image.
  3. Select the <img> in Elements and read $0.currentSrc in the Console. Find that URL in Network and inspect Status, Headers, and Preview. Use Initiator to trace the request back to markup, CSS, or a script.
  4. Record the page URL, viewport, selected asset URL, and observed result. If the image uses a temporary signed URL, leave its token out of the bug report.

The browser can select a different URL from the one in src when the page uses srcset or <picture>. The selected URL is available through HTMLImageElement.currentSrc. It can be present even when the image failed to load. The Chrome DevTools Network reference explains how to inspect requests and their initiators.

Fix the cause indicated by the request

404 or 410. Compare the requested path, including capitalization and extension, with the published files. A reference like images/team.webp on /products/widget/ resolves under /products/widget/ unless the document declares a different base URL. Use an asset URL generated for your deployment's base path, and make sure the file is present in the build output. If the URL contains a build hash, check whether cached HTML still points to a previous release's file. Publish new assets before HTML references them and retain old hashed files while cached HTML can still reference them.

403 or a redirect to a login page. Check the actual page request and the host's access rules. An image that opens in your signed-in browser can still fail for a visitor. Publish public page assets through your intended public delivery path. For private images, repair the authorized delivery flow and test the expected access in both signed-in and signed-out sessions.

200 with HTML. The asset URL may hit a single-page app fallback or a friendly error page. Check Preview for the homepage or an error template and check the final URL for redirects. Repair the missing file or asset route. Configure the host so missing image paths return an asset error instead of your app shell. The same routing mistake can make JavaScript files return HTML.

200 with image bytes but no visible image. Check whether the browser can decode the response. Verify that the deployed file matches the expected format and that the image service returns a complete, supported variant. If it decodes, inspect the Styles and Computed panels for a hidden ancestor, zero-size container, clipping, or an overlay. For a CSS background, inspect the winning background-image value and the size of the element. It will not appear in document.images.

No request. Check that the component rendered an image URL. A custom lazy loader may leave it in data-src if its callback never runs. For native loading="lazy", bring the image into view and check whether a request starts. Inspect the Console if DevTools reports a browser policy block. The HTML Standard's image section describes how browsers select and load embedded images.

For a public URL with no credentials, a GET request can confirm whether the asset path returns the expected response outside the browser:

curl -sS -L --max-redirs 5 -D - -o /dev/null \
  'https://example.com/images/team-480.webp'

Read the final response headers after any redirects. Keep the browser result as your reference for cookie-dependent responses, browser policy errors, and decode failures.

Debug an image that only breaks on mobile

Suppose the desktop version of a pricing page loads team-1200.webp, while a phone selects team-480.webp and gets a 404. Inspect the <picture> source or srcset entry that names the smaller file, then check the build output for that exact path. Repair the variant generation or update the markup to point to the correct published asset.

Reload in separate, fresh mobile and desktop browser contexts to avoid reusing an image selected during an earlier resize. Test another pixel density if the failing entry depends on a density descriptor. A working src fallback gives the browser another source-selection path where applicable. A failed selected candidate still needs repair. See the website image optimization checklist for responsive markup examples and the mobile website testing guide for choosing representative breakpoints.

Add a Playwright image loading test

Use a stable locator for each required image on a representative route. Bring below-the-fold images into view, then check both the element's visibility and whether its selected asset decodes. A successful page response only confirms that the document loaded. Even an image's complete property can be true after a failed load. See the HTML Standard's definition of complete.

This example assumes Playwright Test has a baseURL pointing to the deployment and the route includes a lazy-loaded <img data-testid="team-photo">.

import { expect, test } from "@playwright/test";

const viewports = [
  { name: "mobile", width: 375, height: 812 },
  { name: "desktop", width: 1280, height: 900 },
];

for (const { name, width, height } of viewports) {
  test(`team photo loads on ${name}`, async ({ page }) => {
    await page.setViewportSize({ width, height });
    await page.goto("/about", { waitUntil: "domcontentloaded" });

    const photo = page.getByTestId("team-photo");
    await expect(photo).toBeVisible();
    await photo.scrollIntoViewIfNeeded();

    await expect.poll(async () => {
      return photo.evaluate(async (element) => {
        if (!(element instanceof HTMLImageElement)) {
          throw new Error("Expected an img element for team-photo");
        }

        const url = element.currentSrc;
        if (!url || !element.complete) return "waiting for selected image";

        try {
          await element.decode();
          return "decoded";
        } catch {
          return `could not decode ${url}`;
        }
      });
    }, { timeout: 10_000 }).toBe("decoded");
  });
}

Playwright's polling assertions retry while the page selects or loads the image. The image decode API rejects when the request fails or the image data cannot be decoded. If your component substitutes a valid generic placeholder on failure, also assert the expected selected filename or path after the component settles. When an overlay or cropping bug caused the regression, include a screenshot check of the affected component. Add rerun results and the repaired asset URL to your website regression testing record.

Recheck the deployed page after the fix

Once the affected images pass the browser test, run Slop Audit on the repaired public URL. Review same-site request failure evidence from the browser run and check for unusually large rendered images. Keep the route-specific test for HTTP error responses, image decoding, and images that only load after scrolling or interaction. Rescan after the next asset deployment to compare the page-level findings.