Skip to content

fix(assets): exclude 304 Not Modified from redirect check in revalidateRemoteImage#15940

Open
mango766 wants to merge 1 commit intowithastro:mainfrom
mango766:fix/revalidate-remote-image-304
Open

fix(assets): exclude 304 Not Modified from redirect check in revalidateRemoteImage#15940
mango766 wants to merge 1 commit intowithastro:mainfrom
mango766:fix/revalidate-remote-image-304

Conversation

@mango766
Copy link

Summary

Fixes #15920

When revalidating cached remote images, revalidateRemoteImage() uses fetch with redirect: 'manual' and then checks the status code to detect redirects:

if (res.status >= 300 && res.status < 400) {
    throw new Error(`... The request was redirected.`);
}

The problem is that HTTP 304 (Not Modified) falls in the [300, 400) range, so it gets caught by this guard and throws a "redirected" error before the correct 304 handling logic on the subsequent lines can ever run. This causes a spurious warning during builds:

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

Changes

  • Added && res.status !== 304 to the redirect status check in revalidateRemoteImage(), so 304 responses pass through to the existing correct handling path that refreshes the cache TTL and preserves etag/lastModified values.
  • Added a unit test verifying 304 does not throw and that actual redirects (e.g. 301) still throw as expected.
  • Added a changeset.

Why this works

The downstream code already handles 304 correctly:

  • Line 61: if (!res.ok && res.status !== 304) excludes 304 from the error path
  • Lines 75-80: Treats 304 as cacheable by mapping it to status 200 for TTL calculation
  • Lines 87-89: Preserves stored etag/lastModified for 304 responses
  • The caller in generate.ts (line 219) checks if (revalidatedData.data.length) — an empty buffer (304 case) means "use the cached file, just freshen the metadata"

The one-line fix simply lets 304 responses reach this existing logic.

Test plan

  • Verify the new unit test passes: node --test packages/astro/test/units/assets/remote.test.js
  • Verify existing image tests still pass: pnpm --filter astro run test:match "core-image"
  • Manual: build a project with remote <Image /> where the origin returns 304 on revalidation — the warning should no longer appear

…teRemoteImage

The redirect guard `res.status >= 300 && res.status < 400` incorrectly
caught HTTP 304 Not Modified responses, throwing a "request was redirected"
error before the existing 304 handling logic could run. This caused a
spurious warning during builds when revalidating cached remote images
whose TTL had expired and the origin responded with 304.

The fix adds `&& res.status !== 304` so 304 responses flow through to
the existing correct handling path (lines 61-90) which already properly
refreshes the cache TTL and preserves etag/lastModified values.

Closes withastro#15920

Co-Authored-By: Claude (claude-opus-4-6) <noreply@anthropic.com>
@changeset-bot
Copy link

changeset-bot bot commented Mar 16, 2026

🦋 Changeset detected

Latest commit: 065fbcf

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Mar 16, 2026
@codspeed-hq
Copy link

codspeed-hq bot commented Mar 16, 2026

Merging this PR will not alter performance

✅ 18 untouched benchmarks


Comparing mango766:fix/revalidate-remote-image-304 (065fbcf) with main (325901e)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (3c157f6) during the generation of this report, so 325901e was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: astro Related to the core `astro` package (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant