Parcel: Registering ServiceWorker fails with wrong path

Created on 9 Jul 2018  路  7Comments  路  Source: parcel-bundler/parcel

Should be fixed with #1205 and #1206 but still does not work. I've updated parcel to the latest commits (git clone, npm install, npm run build inside the parcel-bundler folder)

馃 Expected Behavior

Parcel should find calls to navigator.serviceWorker.register and resolve the given path correctly to a public http url accessible by the browser.

馃槸 Current Behavior

Parcel finds the register() call, but resolves the path wrong. The browser can not load the serviceworker file, therefore the registration fails.

The URL produced by parcel in the example repository is https://localhost:1234/srcserviceworkersw.js

The working URL is https://localhost:1234/src/serviceworker/sw.js (but not preferred)

馃拋 Possible Solution

馃敠 Context

馃捇 Code Sample

src/index.js
navigator.serviceWorker.register('./serviceworker/sw.js')

compiled to
/build/src/serviceworker/sw.js

馃實 Your Environment

| Software | Version(s) |
| ---------------- | ---------- |
| Parcel | latest commits
| Node | v8.11.2
| npm/Yarn | 5.6.0/1.7.0
| Operating System | Win 10 64Bit

Bug

Most helpful comment

this is the problem, in the compiled src.XXXX.js it is
navigator.serviceWorker.register('/src\sw.js');
but should be
navigator.serviceWorker.register('/src/sw.js');

All 7 comments

Parcel does not treat serviceworkers in any special way. See parcel-plugin-workbox and parcel-plugin-swprecache

@KeineLimonade actually it does, if I understood #398 right ... the produced URL to the service worker is just wrong (but should be fixed, see #1205 and #1206)

as far as I understand parcel-plugin-workbox and parcel-plugin-swprecache, they always generate a serviceworker-file. I want my own existing serviceworker-file to be detected correctly

this is the problem, in the compiled src.XXXX.js it is
navigator.serviceWorker.register('/src\sw.js');
but should be
navigator.serviceWorker.register('/src/sw.js');

Oh. Ok, thanks for the clarification

I experienced this problem as well, but my mistake was putting the call to navigator.serviceWorker.register() in a