---
title: "Service worker: subdomains/wildcards "
url: "https://help.pushpushgo.com/en/article/service-worker-subdomains-wildcards"
language: "en"
last_updated: "2026-01-08T10:45:02.847Z"
---

# Service worker: subdomains/wildcards 

How to setup worker in one file for multiple subdomains

## Configuring a Single Service Worker for Multiple Projects

This guide explains how to configure one `sw.js` file to support **multiple PushPushGo projects** using **subdomain-based mapping**. This is useful when you want to serve different push notification configurations for various portals under the same root domain (e.g., `*.example.com`) **without generating a separate worker file for each project**.

### Use Case

You're managing several web portals like:

-   `aaa.example.com`
-   `bbb.example.com`

Each of these should receive push notifications from a **separate PushPushGo project**.

### Solution

Instead of dynamically generating `sw.js` for each subdomain, you can configure the service worker once and let it **dynamically import the correct PushPushGo worker script based on the current hostname**.📄 Example `sw.js` Implementation

```
// Map subdomains to PushPushGo project IDs
const idsMap = {
  'aaa.example.com': 'firstProjectId', // Replace with real ID
  'bbb.example.com': 'secondProjectId', // Replace with real ID
  // Add more domains as needed
};

let id = null;

// Determine the current subdomain
if (idsMap[self.location.hostname]) {
  id = idsMap[self.location.hostname];
}

if (id) {
  // Import the correct PushPushGo worker
  importScripts(`https://s-eu-1.pushpushgo.com/${id}/worker.js`);
} else {
  console.warn('[PushPushGo] No project ID configured for this domain:', self.location.hostname);
}
```

-   The service worker is **cached by browsers for up to 24 hours**, and it is re-fetched when:
-   -   The user first visits the site,
    -   A push notification is delivered,
    -   Or the browser refreshes the worker during its update cycle.
-   This solution ensures **separation of concerns** without server-side logic or full app bootstrapping.
-   If you manage a large number of subdomains, consider fetching the mapping from a remote JSON endpoint instead of hardcoding it.

### Debugging Tips

-   Use browser DevTools → **Application → Service Workers** to inspect the registration and activity of the worker.
-   Make sure the worker is served with proper `Content-Type: application/javascript` and over HTTPS.

### Advantages

-   Single worker script for all portals
-   No dynamic server-side rendering needed
-   Scalable and cache-efficient
-   Supports clean and secure push notification routing

### Need Help?

For support or integration questions, contact the PushPushGo team or your technical lead.
