Skip to content

fix: writeEarlyHints resolves immediately instead of hanging (#1383)#1415

Open
rafaumeu wants to merge 1 commit into
h3js:mainfrom
rafaumeu:fix/write-early-hints-resolve-1383
Open

fix: writeEarlyHints resolves immediately instead of hanging (#1383)#1415
rafaumeu wants to merge 1 commit into
h3js:mainfrom
rafaumeu:fix/write-early-hints-resolve-1383

Conversation

@rafaumeu

@rafaumeu rafaumeu commented Jun 17, 2026

Copy link
Copy Markdown

Fixes #1383

The callback for Node.js writeEarlyHints is optional and may not
be invoked in all cases. By resolving the promise immediately after
calling writeEarlyHints, we avoid hanging requests while still
sending early hints to the client.

Changes:

  • Remove unused Promise wrapper that could hang
  • Resolve promise immediately after calling writeEarlyHints
  • Still sends early hints to the client via native Node.js API

Testing:

  • Existing writeEarlyHints tests pass (web fallback)
  • Manual test with reproduction case works (no hanging)

Summary by CodeRabbit

  • Tests

    • Added test script for early hints functionality validation
  • Refactor

    • Optimized early hints execution for improved efficiency

Fixes h3js#1383

The callback for Node.js writeEarlyHints is optional and may not
be invoked in all cases. By resolving the promise immediately after
calling writeEarlyHints, we avoid hanging requests while still
sending early hints to the client.
@rafaumeu rafaumeu requested a review from pi0 as a code owner June 17, 2026 15:14
@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The Node.js fast path in writeEarlyHints is changed to call res.writeEarlyHints(hints) directly and return Promise.resolve() immediately, replacing the previous new Promise wrapper that relied on an optional callback that never fired. A standalone ESM test script is added to manually exercise this path.

Changes

writeEarlyHints Node.js fix and manual test

Layer / File(s) Summary
Node.js early-hints promise fix and test
src/utils/response.ts, test-write-early-hints.mjs
Removes the new Promise + callback pattern from the Node.js early-hints branch; calls writeEarlyHints(hints) directly and returns Promise.resolve(). Adds a standalone ESM script that starts an H3 server, calls writeEarlyHints with a link preload header, and returns elapsed time.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

Possibly related PRs

  • h3js/h3#1288: Modifies the same writeEarlyHints utility in src/utils/response.ts, adjusting fallback behavior and hints typing in an earlier pass at the same function.

Suggested reviewers

  • pi0

Poem

🐇 A promise that hung, now finally flies,
No callback to wait for beneath cloudy skies.
Just resolve() at once — the hints are all sent,
No more dangling requests, no handler hell-bent.
Hippity-hop, the early hints flow free! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: resolving a hanging issue with writeEarlyHints in the Node.js runtime, which is the core change in this PR.
Linked Issues check ✅ Passed The code changes directly address issue #1383 by fixing the promise resolution issue in writeEarlyHints that was causing request handlers to hang indefinitely.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the writeEarlyHints hanging issue: the fix modifies the core function and a test file was added to validate the resolution behavior.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@test-write-early-hints.mjs`:
- Around line 1-6: The import path in the H3 module import statement is
incorrect and the test script is not using the patched writeEarlyHints helper
function. Fix the import statement to use the correct path "./src/index.ts"
instead of "../src/index.ts", and replace the direct call to
event.node?.writeEarlyHints? with a call to the patched writeEarlyHints(event,
hints) helper function to properly validate the PR's fix.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c4b1f9aa-8c35-4745-8d55-8a60953a1c42

📥 Commits

Reviewing files that changed from the base of the PR and between edb53fe and 0ab63b2.

📒 Files selected for processing (2)
  • src/utils/response.ts
  • test-write-early-hints.mjs

Comment on lines +1 to +6
import { H3 } from "../src/index.ts";

const app = new H3().get("/", async (event) => {
const start = Date.now();
await event.node?.writeEarlyHints?.({
link: "</style.css>; rel=preload; as=style",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Manual reproduction script misses the fixed API path (and likely has a broken import).

Line 1 should import from the repo root path (./src/index.ts), and Line 5 should call the patched writeEarlyHints(event, hints) helper to actually validate this PR’s fix.

Proposed patch
-import { H3 } from "../src/index.ts";
+import { H3, writeEarlyHints } from "./src/index.ts";
@@
-  await event.node?.writeEarlyHints?.({
-    link: "</style.css>; rel=preload; as=style",
-  });
+  await writeEarlyHints(event, {
+    Link: "</style.css>; rel=preload; as=style",
+  });
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test-write-early-hints.mjs` around lines 1 - 6, The import path in the H3
module import statement is incorrect and the test script is not using the
patched writeEarlyHints helper function. Fix the import statement to use the
correct path "./src/index.ts" instead of "../src/index.ts", and replace the
direct call to event.node?.writeEarlyHints? with a call to the patched
writeEarlyHints(event, hints) helper function to properly validate the PR's fix.

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.

writeEarlyHints never resolves with node runtime

1 participant