Skip to content

fix(@angular-devkit/build-angular): construct SSR request URL using server resolvedUrls #26658

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
Dec 13, 2023
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 @@ -680,11 +680,9 @@ export async function setupServer(
}

transformIndexHtmlAndAddHeaders(url, rawHtml, res, next, async (html) => {
const protocol = serverOptions.ssl ? 'https' : 'http';
const route = `${protocol}://${req.headers.host}${req.originalUrl}`;
const { content } = await renderPage({
document: html,
route,
route: new URL(req.originalUrl ?? '/', server.resolvedUrls?.local[0]).toString(),
serverContext: 'ssr',
loadBundle: (uri: string) =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
4 changes: 4 additions & 0 deletions tests/legacy-cli/e2e.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ ESBUILD_TESTS = [
"tests/build/**",
"tests/commands/add/**",
"tests/commands/e2e/**",
"tests/commands/serve/ssr-http-requests-assets.js",
"tests/i18n/**",
"tests/vite/**",
"tests/test/**",
]

WEBPACK_IGNORE_TESTS = [
"tests/vite/**",
"tests/commands/serve/ssr-http-requests-assets.js",
"tests/build/prerender/http-requests-assets.js",
"tests/build/prerender/error-with-sourcemaps.js",
]

def _to_glob(patterns):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import assert from 'node:assert';

import { killAllProcesses, ng } from '../../../utils/process';
import { rimraf, writeMultipleFiles } from '../../../utils/fs';
import { installWorkspacePackages } from '../../../utils/packages';
import { ngServe, useSha } from '../../../utils/project';

export default async function () {
// Forcibly remove in case another test doesn't clean itself up.
await rimraf('node_modules/@angular/ssr');
await ng('add', '@angular/ssr', '--skip-confirmation');
await useSha();
await installWorkspacePackages();

await writeMultipleFiles({
// Add http client and route
'src/app/app.config.ts': `
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';

import { HomeComponent } from './home/home.component';
import { provideClientHydration } from '@angular/platform-browser';
import { provideHttpClient, withFetch } from '@angular/common/http';

export const appConfig: ApplicationConfig = {
providers: [
provideRouter([{
path: '',
component: HomeComponent,
}]),
provideClientHydration(),
provideHttpClient(withFetch()),
],
};
`,
// Add asset
'src/assets/media.json': JSON.stringify({ dataFromAssets: true }),
// Update component to do an HTTP call to asset.
'src/app/app.component.ts': `
import { Component, inject } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterOutlet } from '@angular/router';
import { HttpClient } from '@angular/common/http';

@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule, RouterOutlet],
template: \`
<p>{{ data | json }}</p>
<router-outlet></router-outlet>
\`,
})
export class AppComponent {
data: any;
constructor() {
const http = inject(HttpClient);
http.get('/assets/media.json').toPromise().then((d) => {
this.data = d;
});
}
}
`,
});

await ng('generate', 'component', 'home');
const match = /<p>{[\S\s]*"dataFromAssets":[\s\S]*true[\S\s]*}<\/p>/;
const port = await ngServe('--no-ssl');
assert.match(await (await fetch(`http://localhost:${port}/`)).text(), match);

await killAllProcesses();

try {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
const sslPort = await ngServe('--ssl');
assert.match(await (await fetch(`https://localhost:${sslPort}/`)).text(), match);
} finally {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '1';
}
}