【问题标题】:The script has an unsupported MIME type ('text/html')该脚本具有不受支持的 MIME 类型 ('text/html')
【发布时间】:2021-06-12 22:04:16
【问题描述】:

我正在尝试为 Flutter Web 平台设置 Firebase Push。

我复制了与各种教程中显示的相同的设置并遇到以下错误,

Uncaught (in promise) Error: [firebase_messaging/failed-service-worker-registration] Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker for scope ('http://localhost:61027/firebase-cloud-messaging-push-scope') with script ('http://localhost:61027/firebase-messaging-sw.js'): The script has an unsupported MIME type ('text/html')

这是我的 Index.html 的摘录

<script>
    if ('serviceWorker' in navigator) {
      window.addEventListener('flutter-first-frame', function () {
        navigator.serviceWorker.register('/flutter_service_worker.js')
        .then(function(registration) {
      // Registration was successful
      console.log('ServiceWorker registration successful with scope: ', registration.scope);
    }, function(err) {
      // registration failed :(
      console.log('ServiceWorker registration failed: ', err);
    })
      });
 }
</script>
<script src="main.dart.js" type="application/javascript"></script>

Index.html 和 flutter_service_worker.js 在同一个文件夹中。

我收到了成功的服务注册消息。收到推送权限后,我正在使用以下代码使用以下代码获取令牌,然后出现错误。

getIt<FirebaseMessaging>().getToken(vapidKey:"xxx");

我遇到了上述错误。

任何帮助或指导将不胜感激。

【问题讨论】:

    标签: flutter firebase-cloud-messaging flutter-web


    【解决方案1】:

    messaging.useServiceWorker(registration); 是解决此问题的唯一方法。

    我从 Another Stackoverflow Question 找到了解决方案,但是 cmets 说 useServiceWorker 已被弃用,这就是我不倾向于使用它的原因,但我没有看到任何其他选项可以解决这个问题时刻。 这是我的完整注册信息。

    const messaging = firebase.messaging();
    navigator.serviceWorker.register('/firebase-message-sw.js')
              .then(function (registration) {
                // Registration was successful
                console.log('firebase-message-sw :ServiceWorker registration successful with scope: ', registration.scope);
                messaging.useServiceWorker(registration);
              }, function (err) {
                // registration failed :(
                console.log('firebase-message-sw: ServiceWorker registration failed: ', err);
              });
      });
    

    【讨论】:

    • in index.html inside body -> script 标签。
    • 我把这段代码放在哪里?
    【解决方案2】:

    将您的firebase-messaging-sw.js 放在 index.html 所在的根目录中,并确保 你的``firebase-messaging-sw.js 是空的,它不包含任何代码 这适用于 Firestore v9

    【讨论】:

      猜你喜欢
      • 2021-05-29
      • 2019-02-15
      • 2019-12-03
      • 2020-11-05
      • 2019-08-06
      • 1970-01-01
      • 2020-05-31
      • 2020-09-20
      • 2019-09-16
      相关资源
      最近更新 更多