Repository navigation
Support HTTP HEAD requests in CacheView - #68950
Merged
Merged
Conversation
dariatiurina
force-pushed
the
cache-head-support
branch
from
September 1, 2026 12:12
d44fafa to
44668f2
Compare
dariatiurina
marked this pull request as ready for review
September 1, 2026 12:19
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR updates the Components CacheView endpoint caching pipeline so HTTP HEAD requests participate in caching the same way GET requests do, addressing a performance gap for crawler-style traffic where HEAD is expected to be cheap.
Changes:
- Allow
CacheViewService.PrepareAsyncto treatHEADlikeGETfor cache read/write decisions (shared cache key and entries). - Add E2E coverage validating that
HEADrequests hit/populate the cache and thatHEAD-populated entries are reused by laterGETrequests. - Add a test page that exposes a per-
testIdrender counter so cache hits/misses are observable acrossHEAD/GET.
File summaries
| File | Description |
|---|---|
| src/Components/Endpoints/src/CacheView/CacheViewService.cs | Admits HEAD alongside GET when enabling CacheView caching. |
| src/Components/test/E2ETest/Tests/CacheViewTestBase.cs | Adds E2E tests and an HTTP helper to issue HEAD requests against the running server. |
| src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/CacheViewTest/CacheViewHeadPage.razor | Adds a test-only page to observe fragment render counts across requests. |
Review details
- Files reviewed: 3/3 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
oroztocil
approved these changes
Sep 1, 2026
oroztocil
approved these changes
Sep 2, 2026
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.
Support HTTP HEAD requests in CacheView
Description
CacheViewService.PrepareAsyncskipped the cache for any request method other than GET, so<CacheView>never served or populated cache entries for HTTP HEAD requests. Razor component endpoints have advertised HEAD since #64613, and HEAD otherwise runs the identical rendering path as GET, so this was the only place in the endpoint pipeline where the two deliberately diverged. The practical effect was that every HEAD request paid the full render cost, which is backwards from the search-engine-crawler scenario that motivated adding HEAD support in the first place.HEAD now shares GET's cache entries, under the same cache key, in both directions: a HEAD request can be served from an entry created by a GET, and an entry created by a HEAD is served to later GET requests.
Why sharing the cache key is safe
HttpProtocol.CanWriteResponseBody(), which sits well below theCacheViewTextWriter→BufferedTextWriter→HttpResponseStreamWriterchain. Every character still reaches the capture writer, so a HEAD-populated entry is identical to a GET-populated one.CacheView. Unlike HEAD, a POST reads the request body, validates antiforgery, and dispatches form handlers, so its render is request-specific and must not be cached or replayed.A separate cache key for HEAD was considered and rejected: it would halve the hit rate for no benefit, since RFC 9110 §9.3.1 explicitly permits a cached GET response to satisfy a subsequent HEAD request.
Changes
CacheViewService.PrepareAsyncnow admits HEAD alongside GET when deciding whether caching is active. The cache key is unchanged, so GET and HEAD resolve to the same entry.CacheViewTestBase, which run against both the memory-cache and HybridCache stores: repeated HEAD requests must neither re-render the cached fragment nor change its content, and an entry populated by a HEAD request must be reused by a later GET.CacheViewHeadPagetest asset (/cache-component-head), which exposes a per-testIdfragment render counter so the effect of a HEAD request is observable on a subsequent GET.Testing
Coverage is end-to-end, against the real server stack, because the behavior being fixed is an HTTP-level concern: whether a HEAD request reaches the cache, and whether the entry it produces is shared with GET. The render counter on the test page is what makes a cache hit observable, since a HEAD response itself has no body to assert on.
Breaking changes
None observable by clients. A HEAD response carries no content at all (RFC 9110 §9.3.2: HEAD is "identical to GET except that the server MUST NOT send content in the response"), so no caller can see a difference in returned markup. Serving a cached GET representation to a HEAD request is explicitly sanctioned by RFC 9110 §9.3.1: "The response to a GET request is cacheable; a cache MAY use it to satisfy subsequent GET and HEAD requests."
The direction worth reviewer attention is the reverse one, which a conventional HTTP cache cannot do at all, because a HEAD response has no body to store:
CacheViewcaptures the rendered fragment before the server drops it, so a HEAD request can now populate an entry that later GET requests are served from. Nothing in the framework rendering path branches on the request method, so that fragment is identical to what a GET would have produced. The only way to get a divergence is application code inside aCacheViewthat explicitly inspectsHttpContext.Request.Method. Such a component should be annotated[CacheBehavior(CacheBehavior.Rerender)]so it stays live and re-renders on every request, which is already the requirement for any component with per-request output.Fixes #68916