Skip to content
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

[FR] APP_BASE_HREF useFactory Promise Support #25932

Closed
maks-humeniuk opened this issue Sep 12, 2018 · 13 comments
Closed

[FR] APP_BASE_HREF useFactory Promise Support #25932

maks-humeniuk opened this issue Sep 12, 2018 · 13 comments
Labels
area: common Issues related to APIs in the @angular/common package breaking changes feature: insufficient votes Label to add when the not a sufficient number of votes or comments from unique authors feature: votes required Feature request which is currently still in the voting phase feature Issue that requests a new feature freq1: low
Milestone

Comments

@maks-humeniuk
Copy link

I'm submitting a...


[x] Feature request

Current behavior

APP_BASE_HREF useFactory doesn't support promises, only values.

Expected behavior

APP_BASE_HREF useFactory supports both values and promises.

What is the motivation / use case for changing the behavior?

In my application I need to set APP_BASE_HREF asynchronously. Unfortunately, APP_BASE_HREF useFactory doesn't support promises. So, I tried this approach with APP_INITIALIZER, but failed as well as OP, because it seems like if you do AJAX requests in APP_INITIALIZER useFactory, you should setup APP_BASE_HREF first.

APP_INITIALIZER supports promises in useFactory, so it would be very useful if APP_BASE_HREF supports them too.

Environment


Angular version: 6.0.0

Browser:
- [x] Chrome (desktop) version 69
@mlc-mlapis
Copy link
Contributor

mlc-mlapis commented Sep 12, 2018

@Halfist ... ConfigService as the dependency has to specify the value and this is available for the factory function which returns the final value.
export function getBaseHref(configService: ConfigService): string {return configService.myBaseHref;}
{ provide: APP_BASE_HREF, useFactory: getBaseHref, deps: [ConfigService] }

@maks-humeniuk
Copy link
Author

@mlc-mlapis, this is pretty much the same. configService.myBaseHref should be initialized first. In my case this initialization should occur asynchronously. Since APP_BASE_HREF useFactory doesn't support promises, I initialize it in APP_INITIALIZER useFactory. But APP_BASE_HREF useFactory executes before APP_INITIALIZER useFactory promise resolves. So, configService.myBaseHref is undefined.

@mlc-mlapis
Copy link
Contributor

mlc-mlapis commented Sep 12, 2018

@Halfist ... how so when ConfigService is the dependency for both? ConfigService has to be instantiated first.

@mlc-mlapis
Copy link
Contributor

mlc-mlapis commented Sep 12, 2018

@Halfist ... OK, I can see now. So you have to use some environment direct import or sync request.

@mlc-mlapis
Copy link
Contributor

mlc-mlapis commented Sep 12, 2018

@Halfist ... just for curiosity ... what is printed when you inject location into the 'ConfigService`?

import { Location } from '@angular/common';
...
constructor(public location: Location) {
   console.log(location.prepareExternalUrl('/'));
}

@maks-humeniuk
Copy link
Author

@mlc-mlapis, I just figured out that the requested feature won't help me at all. The irony is that I need to get base path from AJAX request in APP_BASE_HREF useFactory. But in order to make the request, base path should already be defined, otherwise the request URL prefix is undefined. If I'm right, the issue could be closed.

@mlc-mlapis
Copy link
Contributor

@Halfist ... OK.

@maks-humeniuk
Copy link
Author

maks-humeniuk commented Sep 13, 2018

Though, it could be helpful for other scenarios. Let it remain opened then.

@benlesh benlesh added the area: common Issues related to APIs in the @angular/common package label Sep 13, 2018
@ngbot ngbot bot added this to the needsTriage milestone Sep 13, 2018
@matsko matsko added freq1: low feature Issue that requests a new feature labels Apr 25, 2019
@ngbot ngbot bot modified the milestones: needsTriage, Backlog Apr 25, 2019
@VitaliPark
Copy link

VitaliPark commented Jan 22, 2020

Here's another use case, when supporting promises in APP_BASE_HREF might be useful:
You need to set app base href dynamically using external configuration.

  • In order to get this external configuration you can use APP_INITIALIZER (download config file via http call)
  • But then, in useFactory of APP_BASE_HREF provider you need to wait for this call to finish to get config. And since you can only use value there, it's not possible (correct me if I'm wrong).
    Seems like currently the only way to do so, is to move config loading into main.ts file, and bootstrap application only after all configs are loaded and pass config in extra providers like so (again correct me if I'm wrong) :
fetchConfig().then(config => {
	platformBrowserDynamic([{provide: ConfigData, useValue: config}])
		.bootstrapModule(AppModule)
		.catch(err => console.log(err));
});

Which seems like a hack, especially when APP_INITIALIZER is used specifically for app initialization, but you can't use it in this particular case.

The same issue is described here: https://stackoverflow.com/questions/46190280/angular-2-set-app-base-href-with-a-value-from-a-promise-observable

@mightymatth
Copy link

Is there any progress about that? This strongly limits our use-case as we want to have base app href dynamically configurable. Thanks.

@angular-robot angular-robot bot added the feature: votes required Feature request which is currently still in the voting phase label Jun 4, 2021
@angular-robot
Copy link
Contributor

angular-robot bot commented Jun 4, 2021

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.

@angular-robot
Copy link
Contributor

angular-robot bot commented Jun 25, 2021

Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.

We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

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

@angular-robot angular-robot bot added the feature: insufficient votes Label to add when the not a sufficient number of votes or comments from unique authors label Jun 25, 2021
@alxhub alxhub closed this as completed Feb 1, 2022
@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Mar 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: common Issues related to APIs in the @angular/common package breaking changes feature: insufficient votes Label to add when the not a sufficient number of votes or comments from unique authors feature: votes required Feature request which is currently still in the voting phase feature Issue that requests a new feature freq1: low
Projects
None yet
Development

No branches or pull requests

7 participants