fix: respect compress settings in dev server#4159
fix: respect compress settings in dev server#4159hellozyemlya wants to merge 1 commit intonitrojs:mainfrom
Conversation
|
@hellozyemlya is attempting to deploy a commit to the Nitro Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughThe pull request updates Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/dev/app.ts (1)
113-113: Remove the explanatory inline comment.Line 113 adds a comment that explains straightforward code behavior; this repo guideline asks to avoid such comments unless requested.
As per coding guidelines, "Do not add comments explaining what the line does unless prompted".
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/dev/app.ts` at line 113, Remove the explanatory inline comment "// Determine compression settings" that was added at the start of the compression configuration block; locate the comment near the compression-related code (the section dealing with compression settings/variables in app.ts) and delete the comment while leaving the surrounding code and logic (the compression configuration variables/functions) unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/dev/app.ts`:
- Around line 140-145: Replace the naive substring checks for brotli/gzip in the
compression branch with an Accept-Encoding parser that honors q-values: add an
acceptsEncoding(header, encoding) helper (as proposed) and use it instead of
acceptEncoding.includes("br") and acceptEncoding.includes("gzip") in the logic
around stream.pipe(createBrotliCompress()) and the gzip branch; the helper
should split on commas, parse each item into name and params, treat "*" as
matching, parse any "q=" param to a number, and only return true if a matching
encoding has no q or q > 0.0 so clients that send q=0 are correctly respected.
---
Nitpick comments:
In `@src/dev/app.ts`:
- Line 113: Remove the explanatory inline comment "// Determine compression
settings" that was added at the start of the compression configuration block;
locate the comment near the compression-related code (the section dealing with
compression settings/variables in app.ts) and delete the comment while leaving
the surrounding code and logic (the compression configuration
variables/functions) unchanged.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
| if (compressOpts.brotli && acceptEncoding.includes("br")) { | ||
| event.res.headers.set("Content-Encoding", "br"); | ||
| event.res.headers.delete("Content-Length"); | ||
| event.res.headers.set("Vary", "Accept-Encoding"); | ||
| return stream.pipe(createBrotliCompress()); | ||
| } else if (acceptEncoding.includes("gzip")) { | ||
| } else if (compressOpts.gzip && acceptEncoding.includes("gzip")) { |
There was a problem hiding this comment.
Honor Accept-Encoding quality values (q) instead of substring matching.
Line 140 and Line 145 use .includes("br") / .includes("gzip"), which will incorrectly compress even when the client sends br;q=0 or gzip;q=0.
💡 Proposed fix
- const acceptEncoding = event.req.headers.get("accept-encoding") || "";
- if (compressOpts.brotli && acceptEncoding.includes("br")) {
+ const acceptEncoding = event.req.headers.get("accept-encoding") || "";
+ if (compressOpts.brotli && acceptsEncoding(acceptEncoding, "br")) {
event.res.headers.set("Content-Encoding", "br");
event.res.headers.delete("Content-Length");
event.res.headers.set("Vary", "Accept-Encoding");
return stream.pipe(createBrotliCompress());
- } else if (compressOpts.gzip && acceptEncoding.includes("gzip")) {
+ } else if (compressOpts.gzip && acceptsEncoding(acceptEncoding, "gzip")) {
event.res.headers.set("Content-Encoding", "gzip");
event.res.headers.delete("Content-Length");
event.res.headers.set("Vary", "Accept-Encoding");
return stream.pipe(createGzip());
}function acceptsEncoding(header: string, encoding: "br" | "gzip") {
return header.split(",").some((item) => {
const [name, ...params] = item.trim().toLowerCase().split(";");
if (name !== encoding && name !== "*") {
return false;
}
const q = params
.map((p) => p.trim())
.find((p) => p.startsWith("q="))
?.slice(2);
if (!q) {
return true;
}
const quality = Number.parseFloat(q);
return Number.isFinite(quality) && quality > 0;
});
}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/dev/app.ts` around lines 140 - 145, Replace the naive substring checks
for brotli/gzip in the compression branch with an Accept-Encoding parser that
honors q-values: add an acceptsEncoding(header, encoding) helper (as proposed)
and use it instead of acceptEncoding.includes("br") and
acceptEncoding.includes("gzip") in the logic around
stream.pipe(createBrotliCompress()) and the gzip branch; the helper should split
on commas, parse each item into name and params, treat "*" as matching, parse
any "q=" param to a number, and only return true if a matching encoding has no q
or q > 0.0 so clients that send q=0 are correctly respected.
🔗 Linked issue
❓ Type of change
📚 Description
Implements handling of "compressPublicAssets" option from nitro.config.ts in development mode.
📝 Checklist