skip to Main Content

I enabled https in vite.config because I need it for development. But I don’t know how to use this field in production since I’m going to use Nginx and let’s encrypt. Do I need to check if I’m in development mode with an environment variable?

I’m using vite with SvelteKit.

Here’s my vite.config

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

export default defineConfig(({ command, mode }) => {
    const env = loadEnv(mode, process.cwd(), '');
    return {
        plugins: [sveltekit()],
        server: {
            https: {
                key: fs.readFileSync(env.HTTPS_KEY),
                cert: fs.readFileSync(env.HTTPS_CERTS)
            },
            proxy: {}
        }
    };
});

2

Answers


  1. If you are looking for production solution I’d suggest using a process manager like pm2

    You can build a server using standard config and running vite build and configure pm2 with ecosystem file to use the compiled project

    Login or Signup to reply.
  2. Example on how to use env vars for future reference:

    import { defineConfig, loadEnv } from "vite";      // <-- loadEnv!
    import { sveltekit } from '@sveltejs/kit/vite';
    import fs from 'fs';
    
    export default defineConfig(({ mode }) => {
        const env = loadEnv(mode, process.cwd(), "");
    
        const serverSettings = env.APP_ENV === "local" // <-- Condition here
            ? { server: { host: "localhost" } } 
            : {};
    
        return {
            plugins: [sveltekit()],
            ...serverSettings,                         // <-- Add here!
        };
    });
    

    Hope that helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search