fix(vite): nuxi dev --https working out of the box (#7547)

This commit is contained in:
pooya parsa 2022-09-15 13:01:52 +02:00 committed by GitHub
parent d882d36805
commit 9426b54562
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 17 deletions

View File

@ -25,3 +25,7 @@ This command sets `process.env.NODE_ENV` to `development`.
::alert{type="info"} ::alert{type="info"}
If you are using a self-signed certificate in development, you will need to set `NODE_TLS_REJECT_UNAUTHORIZED=0` in your environment. If you are using a self-signed certificate in development, you will need to set `NODE_TLS_REJECT_UNAUTHORIZED=0` in your environment.
:: ::
::stabilityEdge{title="Improved HTTPS Support"}
With next releases, `NODE_TLS_REJECT_UNAUTHORIZED` is no longer required. You can try this on edge channel using `npx nuxi-edge@latest dev --https`
::

View File

@ -1,6 +1,3 @@
import { $fetch } from 'ohmyfetch' import { viteNodeFetch } from './vite-node-shared.mjs'
import { getViteNodeOptions } from './vite-node-shared.mjs'
const viteNodeOptions = getViteNodeOptions() export default () => viteNodeFetch('/manifest')
export default () => $fetch('/manifest', { baseURL: viteNodeOptions.baseURL })

View File

@ -1,3 +1,11 @@
export function getViteNodeOptions () { import { Agent as HTTPSAgent } from 'node:https'
return JSON.parse(process.env.NUXT_VITE_NODE_OPTIONS || '{}') import { $fetch } from 'ohmyfetch'
}
export const viteNodeOptions = JSON.parse(process.env.NUXT_VITE_NODE_OPTIONS || '{}')
export const viteNodeFetch = $fetch.create({
baseURL: viteNodeOptions.baseURL,
agent: viteNodeOptions.baseURL.startsWith('https://')
? new HTTPSAgent({ rejectUnauthorized: false })
: null
})

View File

@ -1,11 +1,9 @@
import { performance } from 'node:perf_hooks' import { performance } from 'node:perf_hooks'
import { createError } from 'h3' import { createError } from 'h3'
import { ViteNodeRunner } from 'vite-node/client' import { ViteNodeRunner } from 'vite-node/client'
import { $fetch } from 'ohmyfetch'
import consola from 'consola' import consola from 'consola'
import { getViteNodeOptions } from './vite-node-shared.mjs' import { viteNodeOptions, viteNodeFetch } from './vite-node-shared.mjs'
const viteNodeOptions = getViteNodeOptions()
const runner = createRunner() const runner = createRunner()
let render let render
@ -15,9 +13,7 @@ export default async (ssrContext) => {
process.server = true process.server = true
// Invalidate cache for files changed since last rendering // Invalidate cache for files changed since last rendering
const invalidates = await $fetch('/invalidates', { const invalidates = await viteNodeFetch('/invalidates')
baseURL: viteNodeOptions.baseURL
})
const updates = runner.moduleCache.invalidateDepTree(invalidates) const updates = runner.moduleCache.invalidateDepTree(invalidates)
// Execute SSR bundle on demand // Execute SSR bundle on demand
@ -39,9 +35,7 @@ function createRunner () {
async fetchModule (id) { async fetchModule (id) {
// TODO: fix in vite-node // TODO: fix in vite-node
id = id.replace(/\/\//g, '/') id = id.replace(/\/\//g, '/')
return await $fetch('/module/' + encodeURI(id), { return await viteNodeFetch('/module/' + encodeURI(id)).catch((err) => {
baseURL: viteNodeOptions.baseURL
}).catch((err) => {
const errorData = err?.data?.data const errorData = err?.data?.data
if (!errorData) { if (!errorData) {
throw err throw err