"Cache-Control: only-if-cached" fails offline even if the response is cached

I am developing a mobile application based on web presentation and based on express.js backend. Technically, it comes down to the built-in Chrome browser and the HTTP endpoint.

In my application, I have a toolbar, and I would like it to behave like this:

  • Get the latest data from the server (the server always responds 200 OKwith a payload and a header Last-Modifiedor 304 Not Modifiedwithout content)
    • If the browser does not already have response caching, execute a regular request with Cache-Control: max-age=0
    • otherwise, always execute a conditional query with a header If-Modified-Since
  • If offline / network error - return to browser cache
    • Fulfill the request Cache-Control: only-if-cached, which should either give a cached response, or fail if it is not found.

The problem is that when the server is down, the backup request with Cache-Control: only-if-cachedalways fails. In particular, it fails even if the browser has a response in the cache (I know that it has it cached because I receive it first 200 OK, and in the second request it gives the response content even after receiving it 304 Not Modified- which has no content). So why does this fail? Or how should I structure the headers associated with the cache, so it works as described?

I run:

  • Chrome WebView v54
  • github / fetch polyfill (but that doesn't really matter as I work on a bare HTTP header)
  • express.js 4.14.0 with disabled ETags

: , , -, , , HTTP- .

+4
1

, HTTP-, , .

only-if-cached (only-if-cached package:chromium). , Chrome.

0

Source: https://habr.com/ru/post/1663008/


All Articles