arlyxiao / best-practice

1 stars 0 forks source link

Setup https on your local test with vite #97

Open arlyxiao opened 1 year ago

arlyxiao commented 1 year ago

Step: 1

Install mkcert tool - macOS; you can see the mkcert repo for details

brew install mkcert

Step: 2

Install nss (only needed if you use Firefox)

brew install nss

Step: 3

Setup mkcert on your machine (creates a CA)

mkcert -install

Step: 4 (Final)

at the project root directory run the following command

mkdir -p .cert && mkcert -key-file ./.cert/key.pem -cert-file ./.cert/cert.pem 'localhost'

vite.config.ts

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import fs from 'fs';

// https://vitejs.dev/config/
export default defineConfig({
  server: {
    https: {
      key: fs.readFileSync('./.cert/key.pem'),
      cert: fs.readFileSync('./.cert/cert.pem'),
    },
  },
  plugins: [react()],
});

Source from

https://stackoverflow.com/questions/69417788/vite-https-on-localhost