简体   繁体   English

更新离线缓存(Chrome浏览器移动版)

[英]Updating Offline Cache (Chrome On Mobile)

Afternoon All, 下午全部

A bit of background - I'm building a custom calendar for a company where jobs can be scheduled and engineers can access it from their mobile to know when and where they're going. 有点背景-我正在为一家公司创建自定义日历,可以在该日历中安排工作,工程师可以通过移动设备访问该日历,以了解他们的去向和时间。 They previously used Google calendar but now want something bespoke. 他们以前使用过Google日历,但现在想要定制一些东西。

All is fine until somebody loses phone signal and gets a horrible offline page in Chrome and can't access any information. 一切正常,直到有人失去电话信号并在Chrome中显示一个可怕的脱机页面并且无法访问任何信息为止。 What I'm wanting to do is have it save an offline version of the calendar but also update it when they re-visit it with a better connection - as job times often change. 我想要做的是让它保存日历的脱机版本,并且在他们以更好的连接重新访问它时也进行更新-因为工作时间经常更改。


I've tried saving the page and enabling offline mode in Chrome but the page doesn't update until you manually clear the cache so no good. 我曾尝试保存页面并在Chrome中启用离线模式,但是直到您手动清除缓存后页面才会更新,因此效果不佳。

I've tried adding some javascript to hard refresh the page in the hope it clears the browser cache but again it doesn't update the page. 我尝试添加一些JavaScript来硬刷新页面,希望它清除浏览器缓存,但又不会更新页面。

 <script>location.reload(true);</script> 

I read about cache manifests and have tried that too but although it feels like it wants to work it also doesn't update the page until I go to chrome://appcache-internals and remove the file. 我阅读了有关缓存清单的信息,并进行了尝试,但是尽管感觉像它想工作一样,但它也不会更新页面,直到我转到chrome:// appcache-internals并删除文件为止。

 CACHE MANIFEST /calendar.php /css/style.css 

Neither PHP or Javascript headers work either as they either don't update the file on re-visit or simply don't save any files in the first place. PHP或Javascript标头都不起作用,因为它们要么不会在重新访问时更新文件,要么根本就不会保存任何文件。

 header("Cache-Control: no-cache, must-revalidate"); 

 <meta http-equiv="Cache-Control" content="no-store" /> 

As far as I can tell there's no way to manually delete a user's website cache and re-download it and once the cache has been saved there's no way to force it to update. 据我所知,无法手动删除用户的网站缓存并重新下载,并且一旦保存了缓存,就无法强制对其进行更新。 If you set it to expire then it's not there to access and you don't know when they will next have a connection to update so I'm going round in circles. 如果您将其设置为过期,那么就无法访问它,并且您不知道他们下一次何时可以更新连接,因此我将绕圈走。

I've been trying for several hours now to find something that works and can't believe it's not a simple thing to do and therefore I'm now throwing myself on the mercy of you fine coders to point me in the right direction before my boss hangs me from the first floor window. 我已经尝试了好几个小时,找到了行之有效的方法,并且不敢相信这并非一件容易的事,因此,我现在正全力以赴地请各位优秀的编码人员为我指出正确的方向,老板把我从一楼的窗户上吊下来。

Many Thanks 非常感谢


UPDATE 更新

Using what Clarence said as a starting point I came up with the following code in my appcache file: 以Clarence所说的作为起点,我在appcache文件中想到了以下代码:

 CACHE MANIFEST CACHE: /css/bootstrap.css /css/style.css /calendar.php NETWORK: /calendar.php # UPDATED: 03-04-2018 15:55:57 

What this does is caches the calendar.php file BUT if there is a connection then it has another look at those files under the NETWORK heading so I also put it in there. 它的作用是缓存Calendar.php文件,但如果存在连接,则它会再次查看NETWORK标题下的那些文件,因此我也将其放在了那里。 If the appcache files hasn't changed then the browser doesn't bother looking so I've used the following code to write to the file when a job has been altered: 如果appcache文件没有更改,则浏览器不会打扰您,因此在更改作业后,我使用以下代码将文件写入文件:

 $manifest = file_get_contents(__DIR__ . '/cache.appcache'); $newFile = substr($manifest, 0, (strpos($manifest, '# UPDATED: ') + 11)); $newFile = $newFile . date('dmY H:i:s'); file_put_contents('cache.appcache', $newFile); 

Basically just searches the file for "UPDATED" and inserts a new time thus updating the file and requiring a re-check from returning users. 基本上只在文件中搜索“ UPDATED”并插入新的时间,从而更新文件并需要返回用户的重新检查。

Somebody might point out this isn't the right way to do it but it seems to work from my tests so would like to thank those that contributed. 可能有人指出这不是正确的方法,但从我的测试来看似乎可行,因此要感谢那些贡献者。

well the best option would be creating a PWA. 最好的选择是创建PWA。 This should include the manifest files and Service workers as well. 这应该包括清单文件和服务工作者 It enables you to cache the content of the websites and update it once the connection has been reestablished. 它使您可以缓存网站的内容,并在重新建立连接后对其进行更新。 However it is very new and would require a decent amount of research into service workers. 然而,这是非常新的,需要对服务人员进行大量研究。 If you need any help regarding the development of PWA would be happy to help to an extent 如果您需要有关PWA开发的任何帮助,将在一定程度上乐于帮助

Have you tried changing the contents of your cache manifest whenever you change one of the files? 每当更改文件之一时,您是否尝试过更改缓存清单的内容? The APPCACHE is a bit finicky when it comes to changes in the files and can be troublesome to handle. 关于文件更改,APPCACHE有点挑剔,可能会很麻烦。 I usually include a comment with a timestamp and version number just to force it to update in the browser, like so: 我通常会在注释中添加时间戳和版本号,以强制其在浏览器中进行更新,如下所示:

CACHE MANIFEST
# 01-01-2001 v1.0 (Change whenever you need to force an update of the cache)

CACHE:
/css/file.css
/js/file.js


NETWORK:
*

FALLBACK:

The only way without HTTP-Header is to rename files continuously. 没有HTTP-Header的唯一方法是连续重命名文件。

And the depending HTML tag file-name. 以及相关的HTML tag文件名。

So the files are loaded afresh. 因此,文件将重新加载。



With HTTP-Header look here. 使用HTTP-Header请看这里。

How to control web page caching, across all browsers? 如何在所有浏览器中控制网页缓存?



You can do this whenever a new change in the calendar was made. 只要在日历中进行了新的更改,就可以执行此操作。

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

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