Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Angular 5.0): update engine-etc for angular 5.0 & Domino #437

Merged
merged 5 commits into from
Nov 2, 2017

Conversation

MarkPieszak
Copy link
Member

@MarkPieszak MarkPieszak commented Oct 10, 2017

Fully-Functioning version updated to work with Angular 5.0-rc1.
Waiting until we merge updates to Aspnet-engine (in universal): angular/universal#825

Before officially merging into Master branch.

WIP - More updates to come

Closes #434
Closes #435
Closes #430
Closes #424

🎁 🍾

@isaacrlevin
Copy link
Contributor

@MarkPieszak since this is working (great job by the way). Do we want to clean up that mast branch to be lean with this PR? I can wait till after to create one as well

@naveedahmed1
Copy link

A clean up is much needed!

@MarkPieszak
Copy link
Member Author

Yes definitely 👍👍 If you want to take the branch for a spin and clean anything up just make commits right to this branch and we'll merge them all into master.
Then it'll be easier to do the "how-to's" that add more functionality (maybe in PRs we intentionally never merge (?))

@naveedahmed1
Copy link

naveedahmed1 commented Oct 12, 2017

@MarkPieszak can't we just replace the existing TransferHttp module with a simple interceptor like this? This way it seems more aligned with the theme of new HttpClientModule.

import { Injectable, Inject, PLATFORM_ID } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse, HttpErrorResponse, HttpEventType } from '@angular/common/http';

import { Observable } from 'rxjs/Observable';

import { TransferState } from './../modules/transfer-state/transfer-state';
import { isPlatformServer } from '@angular/common';

import { ORIGIN_URL } from './shared/constants/baseurl.constants';

@Injectable()
export class CustomHttpInterceptor implements HttpInterceptor {

    private isServer = isPlatformServer(this.platformId);

    constructor(
        @Inject(PLATFORM_ID) private platformId,
        protected transferState: TransferState,
        @Inject(ORIGIN_URL) private baseUrl: string) { }

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        let modifiedRequest: any;
        let url: string;

        let isLocalRequest: boolean = !req.url.startsWith('http');
        if (isLocalRequest) {

            url = (isLocalRequest ? this.baseUrl + req.url : req.url);

            // Skip cache check if the request method isn't GET.
            if (req.method == 'GET') {
                const cachedResponse = this.getFromCache(url);

                if (cachedResponse) {
                    // A cached response exists. Serve it instead of forwarding
                    // the request to the next handler.

                    let modifiedResponse = new HttpResponse<any>({ headers: cachedResponse.headers, body: cachedResponse.body, status: cachedResponse.status, statusText: cachedResponse.statusText, url: cachedResponse.url });
                    return Observable.of(modifiedResponse);
                }
            }

            // Clone the request to add the new header.
            modifiedRequest = req.clone({ headers: req.headers, url: url, body: req.body });
        }

        return next.handle(isLocalRequest ? modifiedRequest : req)
            .do((event: HttpEvent<any>) => {
                if (event instanceof HttpResponse) {
                    if (this.isServer) {
                        this.setCache(url, event);
                    }
                }
            });
    }

    private setCache(key, data) {
        return this.transferState.set(key, data);
    }

    private getFromCache(key): HttpResponse<any> | null {
        return this.transferState.get(key);
    }
}

And in our AppModule we just need to add CustomHttpInterceptor as provider.

@MarkPieszak
Copy link
Member Author

We'll definitely be getting rid of the need to have TransferHttp locally, as we basically moved a similar one into Angular Core itself in 5.0. :)

As for an interceptor to auto-append the current base_url, we definitely might as well add it!

@isaacrlevin
Copy link
Contributor

isaacrlevin commented Oct 20, 2017

@MarkPieszak this is probably safe to merge since your PR in Universal is merged as well. Only thing left pending is the transferhttp stuff right?

Looking at this branch, I think all the features that are there are valuable, except maybe bootstrap. Maybe we just take Bootstrap and Translate out and leave Http, LazyLoading and Basic Component

