Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

firebase-messaging-sw.js throws a DOMException on importScripts

ErrorA DOMException from importScripts in the service worker (NetworkError, SyntaxError or TypeError)

Short answer

importScripts throws a NetworkError when a script is served without a JavaScript MIME type, a SyntaxError when a URL cannot be resolved, and a TypeError when the worker is a module. For Firebase Messaging, check that firebase-messaging-sw.js is served from the domain root as JavaScript and that the gstatic URLs are valid.

  • Applies to Chrome, Firebase JS SDK 13, firebase-messaging-sw.js
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Request the worker file and each imported script from the command line and read the status and Content-Type headers.

    curl -sI https://example.com/firebase-messaging-sw.js
    curl -sI https://www.gstatic.com/firebasejs/13.0.0/firebase-messaging-compat.js
  2. 2Match what you see, then jump to its fix

    1. 1Content-Type is text/html, or the status is 404 with your app's error pageThe server answers with HTML instead of JavaScriptFix
    2. 2The error appears on a specific line in the worker; a relative path is usedA script URL inside the worker cannot be resolved or loadedFix
    3. 3TypeError about importScripts in a module workerThe worker is registered as a module, or bundled code uses importFix

What does the error look like?

  • A DOMException from importScripts in the service worker (NetworkError, SyntaxError or TypeError)

Cause 1 of 3

The server answers with HTML instead of JavaScript

1Check

A missing file that falls through to an index.html rewrite is served as HTML, and importScripts refuses scripts without a JavaScript MIME type. FCM expects the file named firebase-messaging-sw.js in the root of your domain.

curl -sI https://example.com/firebase-messaging-sw.js | grep -i -E "^HTTP|content-type"

2Fix

Put firebase-messaging-sw.js in the public folder so it is served at the root, and exclude it from SPA rewrite rules.

# e.g. place the file at public/firebase-messaging-sw.js, then redeploy

3Verify

The request returns 200 with a text/javascript content type.

curl -sI https://example.com/firebase-messaging-sw.js

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 3

A script URL inside the worker cannot be resolved or loaded

1Check

Relative URLs resolve against the worker script's own URL, not the page. The documented Firebase pattern uses two compat scripts from gstatic with the SDK version in the path.

grep -n importScripts public/firebase-messaging-sw.js

2Fix

Use absolute URLs for the Firebase scripts, with a current SDK version, and load app before messaging.

importScripts('https://www.gstatic.com/firebasejs/13.0.0/firebase-app-compat.js');
importScripts('https://www.gstatic.com/firebasejs/13.0.0/firebase-messaging-compat.js');

3Verify

Unregister the old worker in DevTools, reload and check that it activates.

# DevTools > Application > Service Workers

Confirmed by firebase.google.com

Did this fix it?

Cause 3 of 3

The worker is registered as a module, or bundled code uses import

1Check

importScripts cannot be used when the worker is a module. The Firebase docs say the modular SDK needs a bundled worker file because ES modules in workers are not widely supported.

grep -rn "type: 'module'" src

2Fix

Either keep the classic worker with importScripts and the compat packages, or bundle a modular worker that imports from firebase/messaging/sw.

import { initializeApp } from 'firebase/app';
import { getMessaging, onBackgroundMessage } from 'firebase/messaging/sw';

3Verify

Reload the page and confirm the worker status is activated.

# DevTools > Application > Service Workers

Confirmed by firebase.google.com

Did this fix it?

How do I stop it coming back?

  • Keep firebase-messaging-sw.js in a folder served as static files and out of SPA rewrites.
  • Pin the SDK version in the importScripts URLs and update it with the main app.
  • Check the worker's response headers after each deploy.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

Google sign-in popup blocked by Cross-Origin-Opener-Policy

The page loses its reference to the sign-in popup window, because the opener policy severs it

If your site sends Cross-Origin-Opener-Policy: same-origin, a sign-in popup from another origin lands in a separate browsing context group, so your page cannot reach it.

3 causes, each with a fix

Web apps

Browser blocks a Firebase Storage request at the CORS preflight

Response to preflight request doesn't pass access control check: It does not have HTTP ok status

A browser sends an OPTIONS preflight before some cross-origin requests, and Cloud Storage answers it only from the CORS configuration on the bucket.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.