Theme bundling and dist folder

I am injecting script tag from the platform. Where i am trying to register service worker.
Here is the code

if ("serviceWorker" in navigator) {
  window.addEventListener('load', function() {
    navigator.serviceWorker.register("/sw.js").then(
    (registration) => {
      console.log("registration")
      console.log("Service worker registration succeeded:", registration);
    },
    (error) => {
      console.error(`Service worker registration failed: ${error}`);
    },
  );
});
} else {
  console.error("Service workers are not supported.");
}

The problem is where should i put the sw.js file in my code so that it can be accessed like above.

If i directly put the file in the dist folder it works which is not the right way.