Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run sveltekit dev with https

Is it possible, in development env, to run SvelteKit app with https? I tried to run

npm run dev -- --https  

and vite starts the server successfully:

VITE v3.0.2  ready in 359 ms

  ➜  Local:   https://localhost:5173/
  ➜  Network: [...] 

but i can't connect to https://localhost:5173 chrome says: ERR_SSL_VERSION_OR_CIPHER_MISMATCH

I also tried to edit vite.config.js adding my certificate:

https: {
    key: readFileSync( `${__dirname}/../server/key.pem`),
    cert: readFileSync(`${__dirname}/../server/cert.pem`),
}

and i also tried to use mkcert() following this post:

Vite https on localhost

but it results in the same error

then I tried to use mkcert as plugin:

const config = {
    
    server: {       
        https: true
    },

    plugins: [sveltekit(), mkcert()],

}; 

this time, on first load, it seemed to work, but loading other pages with SvelteKit goto() led to the following different error:

TypeError [ERR_HTTP2_INVALID_CONNECTION_HEADERS]: HTTP/1 Connection specific headers are forbidden: "keep-alive"
like image 849
Sten Ka Razin Avatar asked Mar 24 '26 07:03

Sten Ka Razin


2 Answers

Certificate part

Here I suggest you use mkcert for simplicity but you can also generate your certificate using openssl...

install mkcert

brew install mkcert

install a local certificate authority

> mkcert -install
Created a new local CA 💥
The local CA is now installed in the system trust store! ⚡️
The local CA is now installed in the Firefox trust store (requires browser restart)! 🦊

create your certificate with mkcert

mkdir your_project/cert
cd your_project/cert
mkcert -key-file key.pem -cert-file cert.pem localhost

Vite part

update vite.config.js to include your cert files

import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
import fs from 'fs';

export default defineConfig({
    plugins: [sveltekit()],
    server: {
        https: {
            key: fs.readFileSync(`${__dirname}/cert/key.pem`),
            cert: fs.readFileSync(`${__dirname}/cert/cert.pem`)
        }
    }
});
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
import fs from 'fs';

export default defineConfig({
    plugins: [sveltekit()],
    server: {
        https: {
            key: fs.readFileSync(`${__dirname}/../cert/key.pem`),
            cert: fs.readFileSync(`${__dirname}/../cert/cert.pem`)
        },
        proxy: {}
    }
});

then you should run pnpm run dev and see Local: https://localhost:5173/ in the terminal.

like image 144
Big_Boulard Avatar answered Mar 27 '26 08:03

Big_Boulard


I noticed that add: proxy: {} to vite.config.js solved this problem, but I didn't fully understand why and the consequences of this choice

const config = {
    
    server: {       
        https: true,
        proxy: {} <==
    },

    plugins: [sveltekit(), mkcert()],

};
like image 34
Sten Ka Razin Avatar answered Mar 27 '26 10:03

Sten Ka Razin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!