Skip to content

Commit b4cf68d

Browse files
committed
fix(@angular-devkit/build-angular): return 404 for assets that are not found
This commit updates the vite dev-server to return 404 for assets and files that are not found. Closes angular#26917
1 parent c57c2b6 commit b4cf68d

File tree

1 file changed

+6
-19
lines changed

1 file changed

+6
-19
lines changed

packages/angular_devkit/build_angular/src/tools/vite/angular-memory-plugin.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,6 @@ export function createAngularMemoryPlugin(options: AngularMemoryPluginOptions):
123123
// The base of the URL is unused but required to parse the URL.
124124
const pathname = pathnameWithoutBasePath(req.url, server.config.base);
125125
const extension = extname(pathname);
126-
if (!extension) {
127-
next();
128-
129-
return;
130-
}
131126

132127
// Rewrite all build assets to a vite raw fs URL
133128
const assetSourcePath = assets.get(pathname);
@@ -323,24 +318,16 @@ function angularHtmlFallbackMiddleware(
323318
// Similar to how it is handled in vite
324319
// https://github.com/vitejs/vite/blob/main/packages/vite/src/node/server/middlewares/htmlFallback.ts#L15C19-L15C45
325320
if (
326-
// Only accept GET or HEAD
327-
(req.method !== 'GET' && req.method !== 'HEAD') ||
328-
// Has file extensions
329-
(req.url && lookupMimeTypeFromRequest(req.url)) ||
330-
// Require Accept: text/html or */*
331-
!(
332-
req.headers.accept === undefined || // equivalent to `Accept: */*`
333-
req.headers.accept === '' || // equivalent to `Accept: */*`
321+
(req.method === 'GET' || req.method === 'HEAD') &&
322+
(!req.url || !lookupMimeTypeFromRequest(req.url)) &&
323+
(!req.headers.accept ||
334324
req.headers.accept.includes('text/html') ||
335-
req.headers.accept.includes('*/*')
336-
)
325+
req.headers.accept.includes('text/*') ||
326+
req.headers.accept.includes('*/*'))
337327
) {
338-
next();
339-
340-
return;
328+
req.url = '/index.html';
341329
}
342330

343-
req.url = '/index.html';
344331
next();
345332
}
346333

0 commit comments

Comments
 (0)