angular-cli/packages/angular/ssr/test/testing-utils.ts
Alan Agius d66aaa3ca4 feat(@angular/ssr): add server routing configuration API
This commit introduces a new server routing configuration API, as discussed in RFC https://github.com/angular/angular/discussions/56785. The new API provides several enhancements:

```ts
const serverRoutes: ServerRoute[] = [
  {
    path: '/error',
    renderMode: RenderMode.Server,
    status: 404,
    headers: {
      'Cache-Control': 'no-cache'
    }
  }
];
```

```ts
const serverRoutes: ServerRoute[] = [
  {
    path: '/product/:id',
    renderMode: RenderMode.Prerender,
    async getPrerenderPaths() {
      const dataService = inject(ProductService);
      const ids = await dataService.getIds(); // Assuming this returns ['1', '2', '3']
      return ids.map(id => ({ id })); // Generates paths like: [{ id: '1' }, { id: '2' }, { id: '3' }]
    }
  }
];
```

```ts
const serverRoutes: ServerRoute[] = [
  {
    path: '/product/:id',
    renderMode: RenderMode.Prerender,
    fallback: PrerenderFallback.Server, // Can be Server, Client, or None
    async getPrerenderPaths() {
    }
  }
];
```

```ts
const serverRoutes: ServerRoute[] = [
  {
    path: '/product/:id',
    renderMode: RenderMode.Server,
  },
  {
    path: '/error',
    renderMode: RenderMode.Client,
  },
  {
    path: '/**',
    renderMode: RenderMode.Prerender,
  },
];
```

These additions aim to provide greater flexibility and control over server-side rendering configurations and prerendering behaviors.
2024-09-12 19:59:05 +02:00

79 lines
2.5 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 { 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 = '',
): void {
setAngularAppManifest({
inlineCriticalCss: false,
assets: new Map(
Object.entries({
'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),
],
});
},
});
}