Skip to content

Commit 13a3e43

Browse files
committed
fix(@angular/build): allow missing HTML file request to fallback to index
If a HTTP request is made to the development server that explicitly requests an HTML file (i.e., `/abc.html`), the development server will now attempt to fallback to the root `index.html` file if the requested HTML file does not exist. Since this may indicate a defect or other application misconfiguration such as a missing asset, a warning will also be issued in the console during development to notify the developer that something may be wrong.
1 parent ea4a125 commit 13a3e43

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

packages/angular/build/src/tools/vite/middlewares/html-fallback-middleware.ts

+28-6
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,42 @@ import type { ServerResponse } from 'node:http';
1010
import type { Connect } from 'vite';
1111
import { lookupMimeTypeFromRequest } from '../utils';
1212

13+
const ALLOWED_FALLBACK_METHODS = Object.freeze(['GET', 'HEAD']);
14+
1315
export function angularHtmlFallbackMiddleware(
1416
req: Connect.IncomingMessage,
1517
_res: ServerResponse,
1618
next: Connect.NextFunction,
1719
): void {
1820
// Similar to how it is handled in vite
1921
// https://github.com/vitejs/vite/blob/main/packages/vite/src/node/server/middlewares/htmlFallback.ts#L15C19-L15C45
22+
if (!req.method || !ALLOWED_FALLBACK_METHODS.includes(req.method)) {
23+
// No fallback for unsupported request methods
24+
next();
25+
26+
return;
27+
}
28+
29+
if (req.url) {
30+
const mimeType = lookupMimeTypeFromRequest(req.url);
31+
if (mimeType === 'text/html' || mimeType === 'application/xhtml+xml') {
32+
// eslint-disable-next-line no-console
33+
console.warn(
34+
`Request for HTML file "${req.url}" was received but no asset found. Asset may be missing from build.`,
35+
);
36+
} else if (mimeType) {
37+
// No fallback for request of asset-like files
38+
next();
39+
40+
return;
41+
}
42+
}
43+
2044
if (
21-
(req.method === 'GET' || req.method === 'HEAD') &&
22-
(!req.url || !lookupMimeTypeFromRequest(req.url)) &&
23-
(!req.headers.accept ||
24-
req.headers.accept.includes('text/html') ||
25-
req.headers.accept.includes('text/*') ||
26-
req.headers.accept.includes('*/*'))
45+
!req.headers.accept ||
46+
req.headers.accept.includes('text/html') ||
47+
req.headers.accept.includes('text/*') ||
48+
req.headers.accept.includes('*/*')
2749
) {
2850
req.url = '/index.html';
2951
}

0 commit comments

Comments
 (0)