Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): return 404 for assets that are no…
Browse files Browse the repository at this point in the history
…t found

This commit updates the vite dev-server to return 404 for assets and files that are not found.

Closes angular#26917
  • Loading branch information
alan-agius4 committed Jan 24, 2024
1 parent c57c2b6 commit b4cf68d
Showing 1 changed file with 6 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,6 @@ export function createAngularMemoryPlugin(options: AngularMemoryPluginOptions):
// The base of the URL is unused but required to parse the URL.
const pathname = pathnameWithoutBasePath(req.url, server.config.base);
const extension = extname(pathname);
if (!extension) {
next();

return;
}

// Rewrite all build assets to a vite raw fs URL
const assetSourcePath = assets.get(pathname);
Expand Down Expand Up @@ -323,24 +318,16 @@ function angularHtmlFallbackMiddleware(
// Similar to how it is handled in vite
// https://github.com/vitejs/vite/blob/main/packages/vite/src/node/server/middlewares/htmlFallback.ts#L15C19-L15C45
if (
// Only accept GET or HEAD
(req.method !== 'GET' && req.method !== 'HEAD') ||
// Has file extensions
(req.url && lookupMimeTypeFromRequest(req.url)) ||
// Require Accept: text/html or */*
!(
req.headers.accept === undefined || // equivalent to `Accept: */*`
req.headers.accept === '' || // equivalent to `Accept: */*`
(req.method === 'GET' || req.method === 'HEAD') &&
(!req.url || !lookupMimeTypeFromRequest(req.url)) &&
(!req.headers.accept ||
req.headers.accept.includes('text/html') ||
req.headers.accept.includes('*/*')
)
req.headers.accept.includes('text/*') ||
req.headers.accept.includes('*/*'))
) {
next();

return;
req.url = '/index.html';
}

req.url = '/index.html';
next();
}

Expand Down

0 comments on commit b4cf68d

Please sign in to comment.