Skip to content

Commit b4a1b0f

Browse files
alan-agius4dgp1130
authored andcommitted
fix(@angular-devkit/build-angular): correctly respond to preflight requests
With this commit, we add a middleware that handles preflight requests as currently responses for this type of requests returning 404. This is a temporary workaround until this issue is fixed upstream. See: webpack/webpack-dev-server#4180 Closes angular#23639
1 parent b9c6169 commit b4a1b0f

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

packages/angular_devkit/build_angular/src/webpack/configs/dev-server.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import { existsSync, promises as fsPromises } from 'fs';
1111
import { extname, posix, resolve } from 'path';
1212
import { URL, pathToFileURL } from 'url';
1313
import { Configuration, RuleSetRule } from 'webpack';
14-
import { Configuration as DevServerConfiguration } from 'webpack-dev-server';
14+
import type {
15+
Configuration as DevServerConfiguration,
16+
NextFunction,
17+
Request,
18+
Response,
19+
} from 'webpack-dev-server';
1520
import { WebpackConfigOptions, WebpackDevServerOptions } from '../../utils/build-options';
1621
import { assertIsError } from '../../utils/error';
1722
import { loadEsmModule } from '../../utils/load-esm';
@@ -87,6 +92,26 @@ export async function getDevServerConfig(
8792
publicPath: servePath,
8893
stats: false,
8994
},
95+
setupMiddlewares: (middlewares, _devServer) => {
96+
// Temporary workaround for https://github.com/webpack/webpack-dev-server/issues/4180
97+
middlewares.push({
98+
name: 'options-request-response',
99+
path: '*',
100+
middleware: (req: Request, res: Response, next: NextFunction) => {
101+
if (req.method === 'OPTIONS') {
102+
res.statusCode = 204;
103+
res.setHeader('Content-Length', 0);
104+
res.end();
105+
106+
return;
107+
}
108+
109+
next();
110+
},
111+
});
112+
113+
return middlewares;
114+
},
90115
liveReload,
91116
hot: hmr && !liveReload ? 'only' : hmr,
92117
proxy: await addProxyConfig(root, proxyConfig),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import fetch from 'node-fetch';
2+
import { ngServe } from '../../../utils/project';
3+
4+
export default async function () {
5+
const port = await ngServe();
6+
const { size, status } = await fetch(`http://localhost:${port}/main.js`, { method: 'OPTIONS' });
7+
8+
if (size !== 0) {
9+
throw new Error(`Expected "size" to be "0" but got "${size}".`);
10+
}
11+
12+
if (status !== 204) {
13+
throw new Error(`Expected "status" to be "204" but got "${status}".`);
14+
}
15+
}

0 commit comments

Comments
 (0)