繁体   English   中英

Firebase 托管 Flutter Web App 未清除首次部署的缓存

[英]Firebase Hosting Flutter Web App not clearing Cache of first deploy

我们构建了一个 flutter web 应用程序并通过 firebase 托管部署了它。 不幸的是,我们没有在初始部署中配置任何缓存设置。

现在我们部署了一个更新版本的网站,但人们仍然会看到第一次部署时显示的旧网站。 到目前为止我们尝试了什么:

添加版本号到我们的 index.html:

<"script src="main.dart.js?version=1" type="application/javascript"></script>

在 index.html 的标题中添加元数据:

  <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
  <meta http-equiv="Pragma" content="no-cache" />
  <meta http-equiv="Expires" content="0" />

在我们的 firebase.json 中,我们添加了以下标头:

"headers": [
      {
        "source": "**",
        "headers": [
          {
            "key": "Cache-Control",
            "value": "max-age=10"
          }
        ]
      }
    ]

所有这些尝试都没有任何运气。 我们认为问题在于较新版本的文件中没有这些条目。 我们如何强制它更新到我们的最新版本? 如果可能有帮助,我们甚至会考虑开设一个新的 firebase 项目。

尝试这个:

在您的 flutter 应用程序中,在 web 文件夹内的 index.html 中,在src="main.dart.js之后添加版本号

因此,您的新行将如下所示:

<script src="main.dart.js?version=1" type="application/javascript"></script>

然后在每次构建之前将版本号增加到 2 等。

2021 年 6 月更新:在 index.html 中添加这一行作为正文中的最后一个脚本

如果您不想使用服务器工作者缓存,您可以提供--pwa-strategy=none 否则,应用程序将在下载新版本并重新访问页面后更新。

例子:

flutter build web --release --pwa-strategy=none

它将生成一个没有身体的服务人员。 这对于本地测试或不需要服务工作者缓存功能的情况很有用

默认情况下,它设置为offline-first :尝试缓存应用程序 shell,然后在加载所有后续资产时延迟缓存它们。 在对资产进行网络请求时,将首选离线缓存。

参考:

我看到 Flutter 现在默认情况下在 index.html 中包含此脚本,如果最近创建的项目具有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();
}

当你构建 flutter web 时,然后在目录 build/web 中生成一个 version.json 文件。 只需增加此文件中的build_number版本

暂无
暂无

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

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