Skip to content

Commit

Permalink
fix(server): register OPTIONS fallback middleware at last (#2885)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Jul 11, 2024
1 parent 8d2c02a commit a4574a4
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions packages/core/src/server/getDevMiddlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,6 @@ const applyDefaultMiddlewares = async ({
next();
});

middlewares.push((req, res, next) => {
if (req.method === 'OPTIONS') {
res.statusCode = 200;
res.setHeader('Content-Length', '0');
res.end();
return;
}
next();
});

// dev proxy handler, each proxy has own handler
if (server.proxy) {
const { createProxyMiddleware } = await import('./proxy');
Expand Down Expand Up @@ -211,6 +201,20 @@ const applyDefaultMiddlewares = async ({

middlewares.push(faviconFallbackMiddleware);

// OPTIONS request fallback middleware
// Should register this middleware as the last
// see: https://github.com/web-infra-dev/rsbuild/pull/2867
middlewares.push((req, res, next) => {
if (req.method === 'OPTIONS') {
// Use 204 as no content to send in the response body
res.statusCode = 204;
res.setHeader('Content-Length', '0');
res.end();
return;
}
next();
});

return {
onUpgrade: (...args) => {
for (const cb of upgradeEvents) {
Expand Down

0 comments on commit a4574a4

Please sign in to comment.