Skip to content

Component bootstrapping during route extraction is not disabled when using provideAppInitializer() #29131

Closed

Description

@stewieoO

Command

serve, build

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

The issue of component bootstrapping was discussed in #29085 and fixed in version 19.0.5 with #29101

This generally works, except when you're using an app initializer.

Minimal Reproduction

I have created a repro repo here.

When you run npm start and open the page, you can see that the console log inside the app component (and route guard) is called without a REQUEST present (indicating the route extraction process).

When you comment out the provideAppInitializer() in the app.config.ts this doesn't happen anymore (as expected).

Exception or Error


Your Environment

_                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 19.0.5
Node: 20.11.1
Package Manager: pnpm 9.12.2
OS: win32 x64

Angular: 19.0.4
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, platform-server
... router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1900.5 (cli-only)
@angular-devkit/build-angular   19.0.5
@angular-devkit/core            19.0.5 (cli-only)
@angular-devkit/schematics      19.0.5 (cli-only)
@angular/cli                    19.0.5
@angular/ssr                    19.0.5
@schematics/angular             19.0.5 (cli-only)
rxjs                            7.8.1
typescript                      5.6.3
zone.js                         0.15.0

Anything else relevant?

No response

Activity

  1. self-assigned this
    on Dec 13, 2024
  2. added 4 commits that reference this issue on Dec 19, 2024
    28c5d2b
    f11f65a
    7c462af
    3feecdd
  3. stewieoO commented on Jan 16, 2025

    @stewieoO
    Author

    @alan-agius4 First of all thanks for fixing this.
    I ran into another case:

    If i inject a service in the provideAppInitializer

    provideAppInitializer(() => {
      const serv = inject(MyService);
    })

    That service will be constructed during route extraction

    @Injectable()
    export class MyService {
      constructor() {
        const request = inject(REQUEST, { optional: true });
        console.log('Service Called', request != null);
      }
    }

    Getting the log with request being null.

    So the question being, another bug or expected behavior?

  4. alan-agius4 commented on Jan 16, 2025

    @alan-agius4
    Collaborator

    Running the application initializers during route extraction is the intended behavior as of now.

    You can check if REQUEST is null in provideAppInitializer and exit early to prevent unnecessary execution.

    That said, you’re welcome to submit a feature request if you'd like the ability to disable running all application initializers.

  5. stewieoO commented on Jan 16, 2025

    @stewieoO
    Author

    Running the application initializers during route extraction is the intended behavior as of now.

    Got it. Thanks for clarifying.

    You can check if REQUEST is null in provideAppInitializer and exit early to prevent unnecessary execution.

    Yeah, that's what i did as a workaround anyways. Combined with a check that it's on the server. As in the browser REQUEST is null of course 😆

    That said, you’re welcome to submit a feature request if you'd like the ability to disable running all application initializers.

    Thanks, might do that.

  6. angular-automatic-lock-bot commented on Feb 16, 2025

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  7. locked and limited conversation to collaborators on Feb 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions