Skip to content

Commit

Permalink
fix(@angular/build): prevent fallback to serving main.js for unknown …
Browse files Browse the repository at this point in the history
…requests

Previously, when an unknown `main.js` file was requested, the system would
automatically fall back to serving the default `main.js`. This behavior
could cause unexpected issues, such as incorrect resource loading or
misleading errors.

This fix ensures that only valid `main.js` files are served, preventing
unintended fallbacks and improving request handling.

Closes #29524
  • Loading branch information
alan-agius4 committed Jan 29, 2025
1 parent 8971745 commit 9a46be8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
expect(await response?.headers.get('Location')).toBe('/login/');
});

it('serves a JavaScript asset named as a bundle', async () => {
it('serves a JavaScript asset named as a bundle (main.js)', async () => {
await harness.writeFile('public/test/main.js', javascriptFileContent);

setupTarget(harness, {
Expand All @@ -162,5 +162,17 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
expect(result?.success).toBeTrue();
expect(await response?.text()).toContain(javascriptFileContent);
});

it('should return 404 when a JavaScript asset named as a bundle (main.js) does not exist', async () => {
setupTarget(harness, {});

harness.useTarget('serve', {
...BASE_OPTIONS,
});

const { result, response } = await executeOnceAndFetch(harness, 'unknown/main.js');
expect(result?.success).toBeTrue();
expect(response?.status).toBe(404);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,11 @@ export async function setupServer(
},
// This is needed when `externalDependencies` is used to prevent Vite load errors.
// NOTE: If Vite adds direct support for externals, this can be removed.
preTransformRequests: externalMetadata.explicitBrowser.length === 0,
// NOTE: Vite breaks the resolution of browser modules in SSR
// when accessing a url with two or more segments (e.g., 'foo/bar'),
// as they are not re-based from the base href.
preTransformRequests:
externalMetadata.explicitBrowser.length === 0 && ssrMode === ServerSsrMode.NoSsr,
},
ssr: {
// Note: `true` and `/.*/` have different sematics. When true, the `external` option is ignored.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,12 @@ export async function createAngularMemoryPlugin(
return source;
}

if (importer) {
if (importer && source[0] === '.') {
const normalizedImporter = normalizePath(importer);
if (source[0] === '.' && normalizedImporter.startsWith(virtualProjectRoot)) {
if (normalizedImporter.startsWith(virtualProjectRoot)) {
// Remove query if present
const [importerFile] = normalizedImporter.split('?', 1);
source = '/' + join(dirname(relative(virtualProjectRoot, importerFile)), source);
} else if (
!ssr &&
source[0] === '/' &&
importer.endsWith('index.html') &&
normalizedImporter.startsWith(virtualProjectRoot)
) {
// This is only needed when using SSR and `angularSsrMiddleware` (old style) to correctly resolve
// .js files when using lazy-loading.
// Remove query if present
const [importerFile] = normalizedImporter.split('?', 1);
source =
'/' + join(dirname(relative(virtualProjectRoot, importerFile)), basename(source));
}
}

Expand Down

0 comments on commit 9a46be8

Please sign in to comment.