Thoughts? If that sounds good, I can take care of that real quick

@naveedahmed1
Copy link

Below is the updated interceptor, which should work with Angular 5.

import { Injectable, Inject, PLATFORM_ID } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse, HttpErrorResponse, HttpEventType } from '@angular/common/http';

import { Observable } from 'rxjs/Observable';

import { TransferState } from '@angular/platform-browser';
import { isPlatformServer } from '@angular/common';

import { ORIGIN_URL } from './shared/constants/baseurl.constants';

@Injectable()
export class CustomHttpInterceptor implements HttpInterceptor {

    private isServer = isPlatformServer(this.platformId);

    constructor(
        @Inject(PLATFORM_ID) private platformId,
        protected transferState: TransferState,
        @Inject(ORIGIN_URL) private baseUrl: string) { }

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        let modifiedRequest: any;
        let url: string;

        let isLocalRequest: boolean = !req.url.startsWith('http');
        if (isLocalRequest) {

            url = this.baseUrl + req.url;

            // Skip cache check if the request method isn't GET.
            if (req.method == 'GET') {
                const cachedResponse = this.getFromCache(url);

                if (cachedResponse) {
                    // A cached response exists. Serve it instead of forwarding
                    // the request to the next handler.

                    let modifiedResponse = new HttpResponse<any>({ headers: cachedResponse.headers, body: cachedResponse.body, status: cachedResponse.status, statusText: cachedResponse.statusText, url: cachedResponse.url });
                    return Observable.of(modifiedResponse);
                }
            }

            // Clone the request to add the new header.
            modifiedRequest = req.clone({ headers: req.headers, url: url, body: req.body });
        }

        return next.handle(isLocalRequest ? modifiedRequest : req)
            .do((event: HttpEvent<any>) => {
                if (event instanceof HttpResponse) {
                    if (this.isServer) {
                        this.setCache(url, event);
                    }
                }
            });
    }

    private setCache(key, data) {
        return this.transferState.set(key, data);
    }

    private getFromCache(key): HttpResponse<any> | null {
        return this.transferState.get(key, null as any);
    }
}

@MarkPieszak
Copy link
Member Author

Sometimes yes, but in this case since it's an InjectionToken from completely outside of itself, we need to do get it via the Injector, otherwise you get the quite vague error:

ERROR in Error encountered resolving symbol values statically. Only initialized variables and constants can be referenced because the value of this variable is needed by the template compiler.

@MarkPieszak
Copy link
Member Author

@naveedahmed1 Good news, soon there will also be built in support for automatically adding relative path in Core :)
angular/angular#19224

Since we essentially do need them to make sure transfer stuff works.

@naveedahmed1
Copy link

That would be awesome! Meanwhile I am not able to successfully implement ServerTransferStateModule from Angular 5, can you please also take a look at #448

@MarkPieszak
Copy link
Member Author

I didn't have much time today, but ServerTransferStateModule seems to break server-renders for the time being, I'm going to merge this into Master, and hopefully one of us can figure out what's causing that to break, and we can eventually get that in there as well! :)

@MarkPieszak MarkPieszak merged commit 901300a into master Nov 2, 2017
@MarkPieszak MarkPieszak deleted the angular-5.0-updates branch November 2, 2017 14:17
@naveedahmed1
Copy link

