Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SSR with Angular2

SSR with Angular2

ng-sake用

Yosuke Kurami

March 30, 2016
Tweet

More Decks by Yosuke Kurami

Other Decks in Programming

Transcript

  1. About me • Yosuke KURAMI (@Quramy) • Frontend Web Developer

    at WACUL INC. • ೔ʑͷ࢓ࣄ͸AngularJS(1.x)ͰSPA։ൃ • झຯͰTypeScript༻ͷVimϓϥΪϯ։ൃͯ͠·͢ • http://vimawesome.com/plugin/tsuquyomi 

  2. Agenda • Angular 2ͱServer Side Rendering(SSR) • Angular UniversalͰԿ͕ग़དྷΔͷ͔ •

    Angular Universal͸ͲͷΑ͏ʹಈ͘ͷ͔ • σϞͬΆ͍ͳʹ͔
  3. Why SSR? • Ϣʔβʔ଴ͪ࣌ؒͷ୹ॖ • Inital ViewΛαʔόͰग़ྗ → αΫαΫಈ͘!(Α͏ʹݟͤΕΔ) •

    SEO, Preview Link • SSRͳΒ࣮֬ʹΫϩʔϥʔ͕ίϯςϯπΛೝࣝͰ͖Δ • Browser Support • εΫϦʔϯϦʔμ౳ɺΞΫηγϏϦςΟͷରԠ͕͠΍͍͢
  4. What’s Goal of SSR? 1. ϓϥοτϑΥʔϜʹґଘͤͣʹಈ࡞͢Δ͜ͱ 2. SSRͷඳը݁ՌͱSPA͕γʔϜϨεʹ౷߹͢Δ͜ͱ • ʮInital

    Viewͷ௚ޙʹϢʔβΠϕϯτ͕ൃੜͨ͠Βʁʯˠ Preboot 3. ੑೳ໨ඪΛୡ੒Ͱ͖͍ͯΔ͜ͱ • Angular։ൃνʔϜͷ໨ඪ͸ʮ1secͰinital viewͷඳըΛ ׬ྃͰ͖ΔΑ͏͢Δʯͩͱ͔ɻ
  5. Rendering Architecture of Angular • AngularJS 1.x • jQuery(jqLite)ʹڧ͘ґଘ •

    DOMΤϯδϯ͕ແ͍ͱUIΛඳըͰ͖ͳ͍ • Angular 2 • UIͷඳըػߏ(Renderer, DOM)ΛAdapter PatternͰந৅Խ • ద੾ͳAdapter͕͋Ε͹ඇϒϥ΢β؀ڥͰ΋UIͷඳը͕Մೳ
  6. Renderer Stack in Angular2 Application (Components, Directives, etc…) Parse5DomAdapter DomRenderer

    Renderer (createElement, lisetenEvent, etc…) BrowserDomAdapter document parse5 Web Browser Node.js NodeDomRenderer DomAdapter (abstract DOM API, not managed by DI) extend implement use use implement implement use use Platform Layer Abstract UI Layer Runtime Layer
  7. I want to try right now • angular/universal-starter ͕Φεεϝ •

    Expressͱϒϥ΢βͷ྆ํͰRendering • masterͷλΠϛϯάʹΑͬͯ͸ಈ͔ͳ͍͜ ͱ΋…orz • quramy/universal-starter ʹfork
  8. SSR with Express renderͰBootstrapίʔυΛ౉͚ͩ͢ɻ͔ΜͨΜʂ let app = express(); app.engine('.html', expressEngine).set('views',

    __dirname).set('view engine', 'html'); function ngApp(req, res) { let baseUrl = '/'; let url = req.originalUrl || '/'; res.render('index', { directives: [ App, HtmlHead, ServerOnlyApp], providers: [ provide(APP_BASE_HREF, {useValue: baseUrl}), provide(REQUEST_URL, {useValue: url}), ROUTER_PROVIDERS, NODE_LOCATION_PROVIDERS, ] }); } app.use('/', ngApp); app.use('/about', ngApp); app.use('/home', ngApp);
  9. Seamless State Transfer with Preboot 1. Web Server ʹHTTPΞΫηε 2.

    Server SideͰॳظϖʔδͷHTMLΛੜ੒ͯ͠ฦ٫ 3. ϒϥ΢βͰॳظϖʔδΛඳը(͜ͷ࣌఺Ͱ͸୞ͷ੩తHTML) 4. νΣοΫϘοΫεૢ࡞౳ͷϢʔβΠϕϯτͷิ଍։࢝(preboot) 5. ClientΞϓϦέʔγϣϯͷىಈ(bootstrap) 6. 4.~5.ͷؒʹัଊͨ͠ϢʔβΠϕϯτΛplayback 7. ΞϓϦىಈલͷΠϕϯτ͕ॲཧ͞ΕΔ