Skip to content

AppInitializer is called on server with new ServerRoutes even if all routes are client side rendered #30103

@davemoser

Description

@davemoser

Which @angular/* package(s) are the source of the bug?

ssr, platform-server

Is this a regression?

No

Description

This is more of a comprehension question regarding the new ServerRoutes than an actual bug. But if the behavior of it is not intended, it would be a bug.

TL;DR

Using angular-vite and server routes: Is it intended that the AppInitializer function is called on the server side even if I have set all Routes to be RenderMode.Client?

Issue / Question

I have a nx monorepo where I have multiple angular apps. Some of them use server side rendering, some not. Based on the old SSR approach, I have different server implementations for those apps. Now with the ServerRoutes I thought of unifying the server part for those apps.

Having the apps with SSR being configured this way:

export const serverRoutes: ServerRoute[] = [
  {
    path: '**',
    renderMode: RenderMode.Server
  }
];

and the apps that do not support SSR set to:

export const serverRoutes: ServerRoute[] = [
  {
    path: '**',
    renderMode: RenderMode.Client
  }
];

While this works in general, I have an issue with the apps, that are client side rendered. When I start the '@angular-devkit/build-angular:dev-server' and load the app for the first time, the function I pass to 'provideAppInitializer' is exectued on the server. I get the outputs to the server logs. For all consecutive calls, it's not exectued anymore.
This is an issue for me, because the AppInitializer function throws an error on the server side. So whenever I start the server, I get an erorr, before it then actually works fine.

Now the easy workaround would be to just check in the AppInitializer if the code is running on server side and if so, just abort (because the app seems not to be bootstrapped further than the AppInitializer, this works fine).
But I thought I am first gonna ask here, if this is even the intended behavior or if this is considered a bug and should actually be fixed.

Example on how the AppInitializer of my client side rendered app might look:

    provideAppInitializer(() => {
      console.log('This will be logged to the dev-server-console once on first load of the page');
    }),

If you need a reproduction, just let me know, then I'll create a small app with the most current angular version. 😄
Thanks for any feedback, even if you tell me that my use case is too niche.

Please provide the environment you discovered this bug in (run ng version)

Angular 19.2.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions