Skip to content

fix(media): cache remote image dimensions and set timeout - #1032

Open
faisalahammad wants to merge 1 commit into
litespeedtech:devfrom
faisalahammad:fix/1020-remote-img-dimensions-ttfb
Open

faisalahammad wants to merge 1 commit into
litespeedtech:devfrom
faisalahammad:fix/1020-remote-img-dimensions-ttfb

Conversation

@faisalahammad

@faisalahammad faisalahammad commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Summary

When "Add Missing Sizes" is enabled, _detect_dimensions() makes synchronous HTTP requests for remote images without caching or timeouts. This change implements WordPress transient caching, range requests, and safe HTTP timeouts using wp_safe_remote_get().

Fixes #1020

Changes

src/media.cls.php

Before:

try {
    $sizes = getimagesize( $src );
} catch ( \Exception $e ) {
    return false;
}

After:

$cache_key = "lsc_img_sz_" . md5( $src );
$cached    = get_transient( $cache_key );
if ( false !== $cached ) {
    return "failed" === $cached ? false : $cached;
}

$response = wp_safe_remote_get(
    $src,
    [
        "timeout"             => 3,
        "limit_response_size" => 65536,
        "user-agent"          => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) LiteSpeedCache/Dimensions",
        "headers"             => [
            "Range" => "bytes=0-32768",
        ],
    ]
);

Why: Remote image dimension fetches were executing un-cached serial HTTP GETs during page finalization. Caching detected dimensions in transients for 7 days (and failure states for 1 hour) with a 3-second timeout and 32KB Range header resolves TTFB delays and prevents page hangs.

Testing

Test 1: Remote image dimension caching

  1. Enable "Add Missing Sizes" under Media Settings.
  2. Load a page with remote images missing width/height attributes.
    Result: Width and height attributes are added and subsequent page views load dimensions instantly from transient cache.

Test 2: Timeout and failure protection

  1. Load a page containing an invalid remote image URL.
    Result: Request times out after 3 seconds instead of hanging, and failure status is cached for 1 hour.

@faisalahammad
faisalahammad changed the base branch from master to dev August 7, 2026 19:35
@faisalahammad
faisalahammad force-pushed the fix/1020-remote-img-dimensions-ttfb branch from ff83c05 to c002053 Compare August 7, 2026 19:36
Adds WP transient caching and safe HTTP request handling with range headers
to remote image dimension detection in _detect_dimensions(). This prevents
high TTFB spikes and page hangs when fetching remote missing sizes.

Fixes litespeedtech#1020
@faisalahammad
faisalahammad force-pushed the fix/1020-remote-img-dimensions-ttfb branch from c002053 to 2d0e060 Compare August 7, 2026 19:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Image Lazy Load + "Add Missing Sizes" + remote images: serial, uncached, blocking requests causing crazy TTFB

1 participant