Run OneSignal and a PWA service worker together
OneSignal push and a WordPress PWA both want a service worker. Learn how scope works, how to give each its own, and how to test that push still works.
At a glance
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.
Updated November 29, 2026
- 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
On this page
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.
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?
- Open your site in Chrome, then DevTools, then the Application tab.
- Choose Service Workers. Note each scope and script URL. Tick Show all if you have several.
- Use Unregister while experimenting, so you test from a clean state.
Run this to list scopes:
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 |
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:
<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:
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:
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?
- Unregister
all workers for the site, clear site data, then reload the page.
- Confirm
in Application, Service Workers that both scopes are registered and active.
- Click
the OneSignal prompt (see changing the prompt text) and allow notifications.
- Send
a test message to your own subscription from OneSignal's dashboard.
- 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 |
| 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 and plugin developers can do it.
Our developers can do it. Post a project and a person will quote in writing.
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.
Link to this answerDo I need HTTPS?
Yes. Service workers and push notifications need a secure origin, apart from localhost during development.
Link to this answerWhy 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.
Link to this answerHow 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.
Link to this answerWhat 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.
Link to this answerWhich 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.
Link to this answerWhy 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.
Link to this answerCan 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.
Link to this answerDo I need HTTPS?
Yes. Service workers and push notifications need a secure origin, apart from localhost during development.
Link to this answerHow 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.
Link to this answerWhat 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.
Link to this answerDoes a manifest warning stop push notifications?
No. Manifest and notification permissions are separate, so a manifest warning does not stop push.
Link to this answerKeep reading
Want an engineer on it?
Tell us what is wrong in plain words. A person reads it, today.