Repository: static-web-server/static-web-server (language: rust)

Issue:
Don't set "Last-Modified" if mtime is Unix epoch
### Search for duplicate feature request

- [X] I already searched, and this feature request or improvement is not a duplicate.

### Feature scope

Target/OS/arch/platform

### Feature request related to a problem

In NixOS, it's common to set the mtime of build artifacts to the Unix epoch in order to ensure reproducible builds. In this case, ETag can still be used as a validator. However, some caching proxies (including Cloudflare) tend to prioritize "Last-Modified" over "ETag" when performing revalidation if it's provided. To avoid this, the "Last-Modified" header should not be set.

### Describe the solution you'd like

Don't set "Last-Modified" if mtime is Unix epoch. This is what Caddy does, and has worked pretty well.

### Describe alternatives you've considered

Maybe add some config option? But I don't really see the point of serving "Last-Modified" when it's clearly wrong (e.g. Unix epoch).

### Build target

All targets

### Additional context

_No response_


Discussion / hints:
The Go standard library also has this behavior by default: https://github.com/golang/go/blob/8c445b7c9fe6738cbef2040a1011bd11489b0806/src/net/http/fs.go#L551-L553.
That's a good enhancement! Feel free to submit a PR.

Fix patch:
diff --git a/src/response.rs b/src/response.rs
index e9bfe97c..e12698e6 100644
--- a/src/response.rs
+++ b/src/response.rs
@@ -37,7 +37,13 @@ pub(crate) fn response_body(
     #[cfg(feature = "experimental")] memory_cache: Option<&MemCacheOpts>,
 ) -> Result<Response<Body>, StatusCode> {
     let mut len = meta.len();
-    let modified = meta.modified().ok().map(LastModified::from);
+    // If the file's modified time is the UNIX epoch, then it's likely not valid and should
+    // not be included in the Last-Modified header to avoid cache revalidation issues.
+    let modified = meta
+        .modified()
+        .ok()
+        .filter(|&t| t != std::time::UNIX_EPOCH)
+        .map(LastModified::from);
 
     match conditionals.check(modified) {
         ConditionalBody::NoBody(resp) => Ok(resp),


Test patch: