mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-17 19:13:34 +08:00
This commit implements the capability for the App Engine to serve prerendered pages directly. Previously, we relied on frameworks like Express for this functionality, which resulted in inconsistent redirects for directories where in some cases a trailing slash was added to the route. **Note:** This change applies only when using the new SSR APIs. When using the `CommonEngine`, a 3rd party static serve middleware is still required.
81 lines
2.6 KiB
TypeScript
81 lines
2.6 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright Google LLC All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
* found in the LICENSE file at https://angular.dev/license
|
|
*/
|
|
|
|
import { Component, provideExperimentalZonelessChangeDetection } from '@angular/core';
|
|
import { bootstrapApplication } from '@angular/platform-browser';
|
|
import { provideServerRendering } from '@angular/platform-server';
|
|
import { RouterOutlet, Routes, provideRouter } from '@angular/router';
|
|
import { AngularAppManifest, ServerAsset, setAngularAppManifest } from '../src/manifest';
|
|
import { ServerRoute, provideServerRoutesConfig } from '../src/routes/route-config';
|
|
|
|
/**
|
|
* Configures the Angular application for testing by setting up the Angular app manifest,
|
|
* configuring server-side rendering, and bootstrapping the application with the provided routes.
|
|
* This function generates a basic HTML template with a base href and sets up the necessary
|
|
* Angular components and providers for testing purposes.
|
|
*
|
|
* @param routes - An array of route definitions to be used by the Angular Router.
|
|
* @param serverRoutes - An array of ServerRoute definitions to be used for server-side rendering.
|
|
* @param [baseHref=''] - An optional base href to be used in the HTML template.
|
|
*/
|
|
export function setAngularAppTestingManifest(
|
|
routes: Routes,
|
|
serverRoutes: ServerRoute[],
|
|
baseHref = '',
|
|
additionalServerAssets: Record<string, ServerAsset> = {},
|
|
): void {
|
|
setAngularAppManifest({
|
|
inlineCriticalCss: false,
|
|
assets: new Map(
|
|
Object.entries({
|
|
...additionalServerAssets,
|
|
'index.server.html': async () =>
|
|
`<html>
|
|
<head>
|
|
<title>SSR page</title>
|
|
<base href="/${baseHref}" />
|
|
</head>
|
|
<body>
|
|
<app-root></app-root>
|
|
</body>
|
|
</html>
|
|
`,
|
|
'index.csr.html': async () =>
|
|
`<html>
|
|
<head>
|
|
<title>CSR page</title>
|
|
<base href="/${baseHref}" />
|
|
</head>
|
|
<body>
|
|
<app-root></app-root>
|
|
</body>
|
|
</html>
|
|
`,
|
|
}),
|
|
),
|
|
bootstrap: async () => () => {
|
|
@Component({
|
|
standalone: true,
|
|
selector: 'app-root',
|
|
template: '<router-outlet />',
|
|
imports: [RouterOutlet],
|
|
})
|
|
class AppComponent {}
|
|
|
|
return bootstrapApplication(AppComponent, {
|
|
providers: [
|
|
provideServerRendering(),
|
|
provideExperimentalZonelessChangeDetection(),
|
|
provideRouter(routes),
|
|
provideServerRoutesConfig(serverRoutes),
|
|
],
|
|
});
|
|
},
|
|
});
|
|
}
|