Javascript 如何清除主屏幕上 IOS Web App 的缓存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8063126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:34:46  来源:igfitidea点击:

How can I clear the cache of an IOS Web App on the Homescreen?

javascriptiphoneweb-applicationsjqtouchiphone-standalone-web-app

提问by GilbertSinnott

I am using JQTouch to create a Web App on the Homescreen using meta tag "apple-mobile-web-app-capable", but no matter how many times I clear the cache from within Settings, the Web App's Javascript is still an older cached version.

我正在使用 JQTouch 在主屏幕上使用元标记“apple-mobile-web-app-capable”创建一个 Web 应用程序,但是无论我从“设置”中清除缓存多少次,Web 应用程序的 Javascript 仍然是较旧的缓存版本。

Strangely enough, if I set the meta tag's content to;

奇怪的是,如果我将元标记的内容设置为;

<meta name="apple-mobile-web-app-capable" content="no" />

...then add the Web App to the Homescreen, I get the latest version when I launch it.

...然后将 Web 应用程序添加到主屏幕,当我启动它时我会得到最新版本。

Yet if I set the meta tag to;

但是,如果我将元标记设置为;

<meta name="apple-mobile-web-app-capable" content="yes" />

...then add the Web App to the Homescreen, I get the older cached version.

...然后将 Web 应用程序添加到主屏幕,我得到了较旧的缓存版本。

PS. I can confirm that is only the javascriptwhich will not refresh.

附注。我可以确认这只是不会刷新的 javascript

回答by SquareFeet

You could try adding a unique string to the end of your js include's src attribute (use a timestamp, for example). Bear in mind you'll have to change this unique string for each refresh you want to do, so you may want to consider doing lazy-loading (which may in itself solve your problem): <script type="text/javascript" src="myScript.js?012345"></script>

您可以尝试在 js 包含的 src 属性的末尾添加一个唯一的字符串(例如,使用时间戳)。请记住,您必须为要执行的每次刷新更改此唯一字符串,因此您可能需要考虑进行延迟加载(这本身可能会解决您的问题): <script type="text/javascript" src="myScript.js?012345"></script>

This usually forces the browser into grabbing the latest version of the script. Might work...

这通常会强制浏览器获取脚本的最新版本。可能工作...

回答by Mageek

This is a way that worked for me to clear completelythe cache:

这是一种对我来说完全清除缓存的方法:

1-Delete the tabs in safari where there is your app.
2-Delete the icon of the app in the home screen.
3-Killsafari from the memory.
4-Go to settings/safari and press clear cache.
5-You can restart your iPhone/iPod if you want to be sure that it works

1-删除 safari 中包含您的应用程序的选项卡。
2-删除主屏幕中的应用程序图标。
3-从记忆中杀死safari。
4-转到设置/Safari 并按清除缓存。
5-如果你想确保它可以工作,你可以重新启动你的 iPhone/iPod

回答by muzzamo

I found that using bundlingfeature in ASP.NET MVC solved this problem for me.

我发现在 ASP.NET MVC中使用捆绑功能为我解决了这个问题。

It automatically generates a bundled link of the form:

它会自动生成以下形式的捆绑链接:

http://www.domain.com/MvcBM_time/bundles/AllMyScripts?v=r0sLDicvP58AIXN_mc3QdyVvVj5euZNzdsa2N1PKvb81

http://www.domain.com/MvcBM_time/bundles/AllMyScripts?v=r0sLDicvP58AIXN_mc3QdyVvVj5euZNzdsa2N1PKvb81

The token after the v= changes if any file in the bundle changes, guaranteeing that browser requests for the bundle will get the latest bundle.

如果捆绑包中的任何文件发生更改,则 v= 之后的令牌也会更改,从而保证浏览器对捆绑包的请求将获得最新的捆绑包。

回答by Chris

This drove me absolutely nuts. I tried killing Safari, clearing the cache, switching to private mode, and removing the web app from the home screen. None of those work, but what does work is temporarily loading the web app page in Safari itself and reloading.

这让我非常疯狂。我尝试杀死 Safari,清除缓存,切换到私有模式,并从主屏幕中删除 Web 应用程序。这些都不起作用,但有效的是在 Safari 本身中临时加载 Web 应用程序页面并重新加载.

If you're using add to home screen, you can add ?web_app_ready=1 to the install URL. Since that's not an end user procedure, adding a query string to every script (the answer above) will probably be more effective for non-developer environments.

如果您使用add to home screen,您可以将 ?web_app_ready=1 添加到安装 URL。由于这不是最终用户过程,因此向每个脚本(上面的答案)添加查询字符串对于非开发人员环境可能更有效。

回答by SnazzyJava

Building off of what SquareFeet said, one could automatically pull the new file down every time with a unique GET tag appended to the filename, using PHP. For example:

根据 SquareFeet 所说的,人们可以使用 PHP 每次在文件名后附加一个唯一的 GET 标签来自动拉下新文件。例如:

<link rel="stylesheet" type="text/css" href="/css/global.css?
   <?php echo(mt_rand(10000000, 99999999)); ?>
" />

Theoretically, the same task could be accomplished using JavaScript, albeit with some modifications to the web server:

理论上,同样的任务可以使用 JavaScript 完成,尽管对 Web 服务器进行了一些修改:

<link rel="stylesheet" type="text/css" href="/css/global.css?
   <div id="id"></div>
" />

<script language="Javascript" type="text/javascript">
   document.getElementById("id").innerHTML = Math.floor((Math.random() * 10000) + 1);
</script>

回答by Dmitry Grinberg

If you are jailbroken, just do this in the terminal

如果您已越狱,只需在终端中执行此操作

rm ./Library/Caches/com.apple.webapp/Cache.db