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);
}

πŸ”’ 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.

Last updated

Was this helpful?