ReactJS:IE11 不使用缓存数据发出新的 HTTP 请求
ReactJS: IE11 not making new HTTP request, using cached data
我在 IE11
上的 React 应用程序中遇到问题,其中 UI 没有为每个新请求访问后端服务并从缓存数据返回响应。该应用程序在 Chrome.
上运行良好
对于 IE,服务以代码结束:304 而不是 200。
PFB 请求 headers:
Accept application/json,*/*
Request GET /services/v0/search/?uid=12900 HTTP/1.1
Content-Type application/json
Cache-Control no-cache
PFB 在 IE
上获得的响应 headers:
Response HTTP/1.1 304 Not Modified
x-frame-options DENY
x-xss-protection 1; mode=block
x-content-type-options nosniff
任何线索,IE 呈现这种行为背后的原因可能是什么? TIA
来自docs
在您的 http 请求中检查此 header:
Cache-Control:
no-cache
:
强制缓存在释放缓存副本之前将请求提交给源服务器进行验证
no-store
:
缓存不应存储有关客户端请求或服务器响应的任何信息。
must-revalidate
(Revalidation and reloading
) :
缓存在使用之前必须验证过时资源的状态,不应使用过期的资源
Expires
:
0 - 资源已过期
您可以尝试添加 "Pragma" header:
headers: { Pragma: 'no-cache'}
这里也提到了:Axios only called once inside self-invoking function (Internet Explorer)
如果客户端解决方案不能作为最后的手段,您可以尝试在服务器端设置 headers。
如果您使用 node
和 express
,您可以编写一个中间件,为您添加所需的路由 headers,它看起来像这样:
function cacheMiddleware(req, res, next) {
// Should work for detecting IE8+
if (req.headers['user-agent'].includes('Trident')) {
res.set({
'Cache-Control': 'no-store, no-cache, must-revalidate',
Pragma: 'no-cache',
Expires: new Date('01.01.2000'),
});
}
next();
}
router.get('/', cacheMiddleware, (req, res) => res.send('content'))
解决方案的想法来自 link
我刚刚遇到了同样的问题,即 IE11 只是忽略了对后端服务器的获取请求。我发现修复的快速方法是在 get 请求中传递一个不必要的参数,在我们的例子中是一个时间戳。
const t = Date.now();
axios.get(`${API_DOMAIN}/api/bookingep/find?c=${t}`);
因为每次时间戳都不一样,ie11确实按预期发送了get请求
我在 IE11
上的 React 应用程序中遇到问题,其中 UI 没有为每个新请求访问后端服务并从缓存数据返回响应。该应用程序在 Chrome.
对于 IE,服务以代码结束:304 而不是 200。 PFB 请求 headers:
Accept application/json,*/*
Request GET /services/v0/search/?uid=12900 HTTP/1.1
Content-Type application/json
Cache-Control no-cache
PFB 在 IE
上获得的响应 headers:
Response HTTP/1.1 304 Not Modified
x-frame-options DENY
x-xss-protection 1; mode=block
x-content-type-options nosniff
任何线索,IE 呈现这种行为背后的原因可能是什么? TIA
来自docs
在您的 http 请求中检查此 header:
Cache-Control:
no-cache
:
强制缓存在释放缓存副本之前将请求提交给源服务器进行验证
no-store
:
缓存不应存储有关客户端请求或服务器响应的任何信息。
must-revalidate
(Revalidation and reloading
) :
缓存在使用之前必须验证过时资源的状态,不应使用过期的资源
Expires
:
0 - 资源已过期
您可以尝试添加 "Pragma" header:
headers: { Pragma: 'no-cache'}
这里也提到了:Axios only called once inside self-invoking function (Internet Explorer)
如果客户端解决方案不能作为最后的手段,您可以尝试在服务器端设置 headers。
如果您使用 node
和 express
,您可以编写一个中间件,为您添加所需的路由 headers,它看起来像这样:
function cacheMiddleware(req, res, next) {
// Should work for detecting IE8+
if (req.headers['user-agent'].includes('Trident')) {
res.set({
'Cache-Control': 'no-store, no-cache, must-revalidate',
Pragma: 'no-cache',
Expires: new Date('01.01.2000'),
});
}
next();
}
router.get('/', cacheMiddleware, (req, res) => res.send('content'))
解决方案的想法来自 link
我刚刚遇到了同样的问题,即 IE11 只是忽略了对后端服务器的获取请求。我发现修复的快速方法是在 get 请求中传递一个不必要的参数,在我们的例子中是一个时间戳。
const t = Date.now();
axios.get(`${API_DOMAIN}/api/bookingep/find?c=${t}`);
因为每次时间戳都不一样,ie11确实按预期发送了get请求