What is the best solution to solve 'requestAnimationFrame is not defined' ?

384 views
Skip to first unread message

Junior Dev

unread,
Mar 8, 2019, 7:13:20 AM3/8/19
to Angular and AngularJS discussion
I build an Angular Universal Project (by ng-toolkit/universal) and run server project with nodejs 
but I got an Error : requestAnimationFrame is not defined.
What is the best solution to solve this problem? 


Thank you so much,
Junior Frontend Dev.

Sander Elias

unread,
Mar 8, 2019, 8:58:22 AM3/8/19
to Angular and AngularJS discussion
During SSR you don't have a browser handy, so you can't use the browsers API to prerender your page

Yothin Intrsuk

unread,
Mar 8, 2019, 10:58:26 PM3/8/19
to ang...@googlegroups.com
Hi Master Sander,

Thank you so much for your anwser.
But I do not find how to take this error out. 
Please guide me more.

Sorry for my junior skill.

Best Regards,
Junior Frontend Dev.

On Fri, 8 Mar 2019 at 20:58 Sander Elias <sande...@gmail.com> wrote:
During SSR you don't have a browser handy, so you can't use the browsers API to prerender your page

--
You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Sander Elias

unread,
Mar 11, 2019, 2:23:37 AM3/11/19
to Angular and AngularJS discussion
Hi Junior,

Not a problem. Search for requestAnimationFrame, and make sure it's only called when you are in a browser, and not on the server.
You can do that by checking if there is a nativeElement on a components ElementRef

class blahComponent implements OnInit {
constructor(private elRef:ElementRef) { }

ngOnInit() {
if (this.elRef.nativeElement) {
/** here goes your code */
}
}
}

Hope this helps,
Regards
Sander

Reply all
Reply to author
Forward
0 new messages