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"}
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 { getViteNodeOptions } from './vite-node-shared.mjs'
import { viteNodeFetch } from './vite-node-shared.mjs'
const viteNodeOptions = getViteNodeOptions()
export default () => $fetch('/manifest', { baseURL: viteNodeOptions.baseURL })
export default () => viteNodeFetch('/manifest')

View File

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