-
Notifications
You must be signed in to change notification settings - Fork 683
support for lazy routes ? #175
Comments
@kuldeepkeshwar I have not tried this out. Are you saying with the Angular Router, async routes don't work on the server side? |
@jeffwhelpley on client-side , we can define using the One way to do it is to replace |
Hi @kuldeepkeshwar I'm struggling with the same issue. I've tried this: {path: 'gallery', loadChildren: () => new Promise(resolve => {
(require as any).ensure([], require => {
resolve(require('./pages/some.module').SomeModule);
})
})}, Unfortunately it isn't working, this is the error i get: |
Ah, yes, I see now. In short, lazy loaded routes won't work on the server side. However, I can think of a couple hacks we can do depending on your specific use case. Is the route loading completely dynamic or do you know the routes ahead of time? Just give me some more details here @kuldeepkeshwar @flauc. |
In my case i know the route ahead of time. Thank you for taking the time and helping us 👍 By the way do you know if lazy loading will be supported in angular2-universal later on? |
@jeffwhelpley , yes we do know routes ahead of time . I tried to make it working ,the way I have mentioned above . |
So, in general, there is no point to dynamically load routes on the server side and no need to lazy load. The key then is to set it up so we can lazy load on the client, but not on the server. The easiest way to do this is if you are doing lazy loading via NgModule. Then it is just a matter of building your package as one on the server or separate for the client. If you are using a different custom method, however, then I suggest trying to either use DI swapping or |
Could you please provide a very minimal example of doing this? Just to give us an idea of what to do. |
I run into the same issue that @flauc is exhibiting with lazy Loading, any time a lazy loaded module is attempted to be accessed Chrome throws error Have tried with Both of these result in And when using |
@jeffwhelpley using DI swapping approach will increase the bundle size as it still refers to module in the code. |
@jeffwhelpley, @gdi2290, in a universal project, when requesting a page that is loaded lazily, I got error in server like "EXCEPTION: Uncaught (in promise): ReferenceError: System is not defined". I understand that this error is caused by lazy loading and it will not affect the server from serving other eagerly loaded pages. But the error is very misleading. It makes me think there's something wrong with my code. Is there any way to suppress the error (it's several-page long) and instead prompt that the cause, for example, "lazy loading route detected. it will cause ... ... error but not affecting further running of the server.". |
Looking into lazy routes in Universal. The problem is that it flickers on the client because the client is loading the route again. It works on the server and the initial render does show the lazy route. There’s also problem with Angular core’s way of preloading routes where they’re not actually waiting for the routes to load before navigating |
I'm not sure if I understand it correctly but when I use lazy modules with universal, they are downloaded all on first page load (instead of downloading specific module later on navigate to another lazy loaded page). It's actually problem with universal or do I understand something wrong? Without universal, lazy loaded modules worked correctly. |
open browser.module.ts and find RouterModule.forRoot([], { useHash: false, preloadingStrategy: IdlePreload }), and just replace with RouterModule.forRoot([], { useHash: false}), |
@gdi2290 Is there any way now to prevent the flickering? Like you said, it renders the view perfect, loads the view and shows it correctly - but in between it does flicker. Is there any workaround I can do? Anything? Thank you. |
hi @StefanRein I haven't tried it myself yet |
@matiishyn thank you, now it flickers only in this situation: It does not flicker, if I initially go to the start page (let's say /home, which is included in the bundle) and the lazy modules will be loaded in the background with the Except, I navigate directly to the Maybe it is possible to hook into some router lifecycle? CanLeave or CanActivate or something? RouterModule.forRoot([
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'lazy', loadChildren: './lazy/lazy.module#LazyModule'}
], {preloadingStrategy: PreloadAllModules}) Interesting sidenote: Giving the: |
I'm facing the same issue with the flicker on directly loading lazy routes. |
I'm using ui-router with lazyLoad, I say resolve the promise defined in export function onLoad($transition: TransitionService) {
return () => new Promise(resolve => $transition.onSuccess({}, (transition) => resolve()));
}
@NgModule({
....
providers: [
{
provide: APP_INITIALIZER,
useFactory: onLoad,
deps: [TransitionService],
multi: true,
},
],
... so basically it can be applied to |
@aitboudad thank you for the direction! @NgModule({
declarations: [],
imports: [
SharedModule,
AppRoutingModule
],
providers: [
{
provide: APP_INITIALIZER,
useFactory: (injector: Injector) => {
return () => {
const router: Router = injector.get(Router);
router.navigate(['/de-de/contact']); // => This has NO effect
console.log(router.url); // this logs: '/' instead of the 'domain.com/de-de/contact'
}
},
deps: [Injector],
multi: true
}
]
})
export class AppModule {
}
export { AppComponent } from './app.component'; No errors, but now the above logs: '/' instead of the 'domain.com/de-de/contact' in the address bar. Can anyone tell me please why the navigate does not work? It is definitely the correct instance. Thank you very much in advance. Edit: Funny: ...
useFactory: ...
if (isBrowser) {
(<any>router).location.go("/de-de/contact");
}
... Starting at (entering this URI in the browser): It seems like, something is triggering the Router to go to the root and then again up to the initial url. Maybe this leads anywhere: initialNavigation() : void If anyone has other suggestions, please don't mind sharing. |
I just tried APP_INITIALIZER and noticed that it blocks downloading the js for the async routes. Are you aware of that? |
seems |
Hello, anyone solved this issue? I'm facing the same issue with the flicker on directly loading lazy routes. |
Any idea guys? I think it's a real problem and we need to think together. |
@djabif @seklyza But: As I know you need to implement your own rehydration. But just look at how angular universal did it: Render the data from nodeJS into a global variable server side and later on, you use this one in your browser. |
@StefanRein 4.1.3 (It is a project for testing Universal & Lazy Loading) |
@seklyza And do you use angular universal or the new one in the core? Because these fixes weren't done in the angular universal, but in the new moved one. Please try it with the above mentioned, is it working there? |
I use @angular/platform-server |
@seklyza @djabif
in
Here https://angular.io/api/router/ExtraOptions#initialNavigation /**
* @whatItDoes Represents an option to configure when the initial navigation is performed.
*
* @description
* * 'enabled' - the initial navigation starts before the root component is created.
* The bootstrap is blocked until the initial navigation is complete.
* * 'disabled' - the initial navigation is not performed. The location listener is set up before
* the root component gets created.
* * 'legacy_enabled'- the initial navigation starts after the root component has been created.
* The bootstrap is not blocked until the initial navigation is complete. @deprecated
* * 'legacy_disabled'- the initial navigation is not performed. The location listener is set up
* after @deprecated
* the root component gets created.
* * `true` - same as 'legacy_enabled'. @deprecated since v4
* * `false` - same as 'legacy_disabled'. @deprecated since v4
*
* The 'enabled' option should be used for applications unless there is a reason to have
* more control over when the router starts its initial navigation due to some complex
* initialization logic. In this case, 'disabled' should be used.
*
* The 'legacy_enabled' and 'legacy_disabled' should not be used for new applications.
*
* @experimental
*/
export declare type InitialNavigation = true | false | 'enabled' | 'disabled' | 'legacy_enabled' | 'legacy_disabled'; |
@StefanRein Not working for me, when I don't use lazy loading I doesn't have flickering, but when I enable lazy loading the flickering appears.
|
@yamidvo Do you have the latest Router and Angular Core version? |
@StefanRein My angular version is 4.2.5 |
Hey we've updated this starter to include a |
@MarkPieszak I think the issue still exists. The |
Can you open up a new issue with some quick repro instructions? Just so we can differentiate it from this old issue. |
@MarkPieszak I think there will always be some flickering in cases similar to below code:
And I think the reason is, even when the SSR send response with user details, when this component initializes on client side it sets |
@naveedahmed1 Hey, did you solve it? Any news on this? |
These are supported now and should work without any issue. I would refer you to the thread angular/angular#23427 specifically my comment in the end angular/angular#23427 (comment) |
No description provided.
The text was updated successfully, but these errors were encountered: