My goal was to test jch.app serviceworkers with different devices on the same network. While localhost is an exception allowed for serviceworkers, all other origins require a no-warning https connection. This meant the certificate must be signed with a system trusted CA.
Fortunately, mkcert does exactly that. Some additional fiddling was
needed to configure puma with command line options to reference the certs
and listen for SSL connections. No additional gems,
or configuration changes were necessary. Tested on macOS 15.6.1,
puma 6.6.0, mkcert 1.4.4, and rails 8.0.2.
# Run from rails root
# Create locally trusted certificate https://github.com/FiloSottile/mkcert
$ mkcert -install
# Used `sudo scutil --set LocalHostName` to set local hostname to `roboplan.local`
$ mkcert roboplan.local "*.roboplan.local" roboplan.local localhost 127.0.0.1 ::1
# Rename to avoid shell escaping later
$ mkdir -p config/certs
$ mv roboplan.local+5-key.pem config/certs/roboplan.local-key.pem
$ mv roboplan.local+5.pem config/certs/roboplan.local.pem
# Added in bin/dev
$ bin/rails server -b 'ssl://0.0.0.0?key=config/certs/roboplan.local-key.pem&cert=config/certs/roboplan.local.pem'Puma and Falcon support self-signed certificates with localhost gem, but
the defaults did not add a system trusted CA causing certificate warnings
that made serviceworkers unavailable.
Additional notes:
mkcert is a cross platform tool to install a system trusted CA, and use that to sign certs that won't give the insecure warninglocalhost the development env without an explicit requirepuma reads from config/puma/development.rb, but does not evaluate the global config/puma.rb
localhost setup uses bake localhost:install, but does not list bake as a dependencypuma config ssl_bind still requires starting puma or rails server with -b 'ssl://localhost:9292' to handle SSL. Because of this, I preferred keeping all the config in one place as a CLI flag.puma docs start server with puma, but this loses the logging defaults I prefer with rails server
bin/setup updated with mkcert steps for repeatabilityService workers are only available in secure contexts: this means that their document is served over HTTPS, although browsers also treat http://localhost as a secure context, to facilitate local development. MDN Service Worker API
ssl_bind config, but was missing the -b ssl://0.0.0.0:3001. I found mkcert first, but this implementation may be easier to use with bin/setup and version control.