2017-06-06 106 views
0

我想看看我是否可以創建基於CDN和角度2通用的堆棧。因此,當用戶導航有CDN以獲取資產時,並且如果用戶第一次訪問將具有Universal提供的完整HTML。Angular 2 Universal + Akamai

我在想:

客戶< ===> Akamai的< ===>光油< ===>源服務器(與通用的node.js)

這聽起來好?你有沒有嘗試過? 另外我正在考慮爲整個堆棧添加nginx和ELB。

現在的問題是: - 這個堆棧可以按預期工作嗎?

回答

0

是的,它可以做到!最大的問題是如何使Angular中任意數量的http請求無效以確定呈現的頁面。使用某種頭部模式來失效可能會有所幫助。

假設你正在使用的官方NG-Express引擎,這樣的服務可以讓你定義從角運行時的反應:

import { RESPONSE } from '@nguniversal/express-engine/tokens' 
import { Inject, Injectable, Optional } from '@angular/core' 
import { Response } from 'express' 

export interface IServerResponseService { 
    getHeader(key: string): string 
    setHeader(key: string, value: string): this 
    setHeaders(dictionary: { [key: string]: string }): this 
    appendHeader(key: string, value: string, delimiter?: string): this 
    setStatus(code: number, message?: string): this 
    setNotFound(message?: string): this 
    setError(message?: string): this 
} 

@Injectable() 
export class ServerResponseService implements IServerResponseService { 

    private response: Response 

    constructor(@Optional() @Inject(RESPONSE) res: any) { 
    this.response = res 
    } 

    getHeader(key: string): string { 
    return this.response.getHeader(key) 
    } 

    setHeader(key: string, value: string): this { 
    if (this.response) 
     this.response.header(key, value) 
    return this 
    } 

    appendHeader(key: string, value: string, delimiter = ','): this { 
    if (this.response) { 
     const current = this.getHeader(key) 
     if (!current) return this.setHeader(key, value) 

     const newValue = [...current.split(delimiter), value] 
     .filter((el, i, a) => i === a.indexOf(el)) 
     .join(delimiter) 

     this.response.header(key, newValue) 
    } 
    return this 
    } 

    setHeaders(dictionary: { [key: string]: string }): this { 
    if (this.response) 
     Object.keys(dictionary).forEach(key => this.setHeader(key, dictionary[key])) 
    return this 
    } 

    setStatus(code: number, message?: string): this { 
    if (this.response) { 
     this.response.statusCode = code 
     if (message) 
     this.response.statusMessage = message 
    } 
    return this 
    } 

    setNotFound(message = 'not found'): this { 
    if (this.response) { 
     this.response.statusCode = 404 
     this.response.statusMessage = message 
    } 
    return this 
    } 

    setError(message = 'internal server error'): this { 
    if (this.response) { 
     this.response.statusCode = 500 
     this.response.statusMessage = message 
    } 
    return this 
    } 
} 
+0

感謝您的回答!你的意思是使akamai緩存無效,對吧? –

+0

正確。在Node服務器上進行的http請求必須以某種方式捆綁在一起以通知緩存使用了哪些數據來生成呈現的頁面。 –