Skip to content

Fix: #1255 - 304 Not Modified status code being overwritten to 200 on ETag matches and omit body in CloudFront converter. - #1256

Open
jarodsim wants to merge 11 commits into
opennextjs:mainfrom
jarodsim:fix/etag-304-empty-body
Open

jarodsim wants to merge 11 commits into
opennextjs:mainfrom
jarodsim:fix/etag-304-empty-body

Conversation

@jarodsim

Copy link
Copy Markdown

Fixes #1255

When a request is made with a matching If-None-Match header:

  1. Next.js (send-payload.js) detects the match, sets res.statusCode = 304, and ends the stream with 0 chunks.
  2. In next/dist/server/base-server.js (pipeImpl line 1119), Next.js resets res.statusCode = originalStatus (which is undefined).
  3. In traditional Node.js with TCP sockets, this post-response reset is harmless because 304 Not Modified has already been sent over the wire. However, in OpenNext's non-streaming/buffered mode (e.g., Lambda@Edge), convertRes reads res.statusCode only after processRequest resolves, defaulting to 200.
  4. As a result, CloudFront / Lambda@Edge returned status: 200 with an empty body "", causing browsers to render a blank page.
  5. In addition, CloudFront Lambda@Edge specifications dictate that responses with status 304 or 204 must not contain body or bodyEncoding (doing so triggers CloudFront 502 errors).

Changes

  • packages/open-next/src/http/openNextResponse.ts: Encapsulate statusCode with getter/setter to prevent mutation once headersSent === true, matching standard Node.js HTTP response immutability semantics.
  • packages/open-next/src/overrides/converters/aws-cloudfront.ts:
    • Omit body and bodyEncoding for NULL_BODY_STATUSES (304, 204, etc.) per CloudFront requirements.
    • Set statusDescription using http.STATUS_CODES[result.statusCode] instead of hardcoded "OK".
  • Tests:
    • Added unit tests in openNextResponse.test.ts to assert statusCode preservation after res.end() and flushHeaders().
    • Added unit tests in aws-cloudfront.test.ts for 304, 204, and 200 responses.

…nt converter (opennextjs#1255)

- Protect statusCode in OpenNextNodeResponse from being overwritten after headersSent (fixes Next.js resetting statusCode to originalStatus after sendRenderResult)
- Omit body and bodyEncoding for NULL_BODY_STATUSES (304, 204) in aws-cloudfront converter to comply with CloudFront Lambda@Edge specifications
- Set proper statusDescription using http.STATUS_CODES instead of hardcoding "OK"
- Add unit tests for OpenNextNodeResponse and aws-cloudfront converter

Fixes opennextjs#1255
@changeset-bot

changeset-bot Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 837b6a1

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

This PR includes changesets to release 1 package
Name Type
@opennextjs/aws 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

Comment thread packages/open-next/src/http/openNextResponse.ts Outdated
@jarodsim
jarodsim requested a review from conico974 October 15, 2026 00:37
@pkg-pr-new

pkg-pr-new Bot commented Oct 15, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@opennextjs/aws@1256

commit: 77c014c

This branch has not been deployed

No deployments
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.

[Bug]: Requests with matching If-None-Match header return HTTP 200 with empty body instead of 304 in non-streaming mode (Lambda@Edge/CloudFront)

2 participants