Skip to content

Hosting your service worker

Push notifications need a small file called a service worker (sw.js) on your website. It's what lets a visitor's browser receive and show your notifications, even when your site isn't open.

Why it has to be on your site

Browsers only accept a service worker from the same website that asks visitors to subscribe. If your site is https://example.com, the file must be at https://example.com/sw.js. That's a browser security rule, so we can't host it for you.

The service worker:

  • receives notifications and shows them to your subscribers,
  • opens the right page when a subscriber clicks a notification,
  • keeps the subscription working when the browser renews it.

Set it up

  1. Go to Placements and open the Install action on your placement's row. (During sign-up, the onboarding wizard shows the same instructions.)

  2. Step 1 — copy the script snippet into your site's <head>. It looks like this:

    html
    <script src="https://cdn.dlserve.ai/sdk/YOUR-PLACEMENT-ID.js" async></script>
  3. Step 2 — click Download sw.js and upload the file to the top level of your website, so it opens at https://yoursite.com/sw.js.

    • Keep it at the root, not inside a folder like /js/.
    • The file name must stay exactly sw.js.
  4. Make sure your server sends it as JavaScript (Content-Type: application/javascript). Most hosts do this automatically for .js files.

The script snippet registers the service worker for you — there's no extra code to write.

Check that it works

The onboarding wizard's verification step checks your site for both pieces: the script snippet in your page and a reachable sw.js. If a check fails, fix the item it names and click Re-check. Troubleshooting covers the common causes.

Keep your copy up to date

The sw.js on your server is a copy, so it doesn't update itself when we improve it. Download a fresh copy and replace the old file when:

  • you regenerate your placement's keys, or
  • we tell you a service-worker update is available.

Browsers pick up the new file automatically on their next visit to your site.

Good to know

  • HTTPS is required: service workers only run on secure (https://) sites.
  • Content-Security-Policy: if your site sets a strict CSP header, it must allow dlserve's scripts and service worker — see Troubleshooting for the exact lines.
  • iPhone and iPad Safari don't support web push, so no service worker runs there.