In the latest version of Angular, we can configure the routes to render on the server or on the client based on our needs.
Create a file called app.routes.server.ts
in the src/app
folder.
Create a configuration object that contains an array of ServerRoute
objects.
serverRoutes Config
import { RenderMode, ServerRoute } from '@angular/ssr';
export const serverRoutes: ServerRoute[] = [];
Go to src/app/app.config.server.ts
and add the serverRoutes
configuration using provideServerRoutesConfig
provider function.
serverRoutes Config
+ import { provideServerRoutesConfig } from '@angular/ssr';
+ import { serverRoutes } from './app.routes.server';
const serverConfig: ApplicationConfig = {
providers: [
provideServerRendering(),
+ provideServerRoutesConfig(serverRoutes),
],
};
The my-movies
route doesn't need to be rendered on the server, so we can configure it to render on the client.
Solution
export const serverRoutes: ServerRoute[] = [
+ {
+ path: 'my-movies',
+ renderMode: RenderMode.Client,
+ },
];
The list/:category
route needs to be rendered on the server, so we can configure it to render on the server.
Solution
export const serverRoutes: ServerRoute[] = [
{
path: 'my-movies',
renderMode: RenderMode.Client,
},
+ {
+ path: 'list/:category',
+ renderMode: RenderMode.Server,
+ },
];
We can grab all the other routes using the **
wildcard and configure them to render on the client.
Solution
export const serverRoutes: ServerRoute[] = [
{
path: 'my-movies',
renderMode: RenderMode.Client,
},
{
path: 'list/:category',
renderMode: RenderMode.Server,
},
+ {
+ path: '**',
+ renderMode: RenderMode.Client,
+ },
];
After running the application, you should see that the my-movies
route is rendered on the client and the list/:category
route is rendered on the server.
Great job 👏! You have learned how to configure the routes to render on the server or on the client based on your needs.