To add web push to an existing service worker, put the push and notificationclick handlers into the worker you already register, or load a push script inside it with importScripts(). Don’t register a second service worker at the same scope, because only one can control a scope and the new one replaces your existing worker, breaking caching or push.
This is one of the most common setup problems I see on sites that already run a PWA, an offline cache or a Workbox build. A push tool asks you to upload its own service worker file, and suddenly the offline mode stops working, or push stops after the next deploy. Here’s how to combine web push with an existing service worker cleanly.
Key facts at a glance:
- One per scope: a scope, such as /, has one active service worker. Registering a different file at the same scope swaps it in. See MDN on ServiceWorkerContainer.register().
- importScripts(): a classic service worker can load extra scripts, which is the simplest way to add push code.
- Workbox: the generateSW mode has an importScripts option, and injectManifest lets you write your own worker. See Workbox’s build docs.
- Firebase: getToken() accepts a serviceWorkerRegistration option, so FCM can use your existing worker. See Firebase’s GetTokenOptions reference.
Why can’t you just add a second service worker?
You can’t add a second service worker at the same scope because the browser keeps one registration per scope. When you register a different script URL there, the browser treats it as an update and replaces the old worker.
So if your PWA registers /sw.js and a push tool registers /push-sw.js, both at the root scope, they fight. Whichever registers last wins. The loser’s code, whether offline caching or the push handler, stops running.
| Approach | Works? | Problem |
|---|---|---|
| Two workers, same scope | No | The last one registered replaces the other |
| Two workers, different scopes | Partly | Push only works for pages in the push worker’s scope |
| One worker with all handlers | Yes | You maintain one file |
| One worker plus importScripts() | Yes | Cleanest when the push code comes from a vendor |
Different scopes can work in narrow cases. For example, Firebase’s default worker uses its own scope. But a push worker registered under a sub-path can’t control your main pages, so prompts and clicks get messy. One worker per site is easier to reason about.
How do you merge push code into your service worker?
Add a push listener and a notificationclick listener to the service worker you already have. Your fetch and cache logic stays exactly as it is, since each event type has its own listener.
// existing code stays: install, activate, fetch...
self.addEventListener('push', (event) => {
const data = event.data ? event.data.json() : {};
event.waitUntil(
self.registration.showNotification(data.title || 'Update', {
body: data.body,
icon: data.icon,
data: { url: data.url || '/' }
})
);
});
self.addEventListener('notificationclick', (event) => {
event.notification.close();
event.waitUntil(clients.openWindow(event.notification.data.url));
});Multiple listeners for the same event are allowed, but avoid two push handlers that both show a notification. You’d get duplicates. My guide to the service worker for web push explains each event in more depth.
How do you load a vendor’s push script with importScripts()?
If your push provider gives you a service worker file, load it from your own worker with importScripts() instead of registering it separately. The vendor’s handlers then run inside your single worker.
- Keep your existing worker, such as /sw.js, as the only registered file.
- Host the vendor’s push script on your own domain, for example /push-handler.js.
- Add importScripts(‘/push-handler.js’); at the top of /sw.js.
- Remove any separate navigator.serviceWorker.register() call for the vendor file.
- Deploy, then confirm in DevTools that one worker is active and both caching and push work.
Note that importScripts() only works in classic service workers. If your worker is registered with type: ‘module’, use a normal import statement instead.
Before you do this, ask your vendor whether they support it. Some tools expect their own file name at a fixed path. If you rename it, update their settings too.
How do you add push to a Workbox service worker?
With Workbox, use importScripts in generateSW mode or switch to injectManifest. Both keep Workbox’s precaching and add your push code.
generateSW
The generateSW mode builds the whole worker for you. Its importScripts option adds your push script to the generated file:
// workbox-config.js
module.exports = {
globDirectory: 'dist/',
globPatterns: ['**/*.{html,js,css}'],
swDest: 'dist/sw.js',
importScripts: ['push-handler.js']
};injectManifest
The injectManifest mode lets you write your own worker, and Workbox only injects the precache list. Put your push and notificationclick listeners in that source file. I prefer this mode once a site has custom logic, because everything sits in one readable file.
How do you make Firebase use your existing worker?
Pass your own registration to getToken(). By default, the Firebase JS SDK looks for /firebase-messaging-sw.js. However, you can give it the registration of the worker you already use.
const reg = await navigator.serviceWorker.register('/sw.js');
const token = await getToken(messaging, {
vapidKey: 'YOUR_PUBLIC_VAPID_KEY',
serviceWorkerRegistration: reg
});Then add the Firebase messaging code, or your own push handler, inside /sw.js. For the bigger picture of Firebase setup, see how to connect Firebase to self-hosted web push.
Will existing subscribers break when you merge workers?
They can, if the merge changes the scope or the keys. A push subscription belongs to a service worker registration, so moving push to a different scope can leave old subscriptions without a handler.
Keep these the same during a merge:
- Scope: keep the registration that holds the subscriptions, usually at /.
- VAPID keys: keep the same application server key, or subscriptions must be recreated. See my guide to VAPID keys.
- Origin: stay on the same domain.
If you must change the file name or path, read what happens when you change the service worker file first. Then test on a staging copy before release.
How do you test the merged worker?
Test caching and push together, because a merge that fixes one can break the other. Chrome DevTools shows the active worker and lets you send a test push.
- Open DevTools, then Application, then Service Workers.
- Confirm one active worker at the expected scope, with the expected file name.
- Click Push to fire a test event and check that a notification appears.
- Go offline in the Network panel and reload to check that caching still works.
- Send a real message from your server or panel to a test device.
My guide to debugging web push in Chrome DevTools covers these screens step by step.
Where LaraPush fits
LaraPush is a self-hosted web push panel that you install on your own VPS, with a one-time licence from $499 (check LaraPush pricing). Setup involves embedding its JavaScript and service worker on your site, or using the WordPress plugin. If your site already has a PWA or Workbox worker, read the LaraPush guide and ask support how its worker file should sit alongside yours before going live. The trade-offs are no free plan and a server you maintain. The features page lists what the panel includes.
Frequently asked questions
A site can have several registrations, but each scope has only one active service worker. Two workers at the same scope replace each other. Workers at different scopes can coexist, but each only controls pages inside its own path, which makes push and caching harder to manage.
Not noticeably. Push and notificationclick listeners only run when those events fire, so they don’t affect page loads or fetch handling. The main cost is the extra script download the first time the worker installs or updates.
No. importScripts() only works in classic service workers. If you register your worker with type module, use standard import statements to bring in the push code instead, and check that your target browsers support module service workers.
The PWA plugin probably registered its own service worker at the root scope, replacing your push worker. Merge both into one file, or use the plugin’s option to import extra scripts, then confirm in DevTools that one worker handles both caching and push.
No. That’s only the default file name the Firebase SDK looks for. Pass your own service worker registration to getToken() with the serviceWorkerRegistration option, and put the messaging or push handler code in your existing worker.




