Skip to content

Commit cb813e4

Browse files
committed
fix: auto reply to OPTIONS requests only when unhandled
Prior to this change the internal options middleware always responsed to such requests. This is because the middleware was registered too early and was not being used as a fallback. With this change we register this middleware as the last middleware to ensure that this is only used as a fallback when OPTIONS requests are not handled. Closes webpack#4551
1 parent 85dcb31 commit cb813e4

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

lib/Server.js

+22-26
Original file line numberDiff line numberDiff line change
@@ -2131,32 +2131,6 @@ class Server {
21312131
});
21322132
}
21332133

2134-
{
2135-
/**
2136-
*
2137-
* @param {Request} req
2138-
* @param {Response} res
2139-
* @param {NextFunction} next
2140-
* @returns {void}
2141-
*
2142-
*/
2143-
const optionsRequestResponseMiddleware = (req, res, next) => {
2144-
if (req.method === "OPTIONS") {
2145-
res.statusCode = 204;
2146-
res.setHeader("Content-Length", "0");
2147-
res.end();
2148-
return;
2149-
}
2150-
next();
2151-
};
2152-
2153-
middlewares.push({
2154-
name: "options-middleware",
2155-
path: "*",
2156-
middleware: optionsRequestResponseMiddleware,
2157-
});
2158-
}
2159-
21602134
middlewares.push({
21612135
name: "webpack-dev-middleware",
21622136
middleware:
@@ -2411,6 +2385,28 @@ class Server {
24112385
});
24122386
}
24132387

2388+
// Register this middleware always as the last one so that it's only used as a
2389+
// fallback when no other middleware responses.
2390+
middlewares.push({
2391+
name: "options-middleware",
2392+
path: "*",
2393+
/**
2394+
* @param {Request} req
2395+
* @param {Response} res
2396+
* @param {NextFunction} next
2397+
* @returns {void}
2398+
*/
2399+
middleware: (req, res, next) => {
2400+
if (req.method === "OPTIONS") {
2401+
res.statusCode = 204;
2402+
res.setHeader("Content-Length", "0");
2403+
res.end();
2404+
return;
2405+
}
2406+
next();
2407+
},
2408+
});
2409+
24142410
if (typeof this.options.setupMiddlewares === "function") {
24152411
middlewares = this.options.setupMiddlewares(middlewares, this);
24162412
}

0 commit comments

Comments
 (0)