Service worker registration failed because the script URL is outside the allowed scope

The requested scope extends beyond what the worker script path allows. Check the URL passed to register() and the scope option. A script under /app/ can serve that section while a registration asking to cover the root needs a different setup.
Place the worker higher in the path if it should control a wider area. When you control the server and need to keep the script where it is, configure an appropriate Service-Worker-Allowed response header. Keep the scope limited to the pages the worker should handle.
The first registration below stays under /app/. The second asks for /, which is outside that directory. It needs Service-Worker-Allowed: / on the response for /app/sw.js. Use a root scope only if the worker should control the whole site, and verify the resulting scope in DevTools Application.
// Same-origin page in a secure context, with /app/sw.js deployed. await navigator.serviceWorker.register('/app/sw.js', { scope: '/app/' });
// Alternative: use only if the worker response sends Service-Worker-Allowed: /. // await navigator.serviceWorker.register('/app/sw.js', { scope: '/' });
Compare the worker script URL and requested scope in DevTools, then verify the pages the worker actually controls.