feat(writeEarlyHints): add Link: rel:preload headers as fallback#1288
feat(writeEarlyHints): add Link: rel:preload headers as fallback#1288
Link: rel:preload headers as fallback#1288Conversation
📝 WalkthroughWalkthroughThe PR enhances Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
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. Comment |
commit: |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@src/utils/response.ts`:
- Around line 69-85: The fallback currently appends all hint headers from hints
into event.res.headers using event.res.headers.append, which can leak
undesirable headers (e.g., Set-Cookie); restrict the fallback to only propagate
Link headers: check for the "link" header key (case-insensitive) in hints and
append only those values (handling both string and string[] cases) via
event.res.headers.append, skipping any other hint names; keep the native early
hints branch (event.runtime?.node?.res?.writeEarlyHints) unchanged.
| // Use native early hints if available (Node.js) | ||
| if (event.runtime?.node?.res?.writeEarlyHints) { | ||
| return new Promise((resolve) => { | ||
| event.runtime?.node?.res?.writeEarlyHints(hints, () => resolve()); | ||
| }); | ||
| } | ||
|
|
||
| // Fallback: Set response headers for CDN support | ||
| for (const [name, value] of Object.entries(hints)) { | ||
| if (Array.isArray(value)) { | ||
| for (const v of value) { | ||
| event.res.headers.append(name, v); | ||
| } | ||
| } else { | ||
| event.res.headers.append(name, value); | ||
| } | ||
| } |
There was a problem hiding this comment.
Restrict fallback to Link to avoid changing response semantics.
Early hints can include headers that shouldn’t become part of the final response (e.g., Set-Cookie, Cache-Control). The fallback currently appends all hint headers, which can unintentionally alter responses in non-node runtimes. Since the objective is a Link: rel=preload fallback, consider filtering to Link only.
🔧 Suggested change
- for (const [name, value] of Object.entries(hints)) {
+ for (const [name, value] of Object.entries(hints)) {
+ if (name.toLowerCase() !== "link") {
+ continue;
+ }
if (Array.isArray(value)) {
for (const v of value) {
event.res.headers.append(name, v);
}
} else {
event.res.headers.append(name, value);
}
}🤖 Prompt for AI Agents
In `@src/utils/response.ts` around lines 69 - 85, The fallback currently appends
all hint headers from hints into event.res.headers using
event.res.headers.append, which can leak undesirable headers (e.g., Set-Cookie);
restrict the fallback to only propagate Link headers: check for the "link"
header key (case-insensitive) in hints and append only those values (handling
both string and string[] cases) via event.res.headers.append, skipping any other
hint names; keep the native early hints branch
(event.runtime?.node?.res?.writeEarlyHints) unchanged.
resolves #1279
Summary by CodeRabbit
New Features
Bug Fixes
Documentation
Tests
✏️ Tip: You can customize this high-level summary in your review settings.