@MarkPieszak can TransferHttpCacheModule ( https://github.com/angular/universal/tree/master/modules/common ) be used in this case?

It says:

This is the common Angular Universal module that is common across server-side rendering app irrespective of the rendering engine.

I tried adding it, though it doesn't break the app. but the requests are still rendered twice once on server and once on client.

BillSheldon-HunterIndustries pushed a commit to BillSheldon-HunterIndustries/aspnetcore-angular2-universal that referenced this pull request Jan 17, 2018
…IO#437)

* feat(5.0): update engine-etc for angular 5.0-rc1

WIP - More updates to come

* remove ng 4 references

* update source maps for faster HMR builds

* use aspnetcore-engine & misc updates and fixes

* update to 5.0 official

Closes TrilonIO#434
Closes TrilonIO#435
Closes TrilonIO#430
Closes TrilonIO#424
BillSheldon-HunterIndustries pushed a commit to BillSheldon-HunterIndustries/aspnetcore-angular2-universal that referenced this pull request May 15, 2018
…IO#437)

* feat(5.0): update engine-etc for angular 5.0-rc1

WIP - More updates to come

* remove ng 4 references

* update source maps for faster HMR builds

* use aspnetcore-engine & misc updates and fixes

* update to 5.0 official

Closes TrilonIO#434
Closes TrilonIO#435
Closes TrilonIO#430
Closes TrilonIO#424
BillSheldon-HunterIndustries pushed a commit to BillSheldon-HunterIndustries/aspnetcore-angular2-universal that referenced this pull request May 15, 2018
…IO#437)

* feat(5.0): update engine-etc for angular 5.0-rc1

WIP - More updates to come

* remove ng 4 references

* update source maps for faster HMR builds

* use aspnetcore-engine & misc updates and fixes

* update to 5.0 official

Closes TrilonIO#434
Closes TrilonIO#435
Closes TrilonIO#430
Closes TrilonIO#424
BillSheldon-HunterIndustries pushed a commit to Hunter-Industries/aspnetcore-angular2-universal that referenced this pull request May 16, 2018
…IO#437)

* feat(5.0): update engine-etc for angular 5.0-rc1

WIP - More updates to come

* remove ng 4 references

* update source maps for faster HMR builds

* use aspnetcore-engine & misc updates and fixes

* update to 5.0 official

Closes TrilonIO#434
Closes TrilonIO#435
Closes TrilonIO#430
Closes TrilonIO#424
BillSheldon-HunterIndustries pushed a commit to BillSheldon-HunterIndustries/aspnetcore-angular2-universal that referenced this pull request Jun 8, 2018
…IO#437)

* feat(5.0): update engine-etc for angular 5.0-rc1

WIP - More updates to come

* remove ng 4 references

* update source maps for faster HMR builds

* use aspnetcore-engine & misc updates and fixes

* update to 5.0 official

Closes TrilonIO#434
Closes TrilonIO#435
Closes TrilonIO#430
Closes TrilonIO#424
BillSheldon-HunterIndustries pushed a commit to BillSheldon-HunterIndustries/aspnetcore-angular2-universal that referenced this pull request Jun 8, 2018
…IO#437)

* feat(5.0): update engine-etc for angular 5.0-rc1

WIP - More updates to come

* remove ng 4 references

* update source maps for faster HMR builds

* use aspnetcore-engine & misc updates and fixes

* update to 5.0 official

Closes TrilonIO#434
Closes TrilonIO#435
Closes TrilonIO#430
Closes TrilonIO#424
BillSheldon-HunterIndustries pushed a commit to BillSheldon-HunterIndustries/aspnetcore-angular2-universal that referenced this pull request Jun 8, 2018
…IO#437)

* feat(5.0): update engine-etc for angular 5.0-rc1

WIP - More updates to come

* remove ng 4 references

* update source maps for faster HMR builds

* use aspnetcore-engine & misc updates and fixes

* update to 5.0 official

Closes TrilonIO#434
Closes TrilonIO#435
Closes TrilonIO#430
Closes TrilonIO#424
BillSheldon-HunterIndustries pushed a commit to BillSheldon-HunterIndustries/aspnetcore-angular2-universal that referenced this pull request Jun 8, 2018
…IO#437)

* feat(5.0): update engine-etc for angular 5.0-rc1

WIP - More updates to come

* remove ng 4 references

* update source maps for faster HMR builds

* use aspnetcore-engine & misc updates and fixes

* update to 5.0 official

Closes TrilonIO#434
Closes TrilonIO#435
Closes TrilonIO#430
Closes TrilonIO#424
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants