# Run OneSignal and a PWA service worker together

URL: https://wp24x7.com/how-to-integrate-onesignal-with-pwa-in-wordpress-website/
Updated: 2026-11-29
Type: blog post

> OneSignal web push and a WordPress progressive web app both depend on a service worker, and a page can have only one per scope, so when both claim the whole site, one loses. Give OneSignal its own scope, or combine the two scripts deliberately. This developer guide shows how to see registrations, set separate scopes, test push and avoid common failures.

## Key facts

- For: developers running OneSignal push and a PWA on one WordPress site
- Cause: one service worker per scope; the latest registration wins
- Recommended: separate scopes, PWA at root, OneSignal on a sub-path
- Alternative: one worker importing the other with importScripts
- Needs: HTTPS, except localhost in development
- Before changing: back up, test on staging, check OneSignal's current docs
- Help: WordPress and plugin development

A progressive web app (PWA) and OneSignal web push both depend on a **service worker**, a script the browser runs in the background. **A page can have only one service worker per scope, so when both try to claim the whole site, one loses. The fix is to give OneSignal its own scope, or to combine the two scripts deliberately.**

> **Check current docs**
OneSignal and PWA plugins change often. Check OneSignal's current documentation for file names and options before copying any snippet, and test on staging.

This is a developer-level change. Back up the site, test on staging, and treat code as a child-theme or small-plugin job. The OneSignal plugin and SDK change often, so check OneSignal's current documentation for exact file names and options before you copy any snippet.

## Why do they conflict?

A service worker is registered with a **scope**: the part of the site it can control. A worker at `/sw.js` with the default scope controls everything under `/`. If the PWA registers that, and OneSignal also wants the root scope, the browser keeps one registration per scope. The most recent registration replaces the earlier one, so one feature silently stops working: the PWA loses offline caching, or push subscriptions fail with a service worker error.

Symptoms: the browser console shows service worker registration errors, the OneSignal prompt never appears, a subscription is created and then dropped, or **Application, Service Workers** in Chrome DevTools shows a different script than you expected.

## How do I see which workers are registered?

### DevTools
1. Open your site in Chrome, then DevTools, then the **Application** tab.
2. Choose **Service Workers**. Note each scope and script URL. Tick **Show all** if you have several.
3. Use **Unregister** while experimenting, so you test from a clean state.
### Console
Run this to list scopes:

```js
navigator.serviceWorker.getRegistrations().then(r => r.map(x => x.scope))
```

## What are the two main approaches?

| | Separate scopes | One worker imports the other |
|:--|:--|:--|
| Recommended | Yes | Only if you must share the root scope |
| Maintenance | Each product updates on its own | You maintain the combined file |
| Debugging | Simpler | Harder |

_Two service workers, each with its own scope_

**Approach 1: separate scopes (recommended).** Let the PWA keep the root scope and move OneSignal's worker to a sub-path that is not covered by it. OneSignal's current web SDK lets you set the worker path and scope in its init options, and by default it uses a dedicated path. When you use the OneSignal WordPress plugin, the plugin manages this for you, so first check its settings for options about the service worker.

If you initialise OneSignal yourself, the pattern looks like this. Replace `YOUR-APP-ID`, and place the OneSignal worker file, downloaded from OneSignal's setup documentation, on your own domain at the path given:

```html
<script src="https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js" defer></script>
<script>
	window.OneSignalDeferred = window.OneSignalDeferred || [];
	OneSignalDeferred.push(async function (OneSignal) {
		await OneSignal.init({
			appId: "YOUR-APP-ID",
			serviceWorkerPath: "push/onesignal/OneSignalSDKWorker.js",
			serviceWorkerParam: { scope: "/push/onesignal/" }
		});
	});
</script>
```

If the OneSignal plugin also initialises the SDK, you must turn that off, as the old approach on this blog did, so that only one init runs. Two inits cause duplicate prompts and errors.

To print the script from WordPress without editing theme templates, use a small plugin:

```php
add_action( 'wp_enqueue_scripts', function () {
	wp_enqueue_script(
		'onesignal-sdk',
		'https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js',
		array(),
		null,
		array( 'strategy' => 'defer', 'in_footer' => false )
	);

	wp_add_inline_script(
		'onesignal-sdk',
		'window.OneSignalDeferred = window.OneSignalDeferred || [];
		OneSignalDeferred.push(async function (OneSignal) {
			await OneSignal.init({
				appId: ' . wp_json_encode( 'YOUR-APP-ID' ) . ',
				serviceWorkerPath: "push/onesignal/OneSignalSDKWorker.js",
				serviceWorkerParam: { scope: "/push/onesignal/" }
			});
		});',
		'after'
	);
} );
```

