Skip to content

Commit 2f4df6b

Browse files
committedDec 16, 2024·
fix(@angular/ssr): correctly resolve pre-transform resources in Vite SSR without AppEngine
Ensure proper resolution of pre-transform resources when using SSR in Vite without relying on AppEngine. Closes #29132 (cherry picked from commit 1bf9381)
1 parent db74212 commit 2f4df6b

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed
 

‎packages/angular/build/src/tools/vite/plugins/angular-memory-plugin.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import assert from 'node:assert';
1010
import { readFile } from 'node:fs/promises';
11-
import { dirname, join, relative } from 'node:path';
11+
import { basename, dirname, join, relative } from 'node:path';
1212
import type { Plugin } from 'vite';
1313
import { loadEsmModule } from '../../../utils/load-esm';
1414
import { AngularMemoryOutputFiles } from '../utils';
@@ -51,6 +51,18 @@ export async function createAngularMemoryPlugin(
5151
// Remove query if present
5252
const [importerFile] = importer.split('?', 1);
5353
source = '/' + join(dirname(relative(virtualProjectRoot, importerFile)), source);
54+
} else if (
55+
!ssr &&
56+
source[0] === '/' &&
57+
importer.endsWith('index.html') &&
58+
normalizePath(importer).startsWith(virtualProjectRoot)
59+
) {
60+
// This is only needed when using SSR and `angularSsrMiddleware` (old style) to correctly resolve
61+
// .js files when using lazy-loading.
62+
// Remove query if present
63+
const [importerFile] = importer.split('?', 1);
64+
source =
65+
'/' + join(dirname(relative(virtualProjectRoot, importerFile)), basename(source));
5466
}
5567
}
5668

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import assert from 'node:assert';
2+
import {
3+
execAndWaitForOutputToMatch,
4+
ng,
5+
silentNg,
6+
waitForAnyProcessOutputToMatch,
7+
} from '../../utils/process';
8+
import { installWorkspacePackages, uninstallPackage } from '../../utils/packages';
9+
import { useSha } from '../../utils/project';
10+
import { getGlobalVariable } from '../../utils/env';
11+
import { findFreePort } from '../../utils/network';
12+
import { writeFile } from '../../utils/fs';
13+
14+
export default async function () {
15+
assert(
16+
getGlobalVariable('argv')['esbuild'],
17+
'This test should not be called in the Webpack suite.',
18+
);
19+
20+
// Forcibly remove in case another test doesn't clean itself up.
21+
await uninstallPackage('@angular/ssr');
22+
await ng('add', '@angular/ssr', '--no-server-routing', '--skip-confirmation', '--skip-install');
23+
await useSha();
24+
await installWorkspacePackages();
25+
26+
await silentNg('generate', 'component', 'home');
27+
await writeFile(
28+
'src/app/app.routes.ts',
29+
`
30+
import { Routes } from '@angular/router';
31+
import {HomeComponent} from './home/home.component';
32+
33+
export const routes: Routes = [{
34+
path: 'sub/home',
35+
component: HomeComponent
36+
}];
37+
`,
38+
);
39+
40+
const port = await findFreePort();
41+
await execAndWaitForOutputToMatch('ng', ['serve', '--port', `${port}`], /complete/, {
42+
NO_COLOR: 'true',
43+
});
44+
45+
const [, response] = await Promise.all([
46+
assert.rejects(
47+
waitForAnyProcessOutputToMatch(/Pre-transform error: Failed to load url/, 8_000),
48+
),
49+
fetch(`http://localhost:${port}/sub/home`),
50+
]);
51+
52+
assert(response.ok, `Expected 'response.ok' to be 'true'.`);
53+
}

0 commit comments

Comments
 (0)
Please sign in to comment.