Skip to content

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

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

Closed
davemoser opened this issue Apr 14, 2025 · 2 comments
Milestone

Comments

@davemoser
Copy link

davemoser commented Apr 14, 2025

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
@ngbot ngbot bot added this to the needsTriage milestone Apr 14, 2025
@JeanMeche JeanMeche transferred this issue from angular/angular Apr 14, 2025
@alan-agius4
Copy link
Collaborator

Yes, this is expected during route extraction, which occurs on the first request or at build time

@alan-agius4 alan-agius4 closed this as not planned Won't fix, can't repro, duplicate, stale Apr 14, 2025
@davemoser
Copy link
Author

Thank you for your quick feedback! 👌 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants