Skip to content

Add ETag support for static files - #834

Merged
ademar merged 2 commits into
masterfrom
copilot/add-etag-support
Jul 15, 2026
Merged

Add ETag support for static files#834
ademar merged 2 commits into
masterfrom
copilot/add-etag-support

Conversation

Copilot AI commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Suave.Files had no ETag support, so clients could only revalidate cached static files via If-Modified-Since (second-precision only).

Changes

  • Weak ETag, on by defaultFiles.file now emits ETag: W/"{length:x}-{ticks:x}" derived from FileInfo.Length + LastWriteTimeUtc.Ticks. No hashing, no FileSystemWatcher, no extra I/O beyond the stat already needed for Last-Modified, so the opt-in concern raised in the issue doesn't apply.
  • If-None-Match handlingServeResource.resource short-circuits to 304 Not Modified before the If-Modified-Since branch. Supports * and comma-separated tag lists; comparison is opaque-string equality after trim (RFC 7232 §3.2 weak comparison — correct when the client echoes back what we sent). The ETag header is re-emitted on the 304 (which resets headers).
  • ServeResource.resource signature — new getEtag : string -> string option parameter. Embedded-asset serving passes fun _ -> None, preserving existing behavior.
  • Public helperFiles.fileEtag : string -> string exposed for callers who want the same value (e.g. custom pipelines).
  • Tests — 6 cases in HttpFile.fs: emitted on 200, stable across calls, match → 304, 304 carries ETag, non-match → 200, * → 304.
  • Docs — brief note in docs/files.md.

Example

// Nothing to configure; ETag is on for all file serving.
let app = Files.browseHome

// $ curl -I http://localhost:8080/index.html
// HTTP/1.1 200 OK
// ETag: W/"1a4-8dbf1c3a0b1a2e0"
// Last-Modified: Tue, 15 Jul 2026 14:20:00 GMT
//
// $ curl -I -H 'If-None-Match: W/"1a4-8dbf1c3a0b1a2e0"' http://localhost:8080/index.html
// HTTP/1.1 304 Not Modified
// ETag: W/"1a4-8dbf1c3a0b1a2e0"

Copilot AI linked an issue Jul 15, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Add mechanism for serving files with ETag headers Add ETag support for static files Jul 15, 2026
Copilot AI requested a review from ademar July 15, 2026 14:40
@ademar
ademar marked this pull request as ready for review July 15, 2026 14:45
@ademar
ademar merged commit f51e93c into master Jul 15, 2026
2 checks passed
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.

Question: ETag headers for static files

2 participants