# Subdomains / Wildcards

## 🛠️ 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

```js
// 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);
}
```

***

### 🔒 Notes

* 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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.pushpushgo.company/web-push/service-worker/subdomains-wildcards.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
