如何防止WebApp、Flutter for Web中的缓存?

问题描述 投票:0回答:1

我们有一个 Flutter WebApp,在每个部署中用户都必须删除缓存,因此,任务是防止缓存,因此每个部署都必须让最终用户在开始工作之前没有不必要的步骤......

i have tried adding the line: 
entrypointUrl: "main.dart.js?v=" + serviceWorkerVersion`

但是当我这样做时,在部署中我收到错误:

Exception while loading service worker: Error: Service worker not supported (or configured). at FlutterServiceWorkerLoader.loadServiceWorker (flutter.js:122:11) at FlutterLoader.loadEntrypoint (flutter.js:358:33) at VM26 login-one:4:23 Unexpected token '<' in the main.dart.js?v=(serviceWorker_Number)

导致页面在首页充电时卡住。 这是html代码:

`<!DOCTYPE html>
<html>

<head>

  <base href="$FLUTTER_BASE_HREF">

  <meta charset="UTF-8">
  <meta content="IE=Edge" http-equiv="X-UA-Compatible">
  <meta name="description" content="A new Flutter project.">

  <!-- iOS meta tags & icons -->
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="apple-mobile-web-app-title" content="admin_dashboard">
  <link rel="apple-touch-icon" href="icons/Icon-192.png">

  <!-- Favicon -->
  <link rel="icon" type="image/png" href="SU-VAN-Vertical.png" />

  <title>Su-Van</title>
  <link rel="manifest" href="manifest.json">

  <script src="https://maps.googleapis.com/maps/api/js?key=api_serialString_iJustChangeIt_for_default_text"></script>

  <script>
    // The value below is injected by flutter build, do not touch.var serviceWorkerVersion = null;
  </script>
  <!-- This script adds the flutter initialization JS code -->
  <script src="flutter.js" defer></script>
</head>

<body>


  <div id="loading_indicator" class="container" style="height: 100vh;; display:flex; justify-content: center; align-items: center;">
    <img class="indicator" src="./assets/Ripple.gif" />

  </div>

  <script>
    window.addEventListener('load', function (ev) {
      // Download main.dart.js
      _flutter.loader.loadEntrypoint({
        **entrypointUrl: "main.dart.js?v=" + serviceWorkerVersion,**
        serviceWorker: {
          serviceWorkerVersion: serviceWorkerVersion,
        }
      }).then(function (engineInitializer) {
        return engineInitializer.initializeEngine();
      }).then(function (appRunner) {
        return appRunner.runApp();
      });
    });

  </script>
</body>

</html>`

我在评论“entryPointUrl”行后到达主页正常启动,但仍然收到错误:

Exception while loading service worker: Error: Service worker not supported (or configured). at FlutterServiceWorkerLoader.loadServiceWorker (flutter.js:122:11) at FlutterLoader.loadEntrypoint (flutter.js:358:33) at VM26 login-one:4:23

顺便说一句,这不是一个解决方案,因为浏览器不断收取缓存,任何人都知道我如何解决这个错误,然后完成任务,那就是让浏览器下载新的 main.dart 文件(如果有新的)应用程序的版本?

• Flutter version 3.10.6 on channel stable at C:\src\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision f468f3366c (7 weeks ago), 2023-07-12 15:19:05 -0700 • Engine revision  • Dart version 3.0.6 • DevTools version 2.23.1

flutter dart caching web-applications progressive-web-apps
1个回答
0
投票

我也遇到过同样的问题。

这个链接中的简单方法对我有帮助,所以我想分享它。
Flutter Web:仅在第二次重新加载后加载应用程序更新

正如本文所说,您需要做的就是在 web/index.html 中的 flutter.js 后面写入 ?version=xxxx

    <head>
     ......
      <!-- This script adds the flutter initialization JS code -->
      <script src="flutter.js?version=1.0.3" defer></script>
    </head>

每次部署时,我都会利用 Windows 批处理文件 (.bat) 将其重写为最新版本。

© www.soinside.com 2019 - 2024. All rights reserved.