Skip to content

BUG: "Failed to revalidate cached remote image"-Warning on valid 304 Not Modified responses #15920

@jstask82

Description

@jstask82

Astro Info

Astro                    v6.0.4
Vite                     v7.3.1
Node                     v22.22.0
System                   macOS (arm64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             @astrojs/svelte (v8.0.0)
                         @astrojs/sitemap (v3.7.1)
                         redirects
                         @playform/inline (v0.1.2)
                         @astrojs/mdx

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

Description:

When Astro tries to revalidate a cached remote image, the server might correctly respond with a 304 Not Modified. However, Astro interprets this 304 status as a redirect and logs a waring:

[WARN] An error was encountered while revalidating a cached remote asset. Proceeding with stale cache. Error: Failed to revalidate cached remote image https://example.com/remoteimg.jpg. The request was redirected.

Where it happens:

In withastro/astro/packages/astro/src/assets/build/remote.ts (inside the revalidateRemoteImage function):

	if (res.status >= 300 && res.status < 400) {
		throw new Error(`Failed to revalidate cached remote image ${src}. The request was redirected.`);
	}

if (!res.ok && res.status !== 304) {
  ...
}

Even if the if-statement is fixed to exclude 304 (e.g., && res.status !== 304), the function continues to read the response body:

const data = Buffer.from(await res.arrayBuffer());

Since a 304 response has no body, data will be an empty buffer. Returning this empty buffer seems unintended. The function should probably return early or signal the caller to keep using the existing cached image.

Steps to Reproduce:

Use with a remote URL that provides Cache-Control headers (e.g., via Cloudflare Images).

Build the project once to cache the image.

Wait for the cache TTL to expire.

Build again. The server responds with 304, triggering the [WARN] ... The request was redirected. in the console.

What's the expected result?

A 304 Not Modified should be treated as a successful revalidation, refreshing the TTL of the cached image without logging a "The request was redirected."-Warning

Link to Minimal Reproducible Example

NA

Participation

  • I am willing to submit a pull request for this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    - P3: minor bugAn edge case that only affects very specific usage (priority)needs triageIssue needs to be triagedpkg: astroRelated to the core `astro` package (scope)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions