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

AWSで始めるServerless React Server Side Rendering

AWSで始めるServerless React Server Side Rendering

グランフロント大阪2016 LT資料です。

Hidetaka Okamoto

August 28, 2016
Tweet

More Decks by Hidetaka Okamoto

Other Decks in Technology

Transcript

  1. A W S Ͱ ࢝ Ί Δ S e r

    v e r l e s s R e a c t S e r v e r S i d e R e n d e r i n g ά ϥ ϯ ϑ ϩ ϯ τ Τ ϯ υ େ ࡕ 2 0 1 6 LT
  2. H i d e t a k a O k

    a m o t o • Α͘Θ͔Βͳ͍΋ͷΛ࡞Δਓ • WP APIͰNo PHPαΠτ • JSͰಠࣗυϝΠϯߪೖ • PythonͰঝೝϝʔϧॲཧ
  3. Wo rd P re s s αΠ τ ΋ ϑϧ

    J a v a S c r i p t Ͱ ߏ ங Ͱ ͖ Δ
  4. Wo rd P re s s / P H P

    ֮͑ͳͯ͘ࡁΜͩ΍ͬͨʔʂ
  5. ϑ ϩ ϯ τ Τ ϯ υ ͷ ͜ ͱ

    ͩ ͚ ߟ ͑ ͨ ͍
  6. ϑ ϩ ϯ τ ࡞ Δ ͷ ʹ ɺ α

    ʔό ͷ ͜ ͱ ߟ ͑ ͨ ͘ ͳ ͍
  7. α ʔόʔ ͷ ϝ ϯ ς ͕ ख ؒ ͳ

    Β ɺ ͦ ΋ ͦ ΋ ϝ ϯ ς ͠ ͳ ͚ Ε ͹ ͍ ͍
  8. A M A Z O N L A M B

    D A • Node.js / Python / Java͕࢖͑Δ࣮ߦ؀ڥ • εέʔϧ΍ϝϯςφϯε͸AWSଆͰࣗಈతʹ • ϓϩηεΛಈ͔͚ͨ࣌ͩ͠՝ۚ
  9. ࣮ ߦ ͢ Δ ί ʔ υ Ҏ ֎ ϝ

    ϯ ς ͠ ͳ ͘ ͯ ͍ ͍ ʂ
  10. A M A Z O N L A M B

    D A • Node.js / Python / Java͕࢖͑Δ࣮ߦ؀ڥ • εέʔϧ΍ϝϯςφϯε͸AWSଆͰࣗಈతʹ • ϓϩηεΛಈ͔͚ͨ࣌ͩ͠՝ۚ
  11. A W S L a m b d a ʹ

    R e a c t Λ ͭ ͬ ͜ Μͯ Έ ͨ
  12. ख ॱ h t t p : / / b

    i t . l y / 2 b Q r F 4 t h t t p : / / b i t . l y / 2 b q Z X r 0
  13. import λ from 'apex.js';import 'babel-polyfill'; import React, { Component }

    from 'react'; import { renderToString } from 'react-dom/server'; ϥ Π ϒ ϥ Ϧ Λ Π ϯ ϙʔ τ
  14. class App extends Component { render() { return ( <div>

    <h1>hello React</h1> <p>This is a Example page </p> </div> ); }; } R e a c t Λ ॻ ͘
  15. function renderFullPage(renderedContent) { return `<!DOCTYPE html><html><head></head> <body> <div id=“container">${renderedContent}</div> </body></html>`;

    } export default λ( (e, ctx) => { const renderedContent = renderToString( <App /> ); const renderedPage = renderFullPage( renderedContent ); return (renderedPage); }); S S R ͯ͠ re t u r n ͢ Δ
  16. L a m b d a Ͱ Ϩϯ μ Ϧ

    ϯ ά ͠ ͨ R e a c t ʹ ΞΫ η ε ͢ Δ
  17. A M A Z O N A P I G

    AT E WAY • AWS͕Ϛωʔδυ͢ΔAPIαʔόʔ • εέʔϧ΍ηΩϡϦςΟରԠ͸͓೚ͤͰ͖Δ • ϦΫΤετ਺෼͚ͩͷ՝ۚ
  18. C o n t e n t - Ty p

    e : “ t e x t / h t m l ” Λ ฦ ͢ A P I Λ ࡞ Δ
  19. ৄ ࡉ h t t p : / / b

    i t . l y / 2 b q Z X r 0
  20. A P I G a t e w a y

    ͷ U R L ʹ ΞΫ η ε ͯ͠ Έ Δ ͱ
  21. R e a c t ͷ Ϩϯ μ Ϧ ϯ

    ά ݁ Ռ ͕ H T M L ͱ ͯ͠ ද ࣔ ͞ Ε Δ
  22. R e a c t ͷ Ϩϯ μ Ϧ ϯ

    ά ݁ Ռ ͕ H T M L ͱ ͯ͠ ද ࣔ ͞ Ε Δ
  23. export default λ( (e, ctx ) => { return fetch(

    ‘http://api.wp-app.org/wp-json/' ) .then( res => { if ( res.status >= 400 ) { throw new Error('Bad request'); } return res.json(); }).then( data => { const renderedContent = renderToString( <App data={data}/>); const renderedPage = renderFullPage( renderedContent ); return renderedPage; }); }); f e t c h ͠ ͨ Β ֎ ෦ A P I ͱ ΋ ͭ ͳ ͛ Ε Δ
  24. α ʔόʔ Ϩε Ͱɺ ϑ ϩ ϯ τ Τ ϯ

    υ ͷ ͜ ͱ ͩ ͚ ߟ ͑ ͞ ͤΖ ʂ
  25. α ʔόʔ Ϩε Ͱɺ ϑ ϩ ϯ τ Τ ϯ

    υ ͷ ͜ ͱ ͩ ͚ ߟ ͑ ͞ ͤΖ ʂ ੍࡞ɾஶ࡞ Ί૊ ऴ