Microsoft Edge 17中Service Worker注册报错(TypeError: Invalid argument)求助
Hey German, let's work through this frustrating Service Worker issue you're hitting with Edge 17 over HTTPS. Based on the details you shared, here are targeted troubleshooting steps and fixes to try:
Key Observations Recap
- Works fine on
localhostand in Chrome/Firefox, but fails only on Edge 17 + HTTPS (unauthenticated scenario) - Error:
TypeError: Invalid argumentwhen callingnavigator.serviceWorker.register()
Troubleshooting Steps & Fixes
1. Validate Your HTTPS Certificate
Edge 17 has stricter checks for HTTPS certificate trust compared to modern Chrome/Firefox. If you're using a self-signed or untrusted certificate (your "unauthenticated scenario"), Edge 17 may block Service Worker registration entirely—even if you bypass the browser's "unsafe" warning.
- Try switching to a trusted CA-issued certificate for your HTTPS environment. This is what the working Service Worker demo site uses, and it’s likely the root cause of the discrepancy.
2. Fix Service Worker Path Resolution
Relative paths can behave differently across browsers and environments. Even if it works on localhost, HTTPS might introduce path resolution quirks in Edge 17.
- Use an absolute URL for registration to eliminate ambiguity. You can generate it dynamically using
window.location.href:navigator.serviceWorker.register(new URL('sw.js', window.location.href)) .then(() => alert("Registration succeeded!")) .catch(e => console.error("Registration failed:", e)); - Alternatively, use a root-relative path like
"/sw.js"(ensure the file is placed at your website’s root directory).
3. Check sw.js Response Headers
Edge 17 is picky about the Content-Type header for Service Worker files. If your server returns text/plain instead of application/javascript, it will reject the registration.
- Inspect the network tab in Edge DevTools:
- Open DevTools (F12)
- Go to the Network tab
- Filter for
sw.jsand check theContent-Typeresponse header
- If it’s not
application/javascript, configure your server to send the correct header for.jsfiles.
4. Account for Edge 17’s Known Bug
Edge 17 has a documented bug where Service Worker registration fails in unauthenticated HTTPS environments. If the above steps don’t resolve the issue:
- Upgrade Edge: Edge 17 is an outdated legacy version. Upgrading to the Chromium-based Edge will fully resolve this compatibility issue.
- Add Browser-Specific Fallback: If you must support Edge 17, detect the browser and either:
- Show a message prompting users to upgrade, or
- Skip Service Worker registration for this specific browser version.
内容的提问来源于stack exchange,提问作者German

