Skip to content

fix(@angular/build): prevent fallback to serving main.js for unknown requests #29527

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
Jan 29, 2025
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 @@ -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