Skip to content

fix: honor the etag option passed to res.sendFile() and res.download() - #7604

Closed
Taiwudao wants to merge 1 commit into
expressjs:masterfrom
Taiwudao:fix/sendfile-etag-option
Closed

Taiwudao wants to merge 1 commit into
expressjs:masterfrom
Taiwudao:fix/sendfile-etag-option

Conversation

@Taiwudao

Copy link
Copy Markdown

res.sendFile() (and res.download(), which delegates to it) overwrites the per-response etag option with the application setting:

opts.etag = this.app.enabled('etag');

As a result, { etag: false } is ignored when the app has ETags enabled, and { etag: true } is ignored when the app disables ETags. Express 4.21.2 passed the option through to send, and the 5.1.0 changelog lists "Add support for ETag option in res.sendFile()" (#6073), so an explicit option should take precedence over the app-level default.

This treats the app setting as a default:

if (opts.etag === undefined) {
  opts.etag = this.app.enabled('etag');
}

Behavior without the option is unchanged: res.sendFile() and res.download() still follow app.disable('etag').

Before the fix, with default app settings on Node 26:

GET /send-off (sendFile with etag: false) -> 200, ETag: W/"4-1a1fcddee45"
GET /download-off (download with etag: false) -> 200, ETag: W/"4-1a1fcddee45"
GET /send-on (sendFile with etag: true, app.disable("etag")) -> 200, no ETag

On Express 4.21.2 the same routes return: no ETag, no ETag, ETag.

Tests:

  • test/res.sendFile.js: opt out with { etag: false }; opt in with { etag: true } while app.disable('etag') is set.
  • test/res.download.js: res.download(path, { etag: false }) sends no ETag.

Without the fix the three new tests fail (3 failing). With it, the two affected files pass (88 passing), the full suite passes (1292 passing, 1 pending), and npm run lint is clean.

Refs #2294 (app-level ETag default for 5.x) and #6073 (the sendFile wiring).

@krzysdz

krzysdz commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Duplicate of #7467

@krzysdz krzysdz marked this as a duplicate of #7467 Nov 21, 2026
@krzysdz krzysdz closed this Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants