Skip to content

Expose resolved route title #46825

Closed
Closed

Description

@EmmanuelRoux

Which @angular/* package(s) are relevant/related to the feature request?

router

Description

Since Angular 14, router has a new mechanism to update page title from route config. The route can either be a static string or resolved in the same way as resolve data.

But the resolved route title is not exposed and inaccessible from client application (it seems the title is actually stored in the resolved Data, but using a private Symbol so it can't be accessed properly).

Proposed solution

Expose a new title property on ActivatedRoute and ActivatedRouteSnapshot, just like existing params, data...

Alternatives considered

Alternative 1: Expose the internal RouteTitle symbol used to store title in Data, but this is probably not wanted because it would expose an implementation detail.

Alternative 2: Use TitleStrategy.buildTitle(snapshot: RouterStateSnapshot) or, better, TitleStrategy.getResolvedTitleForRoute(snapshot: ActivatedRouteSnapshot). But this has following drawbacks:

  • TitleStrategy must be injected where we need to (only) read the route title, which is not its primary purpose...
  • TitleStrategy is not very convenient to inject, because in default case, DefaultTitleStrategy is used but is not available through DI with TitleStrategy token. To get the actual strategy used, one must retrieve it from Router.titleStrategy.
  • This is inconsistent with existing public api of ActivatedRoute/ActivatedRouteSnapshot

Activity

  1. added this to the needsTriage milestone on Jul 13, 2022
  2. added
    featureLabel used to distinguish feature request from other issues
    on Jul 13, 2022
  3. modified the milestones: needsTriage, Backlog on Jul 13, 2022
  4. atscott commented on Jul 13, 2022

    @atscott
    Contributor

    @EmmanuelRoux Thanks for the request. Can you describe your use-case for needing the resolved title on each individual ActivatedRoute? Given that the browser title is effectively a singleton, what's the use-case for looking at each individually resolved title?

    I don't agree with the first two bullet points of your argument but can certainly get behind the third. For me, the third point is a good enough reason to move forward with the change.

    P.S. Changing the title strategy should be done by providing your own through DI, not directly modifying the property on the Router. Unfortunately, the property was exposed in order to match the other strategy patterns used. I'll make a note to investigate making all of these private as a breaking change in v15.

  5. angular-robot commented on Jul 14, 2022

    @angular-robot
    Contributor

    This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list.

    You can find more details about the feature request process in our documentation.

  6. EmmanuelRoux commented on Jul 15, 2022

    @EmmanuelRoux
    ContributorAuthor

    Thanks for the feedback!

    My use case is specific to a lib which needs to retrieve the title defined for each pages, but I think it may apply to many projects.

    Given that the browser title is effectively a singleton, what's the use-case for looking at each individually resolved title?

    I understand and totally agree with you, but despite DefaultTitleStrategy will update the document title, there is actually absolutely no guarantee the user-chosen strategy will.

    P.S. Changing the title strategy should be done by providing your own through DI, not directly modifying the property on the Router. Unfortunately, the property was exposed in order to match the other strategy patterns used. I'll make a note to investigate making all of these private as a breaking change in v15.

    That's not exactly what I tried to explain: I just describe how to retrieve the actual title strategy (because it could not be retrieved via DI in case the default one is used - if I understand correctly).

  7. atscott commented on Jul 16, 2022

    @atscott
    Contributor

    I understand and totally agree with you, but despite DefaultTitleStrategy will update the document title, there is actually absolutely no guarantee the user-chosen strategy will.

    That's not exactly what I tried to explain: I just describe how to retrieve the actual title strategy (because it could not be retrieved via DI in case the default one is used - if I understand correctly).

    Unless I'm still not understanding your explanation, what you're saying is not true. The router uses the DefaultTitleStrategy by default only. You can provide your own title strategy using {provide: TitleStrategy, useClass: MyCustomStrategy} and that's the one that's used. https://stackblitz.com/edit/angular-ivy-kslayx

  8. EmmanuelRoux commented on Jul 16, 2022

    @EmmanuelRoux
    ContributorAuthor

    Sorry I was not clear!

    I'm speaking here as a library author, so I have no control on the strategy the app developer will define in the app.

    That said, at some point I need to know what title has been defined for a given ActivatedRoute.

    What I'm saying is: in order to retrieve the title defined by the app developer, there are several choices but none of them seems good for me:

    • I can read in the route config, but because the title may be a Resolve<string> this is not easy
    • I can inject the TitleStrategy and call getResolvedTitleForRoute, just like your example: but this will not work if the user is using the default strategy, except by using something like this: inject(TitleStrategy, InjectFlags.Optional) || inject(DefaultTitleStrategy)
    • I can read the document title (either from DOM or by using Title service): but currently nothing guarantees that a custom title strategy will actually update document title (although it is surely not a very common scenario)
  9. atscott commented on Jul 18, 2022

    @atscott
    Contributor

    @EmmanuelRoux Thanks, I understand now. I appreciate the detailed explanation. It helps a lot to justify why we make the change. Even though it's just a 1 liner in each of the ActivatedRoute and ActivatedRouteSnapshot, it's still good to have concrete reasoning.

  10. angular-robot commented on Aug 23, 2022

    @angular-robot
    Contributor

    Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

    Find more details about Angular's feature request process in our documentation.

  11. EmmanuelRoux commented on Aug 30, 2022

    @EmmanuelRoux
    ContributorAuthor

    Fixed by #46826

  12. angular-automatic-lock-bot commented on Sep 30, 2022

    @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.

  13. locked and limited conversation to collaborators on Sep 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: routerfeatureLabel used to distinguish feature request from other issues

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions