firebase-messaging-sw.js throws a DOMException on importScripts
A 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.
Fix it fast
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.js2Match what you see, then jump to its fix
- 1
Content-Type is text/html, or the status is 404 with your app's error pageThe server answers with HTML instead of JavaScriptFix - 2
The 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
TypeError about importScripts in a module workerThe worker is registered as a module, or bundled code uses importFix
- 1
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 redeploy3Verify
The request returns 200 with a text/javascript content type.
curl -sI https://example.com/firebase-messaging-sw.jsConfirmed by developer.mozilla.org
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.js2Fix
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 WorkersConfirmed by firebase.google.com
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'" src2Fix
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 WorkersConfirmed by firebase.google.com
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:
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
Related fixes
Web apps
Attempted import error: firebase/app has no default export
Attempted import error: 'firebase/app' does not contain a default export (imported as 'firebase').
Since the modular SDK (version 9), firebase/app no longer exports one firebase object.
3 causes, each with a fixWeb apps
Firebase Auth says the API key is not valid
Firebase: Error (auth/api-key-not-valid.-please-pass-a-valid-api-key.)
The apiKey your app sends is either empty, wrong, or blocked.
4 causes, each with a fixWeb apps
Firebase sign-in fails with 'missing initial state' after a redirect
This may happen if browser sessionStorage is inaccessible or accidentally cleared.
The sign-in page is opened on one domain and the result is read back from another.
3 causes, each with a fixWeb 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 fixWeb apps
SyntaxError in firebase-messaging-sw.js: import statement outside a module
Uncaught SyntaxError: Cannot use import statement outside a module (at firebase-messaging-sw.js:1:1)
A service worker is a classic script, so an import line in firebase-messaging-sw.js is a syntax error.
2 causes, each with a fixWeb 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