Skip to content

fix(@angular-devkit/build-angular): correctly respond to preflight requests #23642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import { existsSync, promises as fsPromises } from 'fs';
import { extname, posix, resolve } from 'path';
import { URL, pathToFileURL } from 'url';
import { Configuration, RuleSetRule } from 'webpack';
import { Configuration as DevServerConfiguration } from 'webpack-dev-server';
import type {
Configuration as DevServerConfiguration,
NextFunction,
Request,
Response,
} from 'webpack-dev-server';
import { WebpackConfigOptions, WebpackDevServerOptions } from '../../utils/build-options';
import { assertIsError } from '../../utils/error';
import { loadEsmModule } from '../../utils/load-esm';
Expand Down Expand Up @@ -87,6 +92,26 @@ export async function getDevServerConfig(
publicPath: servePath,
stats: false,
},
setupMiddlewares: (middlewares, _devServer) => {
// Temporary workaround for https://github.com/webpack/webpack-dev-server/issues/4180
middlewares.push({
name: 'options-request-response',
path: '*',
middleware: (req: Request, res: Response, next: NextFunction) => {
if (req.method === 'OPTIONS') {
res.statusCode = 204;
res.setHeader('Content-Length', 0);
res.end();

return;
}

next();
},
});

return middlewares;
},
liveReload,
hot: hmr && !liveReload ? 'only' : hmr,
proxy: await addProxyConfig(root, proxyConfig),
Expand Down
15 changes: 15 additions & 0 deletions tests/legacy-cli/e2e/tests/commands/serve/preflight-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import fetch from 'node-fetch';
import { ngServe } from '../../../utils/project';

export default async function () {
const port = await ngServe();
const { size, status } = await fetch(`http://localhost:${port}/main.js`, { method: 'OPTIONS' });

if (size !== 0) {
throw new Error(`Expected "size" to be "0" but got "${size}".`);
}

if (status !== 204) {
throw new Error(`Expected "status" to be "204" but got "${status}".`);
}
}