简体   繁体   English

向后导航时将网站缓存到浏览器

[英]Cache website to browser when navigating back

I have a single url/page of my website that I want to be cached to browser, so whenever a user navigates away from that page, and then presses the BACK button of the browser, I don't want that request to go to django at all, but serve the cached version of the page that is in the browser. 我的网站只有一个网址/页面,我希望将其缓存到浏览器中,因此,每当用户离开该页面,然后按浏览器的“返回”按钮时,我都不希望该请求进入django完全可以,但是会提供浏览器中页面的缓存版本。 Also, I can't use solutions that cache the page in between the web server and Django, as every user has different permissions on what data they can see. 另外,我不能使用在Web服务器和Django之间缓存页面的解决方案,因为每个用户对其看到的数据都有不同的权限。 So I added this in my nginx config: 所以我在我的nginx配置中添加了这个:

...
location /search {
    expires 300s;
    add_header Cache-Control "private";
...

And this works very well, 50% of the time :). 而且效果很好,有50%的时间是:)。 How can I make it work always? 如何使它始终运行?

whenever a user navigates away from that page, and then presses the BACK button of the browser, I don't want that request to go to django at all, but serve the cached version of the page that is in the browser 每当用户离开该页面,然后按浏览器的“后退”按钮时,我根本不希望该请求转到django,而是提供浏览器中页面的缓存版本

For some browsers, this is the default behavior - if you have set no caching directives on the server, then it will keep not only a copy of the response but the entire rendered page in memory so than when you click the back button, it can be shown instantly. 对于某些浏览器,这是默认行为-如果您在服务器上未设置任何缓存指令,那么它将不仅在内存中保留响应的副本,而且还会保留整个呈现的页面,因此与单击“后退”按钮相比,它可以立即显示。

But if you want to explicitly instruct the browser to cache the response you can use a max-age directive on the Cache-Control header. 但是,如果要明确指示浏览器缓存响应,则可以在Cache-Control标头上使用max-age指令。 Set

Cache-Control: max-age=3600

This is a more modern and reliable way than using an "Expires" header, especially for small durations. 与使用“ Expires”标头相比,这是一种更现代,更可靠的方法,尤其是在短时间内。 If the user's browser has the incorrect time or time zone set "Expires" might not work at all, but "max-age" still should. 如果用户的浏览器的时间或时区设置不正确,则“到期”可能根本不起作用,但“最大年龄”仍然应该起作用。

If you are serving each person a different version of the page you can add the "private" too to prevent caching by proxies (as in your example): 如果为每个人提供不同版本的页面,则也可以添加“ private”以防止代理缓存(如您的示例):

Cache-Control: private; max-age=3600

Note: you can't force a browser to always use the cache. 注意:您不能强制浏览器始终使用缓存。 If you are noticing that sometimes it doesn't use the cache, it could be: 如果您注意到有时它不使用缓存,则可能是:

  • The item in the cache has expired. 缓存中的项目已过期。 You were giving it only 5 minutes, so 5 minutes after the request that went into the cache, if you request it again it will send through the request to the remote server - even if there have been requests in the time between. 您只给了5分钟,因此在进入缓存的请求之后5分钟,如果再次请求,它将通过请求发送到远程服务器-即使两次之间存在请求。

  • The browser cache became full and some items were purged. 浏览器缓存已满,某些项目已清除。

  • For some reason the browser believed or was configured to believe that the response should not be cached regardless of cache directives. 由于某种原因,浏览器认为或配置为认为无论缓存指令如何都不应缓存响应。

  • The user pressed reload. 用户按下了重新加载。

  • A proxy between client and server stripped the Cache-Control or other headers. 客户端和服务器之间的代理剥离了Cache-Control或其他标头。

暂无
暂无

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

相关问题 如何防止浏览器在Django中导航时重新填充表单数据 - How to prevent browser from refilling form data when navigating back in Django Django缓存:缓存预热后重新加载浏览器缓存 - Django caching : reload browser cache when cache is warmed up Django:never_cache 不起作用,它仍然在浏览器后退按钮上显示相同的数据 - Django: never_cache is not working, it still shows same data on browser back button 在任何浏览器上单击后退按钮时,表单中的数据都会丢失(Django) - lose data in the form when clicking the back button on any browser (Django) 在Django中使用python-social-auth重定向回网站时不可用的类型 - unhashable type when redirecting back to the website using python-social-auth in Django 返回浏览器后,如何以django形式获取字段的现有值? - How do I get the existing value of the fields in a django form when going back in the browser? 使用浏览器的“后退按钮”时,如何刷新查询集? - How can I refresh the queryset when using the browser's “back button?” 在应用程序内浏览器中,Django allauth facebook 登录未能重定向回来 - Django allauth facebook login failed to redirect back when in an in-app-browser 导航到新网址时,JavaScript在Safari中不起作用 - Javascript not working in Safari when navigating to a new url 当用户关闭基于django2.0构建的网站上的选项卡或浏览器时,如何强制注销用户 - How to forcefully log out a user when user closes tab or browser on website built on django2.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM