FrozenPandaz / ng-universal-demo

171 stars 54 forks source link

What is the purpose of the TransferHttpModule #20

Closed jbeckton closed 7 years ago

jbeckton commented 7 years ago

What is the purpose of the TransferHttpModule

FrozenPandaz commented 7 years ago

its a temporary drop in replacement for @angular/http which will store your api data retrieved by the server so that the client does not have to rerequest them

jbeckton commented 7 years ago

oh thats slick! Does it make sense to use that api for other things in my code as well or is it just needed for the transferring of state?

crebuh commented 7 years ago

@FrozenPandaz

Is there a guide on how to use it?

peterpeterparker commented 7 years ago

@crebuh basically on the server side you do your query and save the results in transfer-state and on the client side you query the transfers-state

in a provider something like:

getSomething(): Observable<Object> {
     if (isPlatformServer(this.platformId)) {
         return Observable.fromPromise(this.doGet());
    } else {
        return this.getFromTransferState();
   }

private doGet(itemHashId: string): Promise<{}> {
      return new Promise((resolve, reject) => {
            const params: HttpParams = new HttpParams();

           this.httpClient.get('http://...', {params: params}).subscribe((result: any) => {
                 this.transferState.set('my-key', result);
                 resolve(result);
           }, (error) => {
               reject(error);
          });
       });
 }

 private getFromTransferState(): Observable<Object> {
    return new Observable(observer => {
        observer.next(this.transferState.get('my-key'));
    });
  }

don't know if it's the right way or a good way but that seems to work for me

ysus commented 7 years ago

angular universal will two http call, one in the server when you realize the request to generate the html render to SEO, and other call in the client when the code js is load in the browser. the purpose of transfer is to avoid the second call and transfert the info of the http call to the client. or wherever info you need to transfer to the client not only http calls

sorry for my bad enghlish.

peterpeterparker commented 7 years ago

@ysus that's good english 👍

If I may, I would just suggest to modify angular universal need to with angular universal will ... it isn't a must, that's the purpose of transfer-state ;)

crebuh commented 7 years ago

@peterpeterparker

thx I got this, maybe my question wasn't clear. the module offers already some methods like get, put, post.

so this was confusing me, if these are just examples inside the class. but it seems you are just working with with transferState.get and transferState.set

peterpeterparker commented 7 years ago

@crebuh yes so far, simple case at least, I'm only working with TransferState