Skip to content

Commit

Permalink
fix(nitro): promisify proxy handler (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Jul 28, 2021
1 parent f78d185 commit 0765495
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/server/dev.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Worker } from 'worker_threads'

import { IncomingMessage, ServerResponse } from 'http'
import { loading as loadingTemplate } from '@nuxt/design'
import chokidar, { FSWatcher } from 'chokidar'
import debounce from 'debounce'
import { stat } from 'fs-extra'
import { createApp, Middleware, useBase } from 'h3'
import { promisifyHandle, createApp, Middleware, useBase } from 'h3'
import { createProxy } from 'http-proxy'
import { listen, Listener, ListenOptions } from 'listhen'
import servePlaceholder from 'serve-placeholder'
Expand Down Expand Up @@ -72,16 +73,17 @@ export function createDevServer (nitroContext: NitroContext) {

// SSR Proxy
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) => {
if (workerAddress) {
// Workaround to pass legacy req.spa to proxy
// @ts-ignore
if (req.spa) {
req.headers['x-nuxt-no-ssr'] = 'true'
}
proxy.web(req, res, { target: workerAddress }, (_err: unknown) => {
// console.error('[proxy]', err)
})
return proxyHandle(req, res)
} else {
res.setHeader('Content-Type', 'text/html; charset=UTF-8')
res.end(loadingTemplate({}))
Expand Down

0 comments on commit 0765495

Please sign in to comment.