Using local HTTPS
When using Mock Service Worker on a local HTTPS server, you'll encounter issues during the worker script registration in the browser:
1SecurityError: Failed to register a ServiceWorker: An SSL certificate error occurred when fetching the script.
Such error is expected and is thrown by the browser whenever you attempt to register a Service Worker over insecure HTTPS. That limitation is implemented according to the Service Worker specification and serves to prevent security vulnurabilities when intercepting a network traffic on insecure connections.
Connections signed by invalid or self-signed certificates are automatically considered insecure. Below you can see a few suggestions on how to enable API mocking on a local HTTPS environment.
Always favor signed SSL certificates, even when developing locally. Use services like Let's Encrypt to sign your SSL certificate and enable Service Workers in your application.
This is a system-wide option. You can configure your OS to trust the self-signed certificate you're using for development. Please refer to the respective instructions depending on your OS.
Alternatively, you can configure your browser to trust
https://localhost despite it using an invalid certificate. Please resort to this option as a last measure, since adjusting the default browser security rules may lead to unexpected behavior and security vulnerabilities if handled poorly.
- Go to
- Locate the
allow-insecure-localhostflag in the list.
- Select the "Enabled" option from the flag's dropdown.
- Open your local HTTPS application.
- On the "Connection is not secture" screen click the "Advanced" buttom.
- In the section below, click the "Add Exception" button.
- Specify your application's URL as the "Location" input value.
- Click the "Confirm Security Exception" button to enable the exception.