Skip to content

Firebase is setting always default root for service worker at "/" directory #9096

Open
@KaloyanGG

Description

@KaloyanGG

Operating System

Windows 11

Environment (if applicable)

Chrome 137.0.7151.69 (Official Build) (64-bit)

Firebase SDK Version

11.8.1

Firebase SDK Product(s)

Messaging

Project Tooling

Simple static html + js website with a default firebase service worker - nothing more nothing less

Detailed Problem Description

I was trying to fetch a token by a vapidkey from my firebase app, but getToken threw an error that it cannot find the default service worker while trying to register.

What I found is, the fact that my webpage is hosted in github pages and because of that it had default url like "github.com/app-name", is actually a problem because for "registerDefaultServiceWorker" the root is "/" ant the scope is again some constant. Basically firebase should not be setting the "/" as default place since a lot of websites can be hosted on "/some place" directory

Steps and code to reproduce issue

in html:

    <script src="https://www.gstatic.com/firebasejs/11.8.1/firebase-app-compat.js"></script>
    <script src="https://www.gstatic.com/firebasejs/11.8.1/firebase-messaging-compat.js"></script>
    <script defer src="initializeFirebase.js"></script>
    <button
      onclick="requestNotificationPermission()"
    >

in initializeFirebase.js:

async function requestNotificationPermission() {
  const res = await Notification.requestPermission();
  if (res === "denied" || res === "default") {
    alert("Notification access is " + res);
    return;
  }

  try {
    const token = await messaging.getToken({
      vapidKey: vapidKey,
    });
    if (token) {
      console.log("🎱 Token retrieved successfully");
      console.log(token);
    } else {
      requestPermission();
      console.log(
        "No registration token available. Request permission to generate one."
      );
    }
  } catch (error) {
    console.log("Error occured while retrieving token. ");
    console.error(error);
  }
}

Locally it works, when hosted it doesn't.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions