Skip to content

Support HTTP HEAD requests in CacheView - #68950

Merged
dariatiurina merged 3 commits into
dotnet:mainfrom
dariatiurina:cache-head-support
Sep 2, 2026
Merged

dariatiurina merged 3 commits into
dotnet:mainfrom
dariatiurina:cache-head-support

Conversation

@dariatiurina

@dariatiurina dariatiurina commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Support HTTP HEAD requests in CacheView

Description

CacheViewService.PrepareAsync skipped 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

  • HEAD renders the identical representation as GET. Nothing in the framework rendering path branches on the request method, so the captured fragment is the same either way.
  • Body suppression happens in Kestrel's HttpProtocol.CanWriteResponseBody(), which sits well below the CacheViewTextWriter → BufferedTextWriter → HttpResponseStreamWriter chain. Every character still reaches the capture writer, so a HEAD-populated entry is identical to a GET-populated one.
  • POST continues to bypass 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.PrepareAsync now admits HEAD alongside GET when deciding whether caching is active. The cache key is unchanged, so GET and HEAD resolve to the same entry.
  • Added two E2E tests to 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.
  • Added the CacheViewHeadPage test asset (/cache-component-head), which exposes a per-testId fragment 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: CacheView captures 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 a CacheView that explicitly inspects HttpContext.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

@dariatiurina dariatiurina self-assigned this Sep 1, 2026
@dariatiurina
dariatiurina marked this pull request as ready for review September 1, 2026 12:19
@dariatiurina
dariatiurina requested a review from a team as a code owner September 1, 2026 12:19
Copilot AI lite review requested due to automatic review settings September 1, 2026 12:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.PrepareAsync to treat HEAD like GET for cache read/write decisions (shared cache key and entries).
  • Add E2E coverage validating that HEAD requests hit/populate the cache and that HEAD-populated entries are reused by later GET requests.
  • Add a test page that exposes a per-testId render counter so cache hits/misses are observable across HEAD/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]>
@dariatiurina
dariatiurina enabled auto-merge (squash) September 2, 2026 08:39
@dariatiurina
dariatiurina merged commit 70f2905 into dotnet:main Sep 2, 2026
31 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 3, 2026
@dariatiurina
dariatiurina deleted the cache-head-support branch September 29, 2026 09:44
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.

CacheView does not apply to HTTP HEAD requests

3 participants