mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 23:22:02 +00:00
fix(nitro): promisify proxy handler (#398)
This commit is contained in:
parent
4a989e9b7a
commit
858e31c7fc
@ -1,10 +1,11 @@
|
|||||||
import { Worker } from 'worker_threads'
|
import { Worker } from 'worker_threads'
|
||||||
|
|
||||||
|
import { IncomingMessage, ServerResponse } from 'http'
|
||||||
import { loading as loadingTemplate } from '@nuxt/design'
|
import { loading as loadingTemplate } from '@nuxt/design'
|
||||||
import chokidar, { FSWatcher } from 'chokidar'
|
import chokidar, { FSWatcher } from 'chokidar'
|
||||||
import debounce from 'debounce'
|
import debounce from 'debounce'
|
||||||
import { stat } from 'fs-extra'
|
import { stat } from 'fs-extra'
|
||||||
import { createApp, Middleware, useBase } from 'h3'
|
import { promisifyHandle, createApp, Middleware, useBase } from 'h3'
|
||||||
import { createProxy } from 'http-proxy'
|
import { createProxy } from 'http-proxy'
|
||||||
import { listen, Listener, ListenOptions } from 'listhen'
|
import { listen, Listener, ListenOptions } from 'listhen'
|
||||||
import servePlaceholder from 'serve-placeholder'
|
import servePlaceholder from 'serve-placeholder'
|
||||||
@ -72,6 +73,9 @@ export function createDevServer (nitroContext: NitroContext) {
|
|||||||
|
|
||||||
// SSR Proxy
|
// SSR Proxy
|
||||||
const proxy = createProxy()
|
const proxy = createProxy()
|
||||||
|
const proxyHandle = promisifyHandle((req: IncomingMessage, res: ServerResponse) => proxy.web(req, res, { target: workerAddress }, (_err: unknown) => {
|
||||||
|
// console.error('[proxy]', err)
|
||||||
|
}))
|
||||||
app.use((req, res) => {
|
app.use((req, res) => {
|
||||||
if (workerAddress) {
|
if (workerAddress) {
|
||||||
// Workaround to pass legacy req.spa to proxy
|
// Workaround to pass legacy req.spa to proxy
|
||||||
@ -79,9 +83,7 @@ export function createDevServer (nitroContext: NitroContext) {
|
|||||||
if (req.spa) {
|
if (req.spa) {
|
||||||
req.headers['x-nuxt-no-ssr'] = 'true'
|
req.headers['x-nuxt-no-ssr'] = 'true'
|
||||||
}
|
}
|
||||||
proxy.web(req, res, { target: workerAddress }, (_err: unknown) => {
|
return proxyHandle(req, res)
|
||||||
// console.error('[proxy]', err)
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
res.setHeader('Content-Type', 'text/html; charset=UTF-8')
|
res.setHeader('Content-Type', 'text/html; charset=UTF-8')
|
||||||
res.end(loadingTemplate({}))
|
res.end(loadingTemplate({}))
|
||||||
|
Loading…
Reference in New Issue
Block a user