-1

If you fetch an image to display a second or n+1 times, or fetch some JSON likewise, and nothing has changed, then the browser shouldn't actually download/fetch the content. This is how GET requests work with caching.

But I'm wondering, hypothetically, if instead of using GET you use PATCH to fetch the image or JSON. Wondering if the browser can still use its cached version if nothing has changed, or what needs to be done to make PATCH work like a GET so that it doesn't fetch cached content.

Lance
  • 75,200
  • 93
  • 289
  • 503

1 Answers1

1

It's important to understand that PATCH is not for fetching anything. You're making a change on the server and the response may have information about how the change was applied.

HTTP requests other than GET sometimes can be cacheable. To find out if PATCH is, you can read the RFC's. The RFC has this to say:

A response to this method is only cacheable if it contains explicit freshness information (such as an Expires header or "Cache-Control: max-age" directive) as well as the Content-Location header matching the Request-URI, indicating that the PATCH response body is a resource representation. A cached PATCH response can only be used to respond to subsequent GET and HEAD requests; it MUST NOT be used to respond to other methods (in particular, PATCH).

This already suggest 'no', doing a PATCH request twice will not result in the second to be skipped.

A second thing to look out for with HTTP methods is if they are idempotent or safe. PATCH is neither.

RFC7231 has this to say about cacheable methods:

In general, safe methods that do not depend on a current or authoritative response are defined as cacheable; this specification defines GET, HEAD, and POST as cacheable, although the overwhelming majority of cache implementations only support GET and HEAD.

Both of these suggest that 'no', PATCH is not cacheable, and there's no set of HTTP headers that will make it so.

Evert
  • 93,428
  • 18
  • 118
  • 189