Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] Service worker registration fails #12214

Closed
Martin-Schlodinski opened this issue Jan 4, 2025 · 2 comments
Closed

[bug] Service worker registration fails #12214

Martin-Schlodinski opened this issue Jan 4, 2025 · 2 comments
Labels
status: needs triage This issue needs to triage, applied to new issues type: bug

Comments

@Martin-Schlodinski
Copy link

Martin-Schlodinski commented Jan 4, 2025

Describe the bug

Hi!
I'm using tauri v2 together with Nuxt and Typescript and I'm struggling to register a service worker in production builds with tauri build. The only way I can use a service worker is with tauri dev in combination with @vite-pwa/nuxt ... but I need a production build :)

The service worker that shall be loaded is successfully placed in .tauri-build/dist/sw.js. The devTools only show the error An unknown error occurred when fetching the script.

The devTools network tab also shows that the request is made to http://tauri.localhost/sw.js but never answered.

I don't think this is an issue with my service worker because when I omit tauri and just serve the app with nuxt the service worker also registers properly in MS Edge.

In dev mode when trying a manual installation it also fails with unsupported MIME type indicating that Nuxt just responded with a 404 error page.

It seems to me like this sw.js file is excluded from the bundle. If someone could point me to the tauri source code file where such filtering happens I'm happy to take a deeper look. I just never programmed in Rust and in the tauri source and I don't see the forest for the trees.

I appreciate any hint! :)

Thank you!
Martin

Reproduction

https://github.com/Martin-Schlodinski/tauri-sw

pnpm install

then pnpm tauri dev works with @vite-pwa/nuxt - the service worker is installed. However, manual registration still fails.
with pnpm tauri build the service worker can't get installed at all.

Expected behavior

Service workers can be loaded.

Full tauri info output

[✔] Environment
    - OS: Windows 10.0.26100 x86_64 (X64)
    ✔ WebView2: 131.0.2903.112
    ✔ MSVC:
        - Visual Studio Build Tools 2022
        - Visual Studio Community 2022
    ✔ rustc: 1.83.0 (90b35a623 2024-11-26)
    ✔ cargo: 1.83.0 (5ffbef321 2024-10-29)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: stable-x86_64-pc-windows-msvc (default)
    - node: 20.17.0
    - pnpm: 9.14.1
    - npm: 10.8.2

[-] Packages
    - tauri 🦀: 2.2.0
    - tauri-build 🦀: 2.0.4
    - wry 🦀: 0.48.0
    - tao 🦀: 0.31.0
    - @tauri-apps/api : 2.2.0
    - @tauri-apps/cli : 2.2.2

[-] Plugins
    - tauri-plugin-shell 🦀: 2.2.0
    - @tauri-apps/plugin-shell : 2.0.1 (outdated, latest: 2.2.0)
    - tauri-plugin-updater 🦀: 2.3.0
    - @tauri-apps/plugin-updater : not installed!

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../.tauri-build/dist
    - devUrl: http://127.0.0.1:3000/
    - framework: Vue.js (Nuxt)
    - bundler: Vite

Stack trace

No response

Additional context

No response

@Martin-Schlodinski Martin-Schlodinski added status: needs triage This issue needs to triage, applied to new issues type: bug labels Jan 4, 2025
@Martin-Schlodinski
Copy link
Author

Martin-Schlodinski commented Jan 5, 2025

Another observation: When adding a new window to the tauri config that shall only show the service worker script it can indeed be found:

"app": {
    "windows": [
      {
        "title": "tauri-sw",
        "label": "sw",
        "url": "/sw.js"
      }
    ],
    ...
 }

So the script is bundled but can't be accessed.

I also tried to do a hack by importing the sw.js in the App.vue and added such a guard inside the sw.js file:

if (!document) {
  self.addEventListener("install", function (e) {
    console.log("service worker installation");
  });
} else {
  navigator.serviceWorker.register("/test.js");
}

But still: Failed to register a ServiceWorker for scope ('http://tauri.localhost/') with script ('http://tauri.localhost/test.js'): An unknown error occurred when fetching the script.

However as fetch('/sw.js') succeeds I don't understand this dark magic :)

@Martin-Schlodinski
Copy link
Author

Solved using the localhost plugin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs triage This issue needs to triage, applied to new issues type: bug
Projects
None yet
Development

No branches or pull requests

1 participant