WebView2Feedback icon indicating copy to clipboard operation
WebView2Feedback copied to clipboard

[Problem/Bug]: Webview2 cache not consistent, does not respect max-age or ETag

Open caymaynard opened this issue 1 year ago • 3 comments

What happened?

I have a WebView 2 application with a request handler that can generate preview images, which are then served to the frontend as PNG files. These files should be cached so that they are not regenerated if the URL is the same.

When handling the request in WebView2, I include a Cache-control header of the form Cache-Control: private, must-revalidate, max-age=3600

I expect this to cache the image for an hour.

What I find though, is that the image is not cached for the entire period, WebView2 will make another request to this URL before an hour has passed. It does not seem to follow a particular rule, the request does get cached, but it seems to rerun the request anyway. This seems to happen after a few separate requests have been handled, the previous requests are removed from the cache.

I have also added an ETag header to the request, to verify if it is for an existing image. Even though I send this header in the request, the response never includes an equivalent 'If-None-Match' header.

I would expect WebView2 to only rerun the request when max-age has passed, and to include the 'If-None-Match' header to verify if the content is the same when the original request includes the ETag header.

Importance

Moderate. My app's user experience is affected, but still usable.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

126.0.2592.102

SDK Version

1.0.2151.40

Framework

Win32

Operating System

Windows 11

OS Version

No response

Repro steps

  1. Setup an add_WebResourceRequested handler inside WebView2 to catch a request.
  2. Request a resource from the frontend which matches the filter.
  3. In the handler, set a Cache-Control Header to 'private, must-revalidate, max-age=3600' and an ETag header to '123'.
  4. Repeat the request from the frontend multiple times for separate images.

Expected: The first request goes to the handler, subsequent requests use the cache. The ETag is sent on any stale data as the 'If-None-Match' header. Actual: Some of the requests use the cache, but not all of them.

Repros in Edge Browser

No, issue does not reproduce in the corresponding Edge version

Regression

No, this never worked

Last working version (if regression)

No response

caymaynard avatar Jul 17 '24 03:07 caymaynard

I'm also seeing this problem. I'm using AddWebResourceRequestedFilter() to receive requests for images from the hosted page. The page contains dynamic content that references image URLs that are fulfilled by my filter. As I interact with the page, causing new dynamic content to be added to the DOM, I'm seeing frequent repeated requests for the same image URLs. Often these repeated requests arrive within seconds of each other.

Adding Cache-Control response headers has no effect.

(Also, like @caymaynard, I find that adding ETag response headers doesn't cause the subsequent requests to carry If-None-Match request headers, but even if it did, I'd rather not receive repeated requests at all.)

RichieHindle avatar Jan 06 '25 21:01 RichieHindle

Any update for this bug? @caymaynard

ccnupq avatar Apr 10 '25 17:04 ccnupq

I personally don't need the cached images anymore, so I haven't been checking if this is still an issue with the latest webview2

caymaynard avatar Apr 10 '25 21:04 caymaynard