liuweiGL / vite-plugin-mkcert

Provide certificates for vite's https dev service.
https://www.npmjs.com/package/vite-plugin-mkcert
MIT License
541 stars 37 forks source link

node18 custom domain doesn't work #64

Closed tjx666 closed 1 year ago

tjx666 commented 1 year ago

Describe the bug

node16 support but node18 doesn't work.

Reproduction

  1. add 127.0.0.1 my.dev.com to /etc/hosts
  2. git clone git@github.com:tjx666/vite-https-issue.git
  3. pnpm install
  4. pnpm dev
  5. open https://my.dev.com:5173/ in chrome, work fine
  6. ctrl + c, change .nvmrc to v18.15.0 and use node18
  7. pnpm dev
  8. open https://my.dev.com:5173/ in chrome
image

System Info

Output of npx envinfo --system --npmPackages vite,vite-plugin-mkcert --binaries --browsers:


  System:
    OS: macOS 13.2.1
    CPU: (8) x64 Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz
    Memory: 38.14 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.15.0 - ~/Library/Caches/fnm_multishells/86518_1678443997092/bin/node
    Yarn: 1.22.19 - ~/Library/Caches/fnm_multishells/86518_1678443997092/bin/yarn
    npm: 9.6.1 - ~/Library/Caches/fnm_multishells/86518_1678443997092/bin/npm
  Browsers:
    Chrome: 111.0.5563.64
    Chrome Canary: 113.0.5640.0
    Firefox: 109.0.1
    Firefox Developer Edition: 111.0
    Safari: 16.3
  npmPackages:
    vite: ^4.1.4 => 4.1.4 
    vite-plugin-mkcert: ^1.13.3 => 1.13.3 

Used package manager: pnpm

Logs

❯ dev

> vite-https-issue@1.0.0 dev
> vite

The list of generated files:
/Users/yutengjing/code/vite-https-issue/node_modules/.cert/dev.pem
/Users/yutengjing/code/vite-https-issue/node_modules/.cert/cert.pem

  VITE v4.1.4  ready in 710 ms

  ➜  Local:   https://localhost:5173/
  ➜  Network: use --host to expose
  ➜  press h to show help

liuweiGL commented 1 year ago

我这边测试没问题,你试试 https://localhost:5173 能访问吗?

tjx666 commented 1 year ago

我这边测试没问题,你试试 https://localhost:5173 能访问吗?

localhost 可以,但是其它域名不行,其它域名在 node16 可以

liuweiGL commented 1 year ago

那明显是域名解析有问题吧,my.dev.com 没解析到 127.0.0.1

tjx666 commented 1 year ago

我在 issue 描述里面说了要配置 hosts,我白写了

liuweiGL commented 1 year ago

建议你了解一下网络相关的知识好吧,插件的 hosts 配置项只是用来生成证书的跟域名解析没有任何联系,而服务无法访问明显是 ip:port 无法建立连接

tjx666 commented 1 year ago

我说的是 /etc/hosts 文件,不是插件的 hosts 配置

liuweiGL commented 1 year ago

localhost 能正常访问说明 http 服务启动没问题,按道理这个问题跟插件就没关系了,你可以在 node18 环境下把 https 设置为 false 测试一下

liuweiGL commented 1 year ago
import { defineConfig } from 'vite'
import mkcert from 'vite-plugin-mkcert'

// https://vitejs.dev/config/
export default defineConfig({
  server: {
    host: 'my.dev.cn'
  },
  plugins: [
    mkcert()
  ]
})
liuweiGL commented 1 year ago

vite 开启的服务默认只会监听 127.0.0.1,所以自定义域名是无法访问的

tjx666 commented 1 year ago

我在 hosts 文件里把自定义域名指向了 127.0.0.1,node16 按照我给的 reproduce 是可以访问的。 实际的业务开发很多都是需要用和线上服务一样的父域名。

tjx666 commented 1 year ago

https://user-images.githubusercontent.com/41773861/224624092-fd72e0d5-eca6-4ef4-abd3-c94a0af11b13.mov

liuweiGL commented 1 year ago
import { defineConfig } from 'vite'
import mkcert from 'vite-plugin-mkcert'

// https://vitejs.dev/config/
export default defineConfig({
  server: {
    host: 'my.dev.cn'
  },
  plugins: [
    mkcert()
  ]
})

改成这个配置

firimar commented 10 months ago

2023-03-13.14.27.31.mov

设置server里 host:true或者‘0.0.0.0’ 试试