Add cache validators to embedded static assets - #97
Merged
Conversation
Files served from embed.FS carry a zero ModTime, so http.FileServerFS emitted no Last-Modified/ETag and no Cache-Control, forcing browsers to refetch every dashboard asset on every page load. Handler now wraps the file server to set Cache-Control: no-cache plus a version-derived ETag, so browsers revalidate cheaply (a 304 when the version matches) while still fetching fresh assets immediately after an upgrade changes the build version. Closes #71
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



📖 Description
Files served from
embed.FScarry a zeroModTime, sohttp.FileServerFS(internal/web/embed.go) emitted noLast-Modified/ETag, and noCache-Controlheader was set — every dashboard page load refetched all static assets, and no intermediary could revalidate a cached copy against anything.web.Handlernow takes the build-timeversionstring (already available asmain.version, and echoed byGET /api/v1/config) and wraps the file server to setCache-Control: no-cacheplus a version-derivedEtagon every response before handing off tohttp.FileServerFS. Go's stdlib conditional-request handling (checkPreconditionsinnet/http/fs.go) picks up anEtagset on the response writer beforeServeContentruns, so this is enough for the file server to answer a matchingIf-None-Matchwith304 Not Modified— no extra logic needed. After an upgrade changesversion, the ETag changes too, so clients refetch immediately instead of serving stale JS.This is an internal API change (
web.Handler()→web.Handler(version string)); the only caller iscmd/pimonitor/main.go, updated to passversion. No REST API shape changes.🎫 Issues
Closes #71
👩💻 Reviewer Notes
Worth double-checking the
Etag/Cache-Controlbehavior directly against the running binary if you want an extra sanity check, e.g.:📑 Test Plan
Added to
internal/web/embed_test.go:TestHandler_SetsCacheHeaders— assertsCache-Control: no-cacheand the version-derivedEtagare present.TestHandler_RevalidatesOnMatchingETag— a request with a matchingIf-None-Matchgets304 Not Modified.TestHandler_RefetchesOnVersionChange— a staleIf-None-Match(from a different version) still gets200 OK.Existing
Handler(...)call sites inembed_test.goupdated to pass a version string.go build ./...,go vet ./...,go test ./... -race -cover, andgolangci-lint runall pass locally.✅ Checklist
General
go test ./... -race -coverpasses locally).go vet ./...andgolangci-lint runare clean.ARCHITECTURE.mdif this changes a documented design decision.REST API / configuration / packaging
Not applicable — no REST API, configuration, or packaging changes.
⏭ Next Steps
None.