我使用 Mac OS X 和 Chrome 制作了一个带有 Django Rest Framework 后端的 React 应用程序。当我刷新应用程序几次时,应用程序停止调用 API 并开始使用缓存数据,即使我有 no-cache
和 max-age
header 。有趣的是,如果开发者工具窗口打开,它总是进行 API 调用而不使用缓存数据。有没有办法强制应用程序始终进行 API 调用,无论开发工具是否打开?
我已经做了很多事情来解决这个问题。我添加了一个包含秒数的查询参数,例如 this SO link says to do .
我还在 DRF 上添加了分析 mentioned here .
奇怪的是,在调用了一些 API 后我没有在 Django 端看到日志,这对我来说意味着 Chrome 正在缓存一些东西。
我在 Safari 中没有看到这种行为。
我还创建了一个简单的 JQuery 应用程序并在那里看到了这种行为,所以我认为这不是 React/Fetch 问题。
它确实不一致,因此很难排除故障。
编辑
这是我显示缓存控制的 header ,我仍然遇到这种情况:
HTTP/1.1 200 OK
Server: gunicorn/19.3.0
Date: Wed, 09 Nov 2016 16:39:07 GMT
Connection: close
Transfer-Encoding: chunked
Expires: Wed, 09 Nov 2016 16:39:07 GMT
Last-Modified: Wed, 09 Nov 2016 16:39:07 GMT
Allow: GET, POST, HEAD, OPTIONS
Cache-Control: no-cache, no-store, must-revalidate, max-age=0
Access-Control-Allow-Origin: *
Content-Type: application/json
Request Headers
view source
最佳答案
直接来自 ( How to stop chrome from caching REST response from WebApi? )
The answer is that Chrome does not like "Expires:Mon, 01 Jan 0001 00:00:00 GMT" (a fake date, basically).
I changed my date to be what they use in their Google API, and it worked:
Cache-Control:no-store, must-revalidate, no-cache, max-age=0
Content-Length:1897
Content-Type:application/json; charset=utf-8
Date:Fri, 19 Jul 2013 20:51:49 GMT
Expires:Mon, 01 Jan 1990 00:00:00 GMT
Pragma:no-cache
Server:Microsoft-IIS/8.0
关于javascript - Chrome 正在缓存 API 响应而 Safari 没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40496853/