Setting up trusted local HTTPS for a Vite app
Compare Vite basic SSL with mkcert, install a trusted localhost certificate, and verify the resulting TLS connection and ALPN negotiation with curl.
I use local HTTPS when I need to exercise an OAuth callback, test secure cookies, or inspect HTTP/2 behavior. Vite can use a self-signed certificate for a quick check or an mkcert certificate trusted by my development machine.
Identify what HTTPS needs to reproduce
Service Workers, Web Crypto, Geolocation, and media device APIs have secure-context requirements. Browsers commonly treat loopback origins such as http://localhost as trustworthy, so some of those APIs can already work there. Trusted HTTPS still matters when the integration explicitly requires an HTTPS callback, the test uses a LAN hostname, or I want to reproduce the transport and cookie behavior of deployment.
I also use HTTPS to test protocol negotiation. Browsers ordinarily use TLS and ALPN to negotiate HTTP/2. Serving a page over TLS proves the certificate and connection work. It does not establish that the connection chose HTTP/2.
Pick a certificate strategy
| Concern | Vite basic SSL plugin | mkcert |
|---|---|---|
| Certificate | Self-signed development certificate | Signed by a local root CA |
| Setup | Plugin installation and registration | Install a root certificate and issue names for the server |
| Browser trust | May require accepting a warning | Trusted after the relevant browser or system trust store accepts the root |
| Additional devices | Each device needs its own trust setup | Install the public CA certificate on the test device |
| Useful when | A fast local protocol test | Repeated OAuth, cookie, and device testing |
A self-signed certificate is convenient for a short experiment. Some tools and browsers stop rather than offering a useful override. For repeated integration testing, I prefer a certificate issued by mkcert’s local CA, with the names and IP addresses the clients actually use.
Create a local certificate and configure Vite
flowchart TD
A["Install mkcert<br/>(brew install mkcert)"] --> B["Create and trust a local root CA<br/>(mkcert -install)"]
B --> C["Issue a localhost certificate<br/>(mkcert localhost 127.0.0.1 ::1)"]
C --> D["Write localhost.pem and localhost-key.pem"]
D --> E["Configure Vite server.https<br/>(vite.config.ts)"]
E --> F["Open the site in a trusted secure context"]
1
2
3
4
5
6
7
8
# 1. Install mkcert
brew install mkcert
# 2. Trust the local root CA once
mkcert -install
# 3. Issue the localhost certificate and key in the project root
mkcert localhost 127.0.0.1 ::1
The certificate and its private key must match. Keep the key out of source control and avoid copying the CA private key to another machine. A colleague can install mkcert and create certificates signed by their own local root.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// vite.config.ts
import { defineConfig } from 'vite';
import fs from 'node:fs';
import path from 'node:path';
export default defineConfig({
server: {
port: 5173,
https: {
key: fs.readFileSync(path.resolve(__dirname, 'localhost-key.pem')),
cert: fs.readFileSync(path.resolve(__dirname, 'localhost.pem'))
}
}
});
The explicit server.https configuration makes the file dependency visible. If I want the package setup to detect the certificate automatically, I can use the mkcert plugin instead. This excerpt shows the option shape used in the original setup. Consult the plugin and Vite types for the installed versions before adopting the boolean https option.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// vite.config.ts
import { defineConfig } from 'vite';
import mkcert from 'vite-plugin-mkcert';
export default defineConfig({
plugins: [
mkcert({
hosts: ['localhost', '127.0.0.1', 'local.my-app.internal']
})
],
server: {
port: 5173,
https: true // The plugin supplies the certificate
}
});
Inspect TLS and ALPN separately
In a large ESM development graph, HTTP/1.1 browser connection limits can queue module requests. With HTTP/2, streams can share a connection. That reduces connection-level request queuing. It cannot eliminate the time spent discovering deeper imports or transforming their source.
sequenceDiagram
autonumber
actor Client as Browser or curl
participant TLS as TLS handshake (ALPN)
participant Vite as Vite HTTPS Dev Server
Client->>TLS: Client Hello (ALPN: h2, http/1.1 offered)
TLS->>Vite: Validate server certificate against the mkcert CA
Vite-->>TLS: Server Hello (ALPN: h2 accepted)
TLS-->>Client: TLS session established
Note over Client,Vite: Parallel HTTP/2 streams share one TCP connection
Client->>Vite: Stream 1: GET /src/main.tsx
Client->>Vite: Stream 3: GET /src/App.tsx
Client->>Vite: Stream 5: GET /src/styles.css
Vite-->>Client: Interleaved response frames (200 OK)
I use a curl build with HTTP/2 support and inspect the negotiated protocol. Vite’s server configuration and proxy settings can affect whether HTTP/2 is available, so I check the handshake on the actual configuration rather than inferring from the URL. Vite HTTPS settings
Check the certificate and the negotiated protocol
Figure 1. mkcert installs local trust and issues a certificate for the loopback names.
The illustrated setup uses localhost. When testing from a phone, I issue a cert with the address that the phone connects to and install the public CA certificate on the phone. A certificate for localhost does not match the developer machine’s LAN address.
Figure 2. The curl check distinguishes a trusted certificate from a successful HTTP/2 negotiation.
The flags are curl -Iv --http2 https://localhost:5173/. Look for certificate verification, ALPN selecting h2, and an HTTP/2 200 response. Running with certificate verification disabled would miss the trust check. Trust stores can differ by browser and operating system.
Keep the local certificate reproducible
- Put the setup command in the project documentation or a local script.
- Ignore certificate key files and keep the root CA private key on the machine that owns it.
- Include each hostname or address the browser actually uses in the certificate.
- Repeat the HTTPS and request routing checks when adding a local API proxy.