Skip to content

Commit

Permalink
fix(middleware): Handle POST requests in middleware router too (#10418)
Browse files Browse the repository at this point in the history
  • Loading branch information
dac09 authored Apr 9, 2024
1 parent 746e02f commit e192cef
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .changesets/10418.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- fix(middleware): Handle POST requests in middleware router too (#10418) by @dac09

Fixes issue with middleware router not accepted POST requests.
9 changes: 4 additions & 5 deletions packages/vite/src/devFeServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,15 @@ async function createServer() {
return createServerAdapter(async (req: Request) => {
// Recreate middleware router on each request in dev
const middlewareRouter = await createMiddlewareRouter(vite)

if (!middlewareRouter) {
return new Response('No middleware found', { status: 404 })
}

const middleware = middlewareRouter.find(
req.method as HTTPMethod,
req.url,
)?.handler as Middleware | undefined

if (!middleware) {
return new Response('No middleware found', { status: 404 })
}

const [mwRes] = await invoke(req, middleware, route ? { route } : {})

return mwRes.toResponse()
Expand Down
12 changes: 6 additions & 6 deletions packages/vite/src/middleware/createMiddlewareRouter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ describe('createMiddlewareRouter', () => {
expect(result.prettyPrint()).toMatchInlineSnapshot(`
"└── (empty root node)
├── /
│ ├── bazinga (GET)
│ └── kittens (GET)
└── * (GET)
│ ├── bazinga (GET, POST)
│ └── kittens (GET, POST)
└── * (GET, POST)
"
`)
expect(distRegisterMwMock).toHaveBeenCalled()
Expand All @@ -85,9 +85,9 @@ describe('createMiddlewareRouter', () => {
expect(result.prettyPrint()).toMatchInlineSnapshot(`
"└── (empty root node)
├── /
│ ├── bazinga (GET)
│ └── kittens (GET)
└── * (GET)
│ ├── bazinga (GET, POST)
│ └── kittens (GET, POST)
└── * (GET, POST)
"
`)
expect(distRegisterMwMock).toHaveBeenCalled()
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/middleware/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ export const addMiddlewareHandlers = (mwRegList: MiddlewareReg = []) => {
const chainedMw = chain(mwList)

// @NOTE: as any, because we don't actually use the fmw router to invoke the mw
// we use it just for matching. FMW doesn't seem to have a way of customising the handler signature
mwRouter.on('GET', pattern, chainedMw as any)
// we use it just for matching. FMW doesn't seem to have a way of customizing the handler type
mwRouter.on(['GET', 'POST'], pattern, chainedMw as any)
}

return mwRouter
Expand Down
8 changes: 4 additions & 4 deletions packages/vite/src/runFeServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ export async function runFeServer() {

const handleWithMiddleware = (route?: RWRouteManifestItem) => {
return createServerAdapter(async (req: Request) => {
if (!middlewareRouter) {
return new Response('No middleware found', { status: 404 })
}

const middleware = middlewareRouter.find(
req.method as HTTPMethod,
req.url,
)?.handler as Middleware | undefined

if (!middleware) {
return new Response('No middleware found', { status: 404 })
}

const [mwRes] = await invoke(req, middleware, route ? { route } : {})

return mwRes.toResponse()
Expand Down

0 comments on commit e192cef

Please sign in to comment.