Skip to content

Conversation

iDVB
Copy link

@iDVB iDVB commented Jun 6, 2025

Moved to new PR against a non-main branch.
Original PR here: #886

Fixes #885

Copy link

changeset-bot bot commented Jun 6, 2025

🦋 Changeset detected

Latest commit: fdc1f4b

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

This PR includes changesets to release 3 packages
Name Type
@opennextjs/aws Major
app-pages-router Patch
app-router Patch

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

Copy link

pkg-pr-new bot commented Jun 6, 2025

Open in StackBlitz

pnpm add https://pkg.pr.new/@opennextjs/aws@893

commit: fdc1f4b

@vicb vicb changed the title Fix 885 feat(image-adapter): improve error handling and status codes Jun 6, 2025
Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a changeset? (pnpm changeset)

Thanks!

@iDVB
Copy link
Author

iDVB commented Jun 12, 2025

Just an update. This seems to be working the dream in our prod environment so far.

Copy link
Contributor

@conico974 conico974 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to be this nitpicky on this one, but that should be the last round of change

// Main handler logic with clearer error paths
try {
// Case 1: remote image URL => download the image from the URL
// EXTERNAL IMAGE HANDLING
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// EXTERNAL IMAGE HANDLING
// remote image URL => download the image from the URL

// Download image from S3
// note: S3 expects keys without leading `/`

// INTERNAL IMAGE HANDLING (S3)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// INTERNAL IMAGE HANDLING (S3)
// local image => download the image from the provided ImageLoader (default is S3)

}
const message = "Empty response body from the S3 request.";
const clientError = new IgnorableError(message, 400);
error("Empty response from S3", clientError);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
error("Empty response from S3", clientError);
error("Empty response from ImageLoader", clientError);


// Special handling for S3 ListBucket permission errors
// AWS SDK v3 nests error details deeply within the error object
const isListBucketError =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the S3 related stuff should not be here. https://github.com/opennextjs/opennextjs-aws/blob/main/packages/open-next/src/overrides/imageLoader/s3.ts
This is the ImageLoader and where you should handle S3 related error, not here.
Here we should only handle our error, or basic error


// Still include error details in headers for debugging only
const errorMessage = isListBucketError ? "Access denied" : message;
res.setHeader("x-nextjs-internal-error", errorMessage);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this one propagates to the end user ?

return buildSuccessResponse(result, options?.streamCreator, etag);
} catch (e: any) {
error("Failed to optimize image", e);
// All image-related errors should be treated as client errors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not true, if something fails on the server it is not client errors

@iDVB
Copy link
Author

iDVB commented Sep 8, 2025

Did this ever make it into the build? We see this build is broken now where it was working before...
pnpm dlx https://pkg.pr.new/@opennextjs/aws@6f0044b build

There was a fix in the image server function that properly handled trailing querystring which now seem to be broken in this build now. (was working in this build less than a week ago)

# PROD: WORKS
/_next/image?w=384&q=75&url=https://images.ctfassets.net/s5qqrp96y1p4/7wHwJSvxFWpIYdm56bOdSk/1208a00c3e9ed4ad32102e84d494860c/IDX_Article_-_Cultural_Forces_5_IDX_HEADER__1_.png

# PROD: WORKS
/_next/image?w=384&q=75&url=https://images.ctfassets.net/s5qqrp96y1p4/7wHwJSvxFWpIYdm56bOdSk/1208a00c3e9ed4ad32102e84d494860c/IDX_Article_-_Cultural_Forces_5_IDX_HEADER__1_.png

---

# DEV: WORKS
/_next/image?w=384&q=75&url=https://images.ctfassets.net/s5qqrp96y1p4/7wHwJSvxFWpIYdm56bOdSk/1208a00c3e9ed4ad32102e84d494860c/IDX_Article_-_Cultural_Forces_5_IDX_HEADER__1_.png

# DEV: NOT WORK
/_next/image?w=384&q=75&url=https%3A%2F%2Fimages.ctfassets.net%2Fs5qqrp96y1p4%2F7wHwJSvxFWpIYdm56bOdSk%2F1208a00c3e9ed4ad32102e84d494860c%2FIDX_Article_-_Cultural_Forces_5_IDX_HEADER__1_.png

@conico974
Copy link
Contributor

@iDVB I rebased and pushed some changes that i requested. Could you test it please ?

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 Optimization Adapter Returns Improper HTTP Status Codes
4 participants