在flutter v3.0.1 Web上强制缓存刷新

xxhby3vn  于 2023-05-23  发布在  Flutter
关注(0)|答案(2)|浏览(188)

在当前版本的flutter 3.0.1中,我们在index.html中有一个service worker。到目前为止,我在www.example.com上找不到任何文档flutter.dev,说明如何在屏幕或代码更新后强制刷新缓存。刷新的唯一方法是使用浏览器刷新按钮等。
在SO上有很多过时的建议,可以通过在这里或那里添加一些内容来手动更改版本号,但这并不适用于serviceworker。服务工作者在运行flutter build web时会自动获得一个新的版本号,每次生成。但是,这不会强制在浏览器中刷新缓存。
默认情况下,此service worker js监听statechange,然后调用console.log('Installed new service worker.');。只有当您点击浏览器的刷新按钮时才会调用,因此提示或强制刷新新内容是没有帮助的。
我尝试使用flutter build web --pwa-strategy=none,这也不会影响缓存。
有什么好主意可以让你重新振作吗?在我构建的其他网站中,对css/js文件进行版本化非常容易,这将在没有任何用户交互的情况下强制刷新,但我没有看到任何明确的使用flutter build web的路径。

web/index.html中serviceWorker js

运行flutter build web后,var serviceWorkerVersion = null将变成类似于var serviceWorkerVersion = '1767749895';的东西,当部署到托管和在web上运行。但是,此serviceWorkerVersion更新不强制刷新内容。

<script>
    var serviceWorkerVersion = null;
    var scriptLoaded = false;
    function loadMainDartJs() {
      if (scriptLoaded) {
        return;
      }
      scriptLoaded = true;
      var scriptTag = document.createElement('script');
      scriptTag.src = 'main.dart.js';
      scriptTag.type = 'application/javascript';
      document.body.append(scriptTag);
    }

    if ('serviceWorker' in navigator) {
      // Service workers are supported. Use them.
      window.addEventListener('load', function () {
        // Wait for registration to finish before dropping the <script> tag.
        // Otherwise, the browser will load the script multiple times,
        // potentially different versions.
        var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
        navigator.serviceWorker.register(serviceWorkerUrl)
          .then((reg) => {
            function waitForActivation(serviceWorker) {
              serviceWorker.addEventListener('statechange', () => {
                if (serviceWorker.state == 'activated') {
                  console.log('Installed new service worker.');
                  loadMainDartJs();
                }
              });
            }
            if (!reg.active && (reg.installing || reg.waiting)) {
              // No active web worker and we have installed or are installing
              // one for the first time. Simply wait for it to activate.
              waitForActivation(reg.installing || reg.waiting);
            } else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
              // When the app updates the serviceWorkerVersion changes, so we
              // need to ask the service worker to update.
              console.log('New service worker available.');
              reg.update();
              waitForActivation(reg.installing);
            } else {
              // Existing service worker is still good.
              console.log('Loading app from service worker.');
              loadMainDartJs();
            }
          });

        // If service worker doesn't succeed in a reasonable amount of time,
        // fallback to plaint <script> tag.
        setTimeout(() => {
          if (!scriptLoaded) {
            console.warn(
              'Failed to load app from service worker. Falling back to plain <script> tag.',
            );
            loadMainDartJs();
          }
        }, 4000);
      });
    } else {
      // Service workers not supported. Just drop the <script> tag.
      loadMainDartJs();
    }
  </script>
insrf1ej

insrf1ej1#

你试过这个吗?在index.html中添加no-cache。看起来像在玩Flutter 3.0。请记住,这样做您的pwa将无法在离线模式下工作。

<head>
...
    <meta http-equiv="cache-control" content="no-cache" />
    <meta http-equiv="expires" content="0" />
    <meta http-equiv="pragma" content="no-cache" />
...
</head>
wqsoz72f

wqsoz72f2#

您可以使用Firestore监听器真实的访问特定文档或Firebase Remote Config,以检查是否需要清除该高速缓存。
一旦它需要清除网页缓存,只需运行:

import 'dart:html' as html show window;
html.window.location.reload();

https://firebase.google.com/docs/remote-config

相关问题