简体   繁体   English

通过AJAX加载内容时的浏览器缓存问题

[英]Browser cache issues when loading content through AJAX

I'm having some issues when rendering a website that I can only attribute to the browser caching an old version of my page. 呈现网站时出现一些问题,我只能归因于浏览器缓存页面的旧版本。

Here's what's happening, my website shows a cart at the right side of the screen that's updated every time the user hits the buy button next to each item. 这就是发生的情况,我的网站在屏幕右侧显示了一个购物车,每当用户单击每个商品旁边的购买按钮时,购物车都会更新。

Let's say my cart is empty, the user hits buy over an item and the bar updates itself to reflect the newly added item... keep in mind that I'm achieving this through the usage of an Ajax call (jQuery), so far everything works perfectly. 假设我的购物车是空的,用户点击了某商品的“购买”,并且栏会自动更新以反映新添加的商品……请记住,到目前为止,我已经通过使用Ajax调用(jQuery)实现了这一点一切正常。

However, if I hit the browser's back button and quickly press forward to return to page Chrome will render the version that was loaded before the AJAX calls. 但是,如果我按下浏览器的后退按钮并快速按前进以返回页面,Chrome将呈现在AJAX调用之前加载的版本。 Here are a few things I noticed while debugging the problem. 这是调试问题时我注意到的几件事。

  1. This only happens in Chrome and Firefox 这仅在Chrome和Firefox中发生
  2. The cart is not emptied if I refresh the page 如果刷新页面,则购物车未清空
  3. In Chrome, if I try the steps mentioned above while having the developer tools tab open (Cache disabled) the bug is not occurring 在Chrome中,如果我在打开开发人员工具标签(禁用缓存)的同时尝试上述步骤,则不会出现该错误
  4. In Chrome and without the developer tools, if I hit the back button and wait for at least 60 seconds and the return to the page, the problem doesn't show 在没有开发人员工具的Chrome中,如果我按下“后退”按钮并等待至少60秒钟,然后返回页面,则不会出现问题
  5. In a similar way, if I wait for at least 60 seconds after adding the item to the cart, go back and forward, no issue can be seen 以类似的方式,如果我将商品添加到购物车后至少等待60秒,然后再来回前进,则看不到任何问题

Any ideas on what can I try to avoid this? 关于如何避免这种情况的任何想法? Preferably javascript or tags since I have no control over the HTTP code that setups the headers. 最好是javascript或标签,因为我无法控制设置标头的HTTP代码。

I'm aware of the following code in order to force or "suggest" a cache flush. 我知道以下代码是为了强制或“建议”缓存刷新。 However, it only works with HTML5 但是,它仅适用于HTML5

// Check if a new cache is available on page load.
window.addEventListener('load', function(e) {

  window.applicationCache.addEventListener('updateready', function(e) {
    if (window.applicationCache.status == window.applicationCache.UPDATEREADY) {
      // Browser downloaded a new app cache.
      // Swap it in and reload the page to get the new hotness.
      window.applicationCache.swapCache();
      if (confirm('A new version of this site is available. Load it?')) {
        window.location.reload();
      }
    } else {
      // Manifest didn't changed. Nothing new to server.
    }
  }, false);

}, false);

Try appending a random query string to the url you're hitting with AJAX. 尝试将随机查询字符串附加到您用AJAX命中的网址上。

var url = 'http://some.url.com/thing?' + new Date().getTime();
$.get(url);

This works so long as there aren't multiple requests happening in the same millisecond. 只要在同一毫秒内没有发生多个请求,此方法就起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM