Using a custom HTTPS cert in Nuxt (but only for dev mode)?

1.4k Views Asked by At

I am using a dependency that requires me to have HTTPS on localhost. I've used the following code in nuxt.config.js to accomplish that:

server: {        
        https: {
            key: fs.readFileSync(path.resolve(__dirname, 'localhost-key.pem')),
            cert: fs.readFileSync(path.resolve(__dirname, 'localhost.pem'))
        }
    },

Those are keys I created myself with mkcert. However, I'm going to be using an actual cert on the live page. Is there any way to limit that server block in nuxt.config.js to only dev mode?

1

There are 1 best solutions below

0
On BEST ANSWER

I've used that in the past

server: {
    https: process.env.NODE_ENV === 'development' && process.env.USE_LOCAL_HTTPS === 'true'
      ? {
        key: fs.readFileSync(path.resolve(__dirname, 'server.key')),
        cert: fs.readFileSync(path.resolve(__dirname, 'server.crt')),
      }
      : false,
},

NODE_ENV was used to double check that the environment was development and USE_LOCAL_HTTPS was another variable to be sure that it was not a staging dev environment. Of course, if you have something like staging or test for your NODE_ENV, you may not even need that.

Otherwise, I never double-checked that but this server key configuration may even work only for local dev maybe. Try to give it a try, otherwise try my configuration.