question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SSR pass data from server to client

See original GitHub issue

πŸš€ feature request

Relevant Package

This feature request is for @angular/universal

Description

Inspired by Next.js getInitialProps to improve SSR performance in Angular. getInitialProps allows us to easily pass data from the server side to the client side, and by doing so save a lot of the initial ajax requests we need to send in the client.

consider the following code:

constructor(private _http : HttpClient) {}
ngOnInit() {
    this._http.get('get some data and populate the template with that data')
}

if running on SSR the request will be sent both on the server side and on the client side (on initial load). The loading of the data would be much quicker if the request would only be sent on the server side and data will be transfered easily from the server side to the client side.

Next.js solved it by adding a static method that will be run on the server on the initial load and if not in the initial load it will run on the client. So inspired by this API I suggest adding another lifecycle hook on the component that will run on the server only on initial load and will pass the data to the ngOnInit. It will look something like the following:

constructor(private _http : HttpClient) {}

/**
 * this will run only on the server on initial load
 * this will run on the client when not in initial load
 *  
 */
ngSnapshotInit() : Promise<any> | Observable<any> {
    return this._http.get('...');
}

/**
 * @param data  - the data im getting from the ngSnapshotInit
 */
ngOnInit(data) {
    this.displayOnTemplate = data;
}

Leveraging the power of server side to make our initial AJAX requests will not only make our app load quicker but also the user won’t see annoying jumps with the data grabbed twice with double the requests.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

0reactions
angular-automatic-lock-bot[bot]commented, Feb 27, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pass data from server side to client without http call
Ideally I should just use service on my server and pass the data to view and then server will render html to client...
Read more >
Server-Side Rendering (SSR) - Vue.js
The data used during render contains randomly generated values. Since the same application will run twice - once on the server, and once...
Read more >
Angular Universal: passing server data to client in ...
Angular Universal: passing server data to client in APP_INITIALIZER ; import {Inject, Injectable, PLATFORM_ID} from '@angular/core'; import ; '@Β ...
Read more >
Using React's Context to Pass Variables From the Server to ...
The problem I had to solve was how to pass variables (containing the data necessary for rendering the page) from the server to...
Read more >
Server Rendering - Redux - JS.ORG
This is important because, if we preload any data before generating the HTML, we want the client to also have access to this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found