saymedia / angularjs-server

Specialized server for AngularJS sites
MIT License
248 stars 49 forks source link

Error: location not available yet #41

Open alexmarch opened 7 years ago

alexmarch commented 7 years ago

When angularjs-server bootstrap my app i have next error: location not available yet

apparentlymart commented 7 years ago

Hi @alexmarch! Sorry for this annoying error.

This is unfortunately one of the ways in which the AngularJS-abstraction leaks due to differences in the request model between the browser and the server. Any JavaScript code running in a browser is always in the context of a particular URL that exists outside of AngularJS, but when running on the server the request URL is passed in as just another AngularJS module and the sequence of events is such that early initialization code (in providers) can run before it's available.

With the current design, it's necessary for an application to be adapted to these constraints. It may be possible to improve on this by using the lower-level API to construct the context in a different way, but I'm not sure.

This library has been used primarily in an application that is tailored to work well within it, so unfortunately there hasn't been a strong incentive to improve the abstraction since it's generally been easier to adapt the application to work within these additional constraints.

Usama-Tahir commented 6 years ago

so any workaround to cater this problem? @apparentlymart

apparentlymart commented 6 years ago

The only workaround is to avoid using $location during bootstrapping, and instead wait to do location-sensitive operations after the application has bootstrapped.

Please note that AngularJS-Server is no longer under active development because Angular Universal is likely to be a better solution for new development. It has a similar high-level approach to AngularJS-Server but is first-class.

Additionally, Say Media is now solving this problem a different way (by not using AngularJS to render pages at all) and so AngularJS-Server is no longer running in production there.