From 261633a1a1d22706728701b42b4b113662122802 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Wed, 11 Oct 2023 02:07:24 -0500 Subject: [PATCH] fix(proxy): correct the logic of bypass returning false (#14579) --- .../vite/src/node/server/middlewares/proxy.ts | 3 ++- .../proxy-bypass/__tests__/proxy-hmr.spec.ts | 8 ++++++++ playground/proxy-bypass/index.html | 2 ++ playground/proxy-bypass/package.json | 11 +++++++++++ playground/proxy-bypass/vite.config.js | 15 +++++++++++++++ pnpm-lock.yaml | 2 ++ 6 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 playground/proxy-bypass/__tests__/proxy-hmr.spec.ts create mode 100644 playground/proxy-bypass/index.html create mode 100644 playground/proxy-bypass/package.json create mode 100644 playground/proxy-bypass/vite.config.js diff --git a/packages/vite/src/node/server/middlewares/proxy.ts b/packages/vite/src/node/server/middlewares/proxy.ts index bf658a9ea7474f..219d15f3c81d21 100644 --- a/packages/vite/src/node/server/middlewares/proxy.ts +++ b/packages/vite/src/node/server/middlewares/proxy.ts @@ -154,7 +154,8 @@ export function proxyMiddleware( return next() } else if (bypassResult === false) { debug?.(`bypass: ${req.url} -> 404`) - return res.end(404) + res.statusCode = 404 + return res.end() } } diff --git a/playground/proxy-bypass/__tests__/proxy-hmr.spec.ts b/playground/proxy-bypass/__tests__/proxy-hmr.spec.ts new file mode 100644 index 00000000000000..093179072a4118 --- /dev/null +++ b/playground/proxy-bypass/__tests__/proxy-hmr.spec.ts @@ -0,0 +1,8 @@ +import { expect, test, vi } from 'vitest' +import { browserLogs } from '~utils' + +test('proxy-bypass', async () => { + await vi.waitFor(() => { + expect(browserLogs.join('\n')).toContain('status of 404 (Not Found)') + }) +}) diff --git a/playground/proxy-bypass/index.html b/playground/proxy-bypass/index.html new file mode 100644 index 00000000000000..f14fde8e428635 --- /dev/null +++ b/playground/proxy-bypass/index.html @@ -0,0 +1,2 @@ +root app
+ diff --git a/playground/proxy-bypass/package.json b/playground/proxy-bypass/package.json new file mode 100644 index 00000000000000..fa9ecab396977a --- /dev/null +++ b/playground/proxy-bypass/package.json @@ -0,0 +1,11 @@ +{ + "name": "@vitejs/test-proxy-bypass", + "private": true, + "version": "0.0.0", + "type": "module", + "scripts": { + "dev": "vite", + "build": "vite build", + "preview": "vite preview" + } +} diff --git a/playground/proxy-bypass/vite.config.js b/playground/proxy-bypass/vite.config.js new file mode 100644 index 00000000000000..13f7a6b42eaafb --- /dev/null +++ b/playground/proxy-bypass/vite.config.js @@ -0,0 +1,15 @@ +import { defineConfig } from 'vite' + +export default defineConfig({ + server: { + port: 9606, + proxy: { + '/anotherApp': { + target: 'http://localhost:9607', + bypass: () => { + return false + }, + }, + }, + }, +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e0406f3389b179..a421b405b6f0b6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1030,6 +1030,8 @@ importers: playground/preserve-symlinks/module-a: {} + playground/proxy-bypass: {} + playground/proxy-hmr: {} playground/proxy-hmr/other-app: {}