向现有工作箱工作器注册 Firebase 服务工作器

Arc*_*her 5 firebase vue.js service-worker angular workbox

我将 Google 的Workbox用于一些离线功能并在我的 angular 9 应用程序中进行缓存。对于推送消息,我们使用 Google 的Firebase

自从将 Web 应用程序更新为 Vue.js 后,我在访问网站时收到错误消息:

未捕获的 FirebaseError: Messaging: 我们无法注册默认的 Service Worker。未能注册为范围ServiceWorker(” https://my-domain.com/firebase-cloud-messaging-push-scope与脚本‘)(’ https://my-domain.com/firebase-messaging-sw。 js '):脚本具有不受支持的 MIME 类型 ('text/html')。(消息/失败的服务工作人员注册)。

是的,根目录下没有firebase-messaging-sw.js
一切都应该由 Workbox 处理。

我按照文档使用现有的服务工作者进行注册useServiceWorker(),但这会导致上述错误。

import * as firebase from 'firebase/app';
import 'firebase/messaging';
import { Workbox } from 'workbox-window';
import { environment } from './environments/environment';

...

if ('serviceWorker' in navigator && environment.production) {
  const scope = window.location.origin + window.location.pathname.split('/').slice(0, 3).join('/');
  const wb = new Workbox('/my-worker.js', { scope: scope });

  wb.register().then(reg => {
    if (!firebase.apps.length) {
      firebase.initializeApp(environment.firebase);
    }
    if (firebase.messaging.isSupported()) {
      firebase.messaging().useServiceWorker(reg);
    }
    reg.update();
  }).catch(err => console.log(err));
}
Run Code Online (Sandbox Code Playgroud)

我需要firebase-messaging-sw.js吗?