簡體   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