angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

First page is loaded twice in universal app

Open sravikiran opened this issue 9 years ago • 49 comments

Bug Report or Feature Request (mark with an x)

bug report

Versions.

λ ng --version @angular/cli: 1.3.0 node: 6.11.0 os: win32 x64 @angular/animations: 4.3.4 @angular/common: 4.3.4 @angular/compiler: 4.3.4 @angular/core: 4.3.4 @angular/forms: 4.3.4 @angular/http: 4.3.4 @angular/platform-browser: 4.3.4 @angular/platform-browser-dynamic: 4.3.4 @angular/router: 4.3.4 @angular/cli: 1.3.0 @angular/compiler-cli: 4.3.4 @angular/language-service: 4.3.4 @angular/platform-server: 4.3.4

λ node --version v6.11.0

λ npm --version v3.9.3

Running on Windows 10

Repro steps.

I built a sample repo using angular-cli and followed the steps in the Universal Rendering story to enable server side rendering. The application loads well on running, but I see the client rendering also happening after the page is served from the server. So the page gets rendered from the server and then the content disappears for a second as the client rendering happens.

To reproduce this, you can clone this repository, install the packages and run the following commands:

  • ng build && ng build -prod -app 1 --output-hashing none
  • node server.js

I am using the dist/index.html file instead of src/index.html, as on using the file from the src causes full page reload when I switch between the routes.

The log given by the failure.

No log, as there are no errors from npm and angular-cli

Desired functionality.

Client rendering shouldn't happen after the page is loaded from the server

sravikiran avatar Aug 23 '17 04:08 sravikiran

@hesampour Have you also used prerender?

hinalshah91 avatar Jul 25 '18 12:07 hinalshah91

yes

hesampour avatar Jul 25 '18 12:07 hesampour

The transfer state works now. However, we have one api which returns different response for different slug parameter we pass. So, if I use transfer state for that Api call with one slug parameter on one page and transfer state for same api call with different slug parameter on another page, it returns the previous response. Any solution for this?

hinalshah91 avatar Aug 07 '18 09:08 hinalshah91

I have been searching for a proper solution for this issue. Seems like Transfer-State is the solution. But in my case I have used Lazy Loading on Angular too. So the issue persists. I managed to fix it by using,

@NgModule({
  imports: [RouterModule.forRoot(routes, { initialNavigation: 'enabled' }) ],
  exports: [RouterModule]
})

Try this with your project. It will resolve your flickering or reloading twice issue.

Hiranlive avatar Oct 20 '18 07:10 Hiranlive

using initialNavigation: 'enabled' removes the flickering, but in case you have a resolver in one of the lazy loaded routes, the resolver runs even before app.component.ts does, causing some weird behaviour. if i removed initialNavigation: 'enabled', app.component.ts runs first thing, and the state is loaded in my app correctly and initially, the behaviour isnt the same for initialNavigation: 'enabled'

sirajtahra avatar Nov 23 '18 09:11 sirajtahra

@sirajtahra using {initialNavigation: 'enabled'} will only solve the problem partially. If u see the page getting load in frames you will see flickering still happening but will not be visible to user as it happen for a very short time. I am using angular universal and currently facing the same issue

vggarg96 avatar Jun 21 '19 08:06 vggarg96

Hey guys, just wanted to provide my solution too. What i did to make it work is use the {initialNavigation: 'enabled'} but also implemented an interceptor creating and handling states transfer for requests caching. Using both of these solutions fixed all flickering on my side. Quick note: when using a transfer state interceptor, if your UI has duplicate calls to the api which you may need to have those calls in (even though i would not recommend), for instance, two sub components need to get same data from server, then dont delete the key from the state within your interceptor, otherwise, when browser boots it won't find the data that it needs and it will request data to server and re-render DOM.

// this.transferState.remove(key); // commented out

AndreasKz avatar Aug 02 '19 07:08 AndreasKz

In my case the issue was i had similar routes i.e for HomeComponent i was using '' and for ItemComponent i was using ':id/:category' so i had to change my ItemComponent to use 'item/:id/:category' as below:

RouterModule.forRoot([ {path: '', component: HomeComponent}, {path: 'item/:id/:category', component: ItemComponent} ])

joeabala avatar Aug 18 '19 00:08 joeabala

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.