Skip to content

fix: respect compress settings in dev server#4159

Open
hellozyemlya wants to merge 1 commit intonitrojs:mainfrom
hellozyemlya:respect-compress
Open

fix: respect compress settings in dev server#4159
hellozyemlya wants to merge 1 commit intonitrojs:mainfrom
hellozyemlya:respect-compress

Conversation

@hellozyemlya
Copy link
Copy Markdown

🔗 Linked issue

❓ Type of change

  • 📖 Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

Implements handling of "compressPublicAssets" option from nitro.config.ts in development mode.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@hellozyemlya hellozyemlya requested a review from pi0 as a code owner March 28, 2026 19:34
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 28, 2026

@hellozyemlya is attempting to deploy a commit to the Nitro Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 28, 2026

📝 Walkthrough

Walkthrough

The pull request updates NitroDevApp to pass compression options to the serveStaticDir function, enabling compression behavior for public assets to be controlled by Nitro configuration. The serveStaticDir function now accepts compress options and gates encoding selection based on both client Accept-Encoding headers and configured compression settings.

Changes

Cohort / File(s) Summary
Compression Options Configuration
src/dev/app.ts
Updated NitroDevApp to pass compressPublicAssets option to serveStaticDir; expanded serveStaticDir parameter type to accept compress?: boolean | CompressOptions; added logic to compute compression options and gate gzip/brotli encoding selection based on both client Accept-Encoding headers and compression configuration.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description check ✅ Passed The description is related to the changeset, detailing that it implements handling of compressPublicAssets option from nitro.config.ts in development mode.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Title check ✅ Passed The title follows conventional commits format with 'fix:' prefix and clearly describes the main change: respecting compression settings in the development server.

✏️ 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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

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

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 02e8ee58-0c7c-4f79-9654-691ee69c21df

📥 Commits

Reviewing files that changed from the base of the PR and between 5cd5cb2 and 145eaea.

📒 Files selected for processing (1)
  • src/dev/app.ts

Comment on lines +140 to +145
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")) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

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.

@pi0 pi0 changed the title respect compress settings in dev server fix: respect compress settings in dev server Apr 2, 2026
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.

1 participant