在缓存清单文件 HTML5 的 FALLBACK 部分中使用通配符

发布于 2024-09-19 22:17:08 字数 138 浏览 3 评论 0原文

如何创建一个支持离线的 Web 应用程序,以便 当用户访问 hxxp://mywebsite/ 并且离线时,会显示 hxxp://mywebsite/offline/ 。 [我的网站中有大约 100 个不同的动态页面,因此我无法将所有页面硬编码到缓存清单文件中]

How to create an offline enabled web-application such that
when user visits hxxp://mywebsite/ and is offline than hxxp://mywebsite/offline/ is displayed. [There are about 100 different dynamic pages in my website, so I cannot afford to hardcode all of them in the cache manifest file]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清醇 2024-09-26 22:17:08

我引用“manifest.php”而不是“cache.manifest”,然后我的 php 文件如下所示:

<?php
    header('Content-Type: text/cache-manifest');
    echo "CACHE MANIFEST\n";

    $hashes = "";

    $dir = new RecursiveDirectoryIterator(".");
    foreach(new RecursiveIteratorIterator($dir) as $file) {
        $info = pathinfo($file);
        if ($file->IsFile() &&
            $file != "./manifest.php" &&
            substr($file->getFilename(), 0, 1) != ".")
        {
            echo $file . "\n";
            $hashes .= md5_file($file);
        }
    }

    echo "# Hash: " . md5($hashes) . "\n";

?>

文件哈希使其保持最新,以便如果任何文件更改,清单也会更改。希望有帮助:)

I reference "manifest.php" instead of "cache.manifest", then my php file looks like this:

<?php
    header('Content-Type: text/cache-manifest');
    echo "CACHE MANIFEST\n";

    $hashes = "";

    $dir = new RecursiveDirectoryIterator(".");
    foreach(new RecursiveIteratorIterator($dir) as $file) {
        $info = pathinfo($file);
        if ($file->IsFile() &&
            $file != "./manifest.php" &&
            substr($file->getFilename(), 0, 1) != ".")
        {
            echo $file . "\n";
            $hashes .= md5_file($file);
        }
    }

    echo "# Hash: " . md5($hashes) . "\n";

?>

The file hashes keep it up-to-date so that if any files change the manifest changes as well. Hope that helps :)

℡寂寞咖啡 2024-09-26 22:17:08
CACHE MANIFEST
CACHE:
/Offline/OfflineIndex.html

FALLBACK:
/ /Offline/OfflineIndex.html

NETWORK:
*

这将导致整个网站上的所有页面在离线时重定向到离线状态。唯一的问题是声明清单的页面,因为该页面始终被缓存。这意味着您无法在每个页面上声明清单,因为每个访问的页面都会自行缓存而不是重定向。因此,您可以做的是在另一个 html 文件(即 Synchronize.html)上声明您的清单,然后通过存储 cookie 或本地缓存值默认检查您的应用程序是否可以离线使用。如果没有重定向到带有声明的清单的synchronize.html,请设置localcache值,然后重定向回index。

离线很棒SSSSSSSSSS!!!

CACHE MANIFEST
CACHE:
/Offline/OfflineIndex.html

FALLBACK:
/ /Offline/OfflineIndex.html

NETWORK:
*

This will cause all your pages across the entire site to redirect to offline when offline. The only issue is with the page that declares the manifest as that page is always cached. This means you cannot declare the manifest on every page because every visited page will then be cached itself and not redirect. So what you can do is declare your manifest on another html file (IE. Synchronize.html) then from default check whether or not your app has been made available for offline by storing a cookie or localcache value. If not redirect to synchronize.html with the manifest declared, set the localcache value, and redirect back to index.

OFFLINE AWESOMENESSSSSSSSSSS!!!!

赏烟花じ飞满天 2024-09-26 22:17:08

不可能在缓存清单中使用通配符,至少据我所知,它在任何当前浏览器中都不起作用。另一种方法可能是动态生成缓存清单,并让脚本生成所有这些后备条目。

It's not possible to use wildcards in the cache manifest, at least it doesn't work in any current browser as far as I'm aware. An alternative approach might be to generate your cache manifest dynamically, and let a script generate all those fallback entries.

我只土不豪 2024-09-26 22:17:08

在索引页面的不可见 iframe 中引用您的清单文件。这样,您的索引页就不会像通常默认情况那样被缓存,并且您可以完全控制后备...

不需要不可靠的 cookie 或 localStorage!

Reference your manifest file in an invisible iframe in your index page. That way your index page isn't cached as it is normally by default and you have total control over your fallbacks...

No need for unreliable cookies or localStorage!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文