`wp_json_encode` prints the ID as a safely quoted JavaScript string. The `strategy` argument needs WordPress 6.3 or later.

Because of the sub-scope, the OneSignal worker controls push events, which do not depend on scope for delivery, while your PWA worker still controls page caching.

**Approach 2: one worker that imports the other.** If you must use the root scope for both, your PWA's worker file can load OneSignal's worker as its first line, using a path from OneSignal's documentation:

```js
importScripts("https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.sw.js");
```

You then register only your worker and point OneSignal to it with its `serviceWorkerPath` option. This works, but you must now maintain the combined file and recheck it whenever either product updates. It is also harder to debug, so prefer separate scopes.

## Where must the files live?

A service worker file must be served from your own domain, with a JavaScript content type, and its path decides the maximum scope it can claim, unless the server sends a `Service-Worker-Allowed` header. That is why a worker uploaded to `/wp-content/plugins/...` can only control URLs under that folder. Putting the OneSignal worker in a sub-folder at the site root gives it a clean, predictable path.

## How do I test push after the change?

1. **Unregister** all workers for the site, clear site data, then reload the page.
2. **Confirm** in **Application, Service Workers** that both scopes are registered and active.
3. **Click** the OneSignal prompt (see [changing the prompt text](/how-to-customize-onesignal-prompt-text)) and allow notifications.
4. **Send** a test message to your own subscription from OneSignal's dashboard.
5. **Confirm** it arrives with the tab open and closed. Check that offline behaviour from the PWA still works.

## What commonly goes wrong?

| Problem | What to do |
|:--|:--|
| Caching plugins minify, combine or delay JavaScript | Exclude the OneSignal scripts from optimisation |
| Old workers cached by browsers | Users need a refresh or two before a new worker takes over |
| Mixed content or no HTTPS | Serve everything over HTTPS |
| Manifest warning | Separate from push; see [adding a short_name to the manifest](/how-to-add-short-name-to-pwa-manifest) |
| Plugin versions | Features have moved or been retired; check what your version still provides |

If you want this done and tested across browsers, our [WordPress development team](/wordpress-development) and [plugin developers](/wordpress-plugin-development) can do it.

**Want this built and tested across browsers?**
Our developers can do it. Post a project and a person will quote in writing.
[Post a project](/post-project)
[Plugin development](/wordpress-plugin-development)

## Frequently asked questions

### Why does OneSignal stop working after I add a PWA plugin?

Two service workers cannot control the same scope. If the PWA plugin registers one for the whole site, OneSignal's worker may be replaced or never registered, so push subscriptions fail.

### Can OneSignal and a PWA share one service worker?

Yes, by importing OneSignal's worker script into your own with importScripts, but that is more fragile than separate scopes, so prefer separate scopes first.

### Do I need HTTPS?

Yes. Service workers and push notifications need a secure origin, apart from localhost during development.

### Why does OneSignal stop working after I add a PWA plugin?

Two service workers cannot control the same scope. If the PWA plugin registers one for the whole site, OneSignal's worker may be replaced or never registered, so push subscriptions fail.

### How do I see which service workers are registered?

In Chrome, open DevTools, Application, Service Workers and note each scope and script URL. In the console, navigator.serviceWorker.getRegistrations lists scopes. Use Unregister while experimenting to test from a clean state.

### What are the symptoms of a conflict?

Registration errors in the console, a OneSignal prompt that never appears, a subscription created then dropped, or a different script listed in DevTools than you expected.

### Which approach should I prefer?

Separate scopes. Let the PWA keep the root scope and move OneSignal's worker to a sub-path. Importing one worker into the other works but you must maintain the combined file and recheck it whenever either product updates.

### Why does the worker file location matter?

A worker must be served from your own domain with a JavaScript content type, and its path decides the maximum scope unless the server sends a Service-Worker-Allowed header. A worker under a plugin folder can only control URLs under that folder.

### Can I let the OneSignal plugin and my own init both run?

No. Turn off the plugin's init if you initialise OneSignal yourself, so only one init runs. Two inits cause duplicate prompts and errors.

### Do I need HTTPS?

Yes. Service workers and push notifications need a secure origin, apart from localhost during development.

### How do I test push after the change?

Unregister all workers and clear site data, reload, confirm both scopes are active, allow notifications, send a test message from OneSignal's dashboard, confirm it arrives with the tab open and closed and check PWA offline behaviour.

### What commonly breaks it?

Caching plugins that minify, combine or delay the scripts, old workers cached by browsers that need a refresh or two, mixed content, and plugin versions that have moved or retired features.

### Does a manifest warning stop push notifications?

No. Manifest and notification permissions are separate, so a manifest warning does not stop push.
