How to force the browser to cache a page .html?

你离开我真会死。 提交于 2021-01-27 13:52:15

问题


Someone know how to force a page cache in the browser? I need to reload a page several times, and the browser isn't caching the page!


回答1:


If browser is not caching the page there's no much you can do than using one of them:

<meta http-equiv="Cache-control" content="public">

OR

<meta http-equiv="Cache-control" content="private">

You can't programatically override browser settings. It would be a security issue!




回答2:


Even if you have <meta http-equiv="Cache-control" content="public"> there are other factors to consider.

Note that some browsers have a file size limit for caching ... keep html below 25Kb if you can and external resources like css, js etc... below 1Mb, but for browser specifics see: here and here

You can check browser.cache.disk.max_entry_size on some browsers and you can approximate the length with document.getElementsByTagName('html')[0].outerHTML.length if it is not possible to precalculate the sizes of your files.

Your server needs to appropriately set "Last-Modified" and "Expires"

This will only increase the likelihood of caching if it is enabled though, not force it. You might consider using local storage if it is enabled and caching is not (some people will just disable caching altogether to cope with 1 particular site)




回答3:


You have to add this metatag :

<meta http-equiv="Cache-control" content="public">


来源:https://stackoverflow.com/questions/19059380/how-to-force-the-browser-to-cache-a-page-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!