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

You Need to know SSR

You Need to know SSR

ng-japan で発表した Server Side Rendering の資料です。

Yosuke Furukawa

June 17, 2017
Tweet

More Decks by Yosuke Furukawa

Other Decks in Programming

Transcript

  1. You need to know SSR
    2017/06/17
    ng-japan @ Shinagawa MS Office

    View full-size slide

  2. Twitter: @yosuke_furukawa
    Github: yosuke-furukawa

    View full-size slide

  3. 8FC%#13&44݄߸

    View full-size slide

  4. ͍ͭ΋͸3FBDUͷਓ͚ͩͲ
    "OHVMBSʹࠢചΓ·ͨ͠ʂʂ

    View full-size slide

  5. Server Side Rendering

    View full-size slide

  6. Server Side Rendering
    4FSWFS
    1BHF3FRVFTU
    $PNQPOFOUT
    5FNQMBUFT
    'FUDI%BUB
    %#"1*
    3FOEFS)5.- (FUQBHFQBSUT

    View full-size slide

  7. Server Side Rendering
    4FSWFS
    1BHF3FRVFTU
    $PNQPOFOUT
    5FNQMBUFT
    'FUDI%BUB
    %#"1*
    3FOEFS)5.- (FUQBHFQBSUT
    3FOEFSFS
    $PNQPOFOUT
    5FNQMBUFT
    3FOEFS)5.-XJUITBNF
    DMJFOUTFSWFSSFOEFSMPHJD

    View full-size slide

  8. Why we need Server Side
    Rendering?

    View full-size slide

  9. SEO
    Ϋϩʔϥ͕+4Λ࣮ߦͰ͖Δͱ͸
    ͍͑ɺͦͷ+4͕ͪΌΜͱಈ͘อ
    ূ͸ͳ͍ɻ

    View full-size slide

  10. SEO͸ॏཁɺͰ΋ຊ୊͸࣍

    View full-size slide

  11. First View Performance

    View full-size slide

  12. ͦ΋ͦ΋First ViewͱҰޱʹ
    ݴͬͯ΋छྨ͕͋Δ
    /BWJHBUJPO4UBSU 'JSTU1BJOU 'JSTU$POUFOUGVM
    1BJOU
    Loading..
    'JSTU.FBOJOHGVM
    1BJOU
    'JSTU.FBOJOHGVM
    1BJOU
    7JTVBMMZSFBEZ 5JNF5P
    *OUFSBDUJWF
    +4MPBEFE

    'VMMZ-PBEFE
    BMMSFTPVSDFTBSF
    MPBEFE

    View full-size slide

  13. First View Performance
    • SSR ʹ͓͚Δ First View ͷվળͱ͸ɺ͜ͷ
    First Meaningful Paint ·ͰΛࢦ͢ɻ
    /BWJHBUJPO4UBSU 'JSTU1BJOU
    'JSTU$POUFOUGVM
    1BJOU
    Loading..
    'JSTU.FBOJOHGVM
    1BJOU

    View full-size slide

  14. First View Performance
    • First Meaningful PaintҎ߱ͷվળ͸Service
    Worker΍HTTP2ͳͲผͳ΍Γํ͕͋Δɻ
    'JSTU.FBOJOHGVM
    1BJOU
    7JTVBMMZSFBEZ 5JNF5P
    *OUFSBDUJWF
    +4MPBEFE

    'VMMZ-PBEFE
    BMMSFTPVSDFTBSF
    MPBEFE

    View full-size slide

  15. Client Side Rendering
    ͷΈͰͷಈ͖

    View full-size slide

  16. Client Side Rendering ͷΈͷ
    ৔߹
    • First Meaningful Paint ͱ Time To Interact ͷ
    λΠϛϯά͕΄ͱΜͲಉ͡ɻ
    'JSTU.FBOJOHGVM1BJOU
    5JNF5P*OUFSBDUJWF
    +4MPBEFE

    'JSTU$POUFOUGVM
    1BJOU
    Loading..

    View full-size slide

  17. Client Side Rendering ͷΈͷ
    ৔߹
    • ݴ͍׵͑ΔͱɺFirst Contentful Paint͔ΒFirst
    Meaningful Paint ·Ͱ͕௕͘ͳΔɻ
    'JSTU.FBOJOHGVM1BJOU
    5JNF5P*OUFSBDUJWF
    +4MPBEFE

    'JSTU$POUFOUGVM
    1BJOU
    Loading..

    View full-size slide

  18. Client Side Rendering ͷΈͷ
    ৔߹
    • ݴ͍׵͑ΔͱɺFirst Contentful Paint͔ΒFirst
    Meaningful Paint ·Ͱ͕௕͘ͳΔɻ
    'JSTU.FBOJOHGVM1BJOU
    5JNF5P*OUFSBDUJWF
    +4MPBEFE

    'JSTU$POUFOUGVM
    1BJOU
    Loading..
    Ϣʔβʔ͸ݟ͔͑ͯΒૢ࡞͢Δ 'JSTU.FBOJOHGVM1BJOU͕ग़͔ͯΒ*OUFSBDU
    ͢Δ
    ͷͰɺ͜ͷ$MJFOU4JEF3FOEFSJOH͚ͩͰ͸࣌ؒతʹແବ͕ଟ͍ɻ

    View full-size slide

  19. ͦ͜Ͱ
    Server Side Rendering

    View full-size slide

  20. /BWJHBUJPO4UBSU 'JSTU1BJOU 'JSTU$POUFOUGVM
    1BJOU
    Loading..
    'JSTU.FBOJOHGVM
    1BJOU
    ࠷ॳ͔ΒσʔλΛຒΊࠐΜͩঢ়ଶͰ)5.-ʹ͢Δ
    ʢ4FSWFS4JEF3FOEFSJOHʣ

    View full-size slide

  21. ͡Ό͋ SSR ʹ͢Ε͹͍͍͔ͱ
    ͍͏ͱ࣮͸ͦ͏୯७Ͱ΋ͳ͍

    View full-size slide

  22. Server Side Rendering
    Critical Points
    • HTMLੜ੒͕CPUΠϯςϯγϒλεΫʹͳΔɻ
    • ·ͨNode.jsͰ࣮ߦ͢ΔͱϝΠϯϧʔϓΛϒϩοΫ͢Δ(গͳ͘ͱ΋ݱ࣌
    ఺ͷReactͰ͸ɺAngular΋ಉ༷)ɻ
    • ΋͔ͨ͠͠ΒϒϩοΩϯά͸࣍ͷReactͷΞοϓσʔτͰͳΜͱ͔ͳ
    Δ͔΋(https://github.com/facebook/react/pull/9710)ʁ
    • CPUΠϯςϯγϒλεΫʹͳΓɺCPUෛՙ͕ߴ·Δͱαʔό͕ॲཧ͖͠
    Εͳ͘ͳΔʢෛՙܰݮ͢ΔͨΊʹαʔό୆਺૿΍͞ͳ͍ͱ͍͚ͳ͘ͳΔʣ

    View full-size slide

  23. Server Side Rendering
    Critical Points
    https://speakerdeck.com/yoshidan/nodefest2016

    View full-size slide

  24. Server Side Rendering
    Critical Points

    View full-size slide

  25. Server Side Rendering
    Critical Points
    Componentࢦ޲Ͱ͖ͪΜͱComponentΛ෼͚ͯ
    ࣮૷͢Ε͹͢Δ΄ͲHTMLੜ੒ίετ͕ߴ͘ͳΔ

    View full-size slide

  26. Server Side Rendering
    Critical Points
    • React ͷ৔߹ɿ
    • renderToString ͷ࣌ʹҰͭҰͭͷComponentͷΠϯελϯεΛੜ੒͠ɺreact-
    idΛ߱Γɺ࠷ޙʹHTMLͷνΣοΫαϜΛܭࢉͤ͞Δඞཁ͕͋Δ(Clientͷ
    Virtual DOM ͱͷ੔߹ੑΛ߹ΘͤΔͨΊ)ɻ
    • Angular ͷ৔߹ɿ
    • VDOMͰ͸ͳ͍ͷͰɺidΛৼΔඞཁ͸ͳ͍͕ɺrenderModuleFactory ͷ࣌ʹຖ
    ճHTMLύʔεॲཧͯ͠ΔʢϓϦίϯύΠϧ͢Ε͹ίετݮΔʣ
    • rxjs ͷ subscribeToResult ͕ॏͦ͏ (Routerͷॲཧ?)

    View full-size slide

  27. Server Side Rendering
    Critical Points
    • React ͷ৔߹ɿ
    • renderToString ͷ࣌ʹҰͭҰͭͷComponentͷΠϯελϯεΛੜ੒͠ɺ
    react-idΛ߱Γɺ࠷ޙʹHTMLͷνΣοΫαϜΛܭࢉͤ͞Δඞཁ͕͋Δ
    (ClientͷVirtual DOM ͱͷ੔߹ੑΛ߹ΘͤΔͨΊ)ɻ
    • Angular ͷ৔߹ɿ
    • VDOMͰ͸ͳ͍ͷͰɺidΛৼΔඞཁ͸ͳ͍͕ɺrenderModuleFactory ͷ
    ࣌ʹຖճHTMLύʔεॲཧͯ͠Δʢಉ͘͡Componentࢦ޲Ͱ෼ׂ͞ΕΕ͹
    ͞ΕΔ΄ͲɺCPUෛՙ͕ߴͦ͏ʣ
    ͭ·ΓɺͲͬͪ΋ಉ͡Α͏ʹCPUෛՙ͕͔͔Δɻ

    View full-size slide

  28. ݱ࣮Ͱͷઓུ͸2ͭ
    • HTMLੜ੒݁ՌΛΩϟογϡͤ͞Δ
    • above the fold (εΫϩʔϧ͠ͳ͍ͰݟΕΔൣ
    ғ)͚ͩHTMLΛϨϯμϦϯάͯ͠ฦ͢

    View full-size slide

  29. ݱ࣮Ͱͷઓུ͸2ͭ
    • HTMLੜ੒݁ՌΛΩϟογϡͤ͞Δ
    • above the fold (εΫϩʔϧ͠ͳ͍ͰݟΕΔൣ
    ғ)͚ͩHTMLΛϨϯμϦϯάͯ͠ฦ͢
    ͨͩ͜͠Ε͸྆ํڞɺॾਕͷ݋

    View full-size slide

  30. Ωϟογϡ
    • ੜ੒ࡁΈͷHTMLΛ࡞ͬͨޙͰΩϟογϡ͢Δ
    • CPUίετͷߴ͍ॲཧ͸͍͍ͤͥ࠷ॳͷҰ౓
    ͚ͩ
    3FEJT.FNEJO
    NFNPSZ$BDIF

    View full-size slide

  31. Ωϟογϡ໰୊
    • Ωϟογϡͨ͠৘ใΛ͍ͭpurge͢Δͷ͔໰୊
    ʢߋ৽ස౓ߴ͍΋ͷ͸Ωϟογϡແҙຯʣ
    • Redis/Memcached/LRU CacheͳͲͲ͜ʹอ
    ଘ͢Δͷ͔໰୊
    • ͦ΋ͦ΋ಈతͳίϯςϯπ͸ΩϟογϡͰ͖
    ͳ͍໰୊

    View full-size slide

  32. Ωϟογϡ໰୊
    • Ωϟογϡͨ͠৘ใΛ͍ͭpurge͢Δͷ͔໰୊
    ʢߋ৽ස౓ߴ͍΋ͷ͸Ωϟογϡແҙຯʣ
    • Redis/Memcached/LRU CacheͳͲͲ͜ʹอ
    ଘ͢Δͷ͔໰୊
    • ͦ΋ͦ΋ಈతͳίϯςϯπ͸ΩϟογϡͰ͖
    ͳ͍໰୊
    ͜ͷล͸ۜͷ஄ؙͳ͍ɺαʔϏεʹΑͬͯಛੑ͕ҧ
    ͏ͷͰͦΕͧΕαʔϏε͝ͱʹؤுΔඞཁ͋Γɻ

    View full-size slide

  33. above the fold rendering

    View full-size slide

  34. above the fold rendering
    • ݟ͑Δൣғ͚ͩαʔόαΠυϨϯμϦϯάͯ͠ɺ
    ݟ͑ͯͳ͍ൣғ͸ΫϥΠΞϯταΠυϨϯμ
    Ϧϯά͢Δɻ
    443
    $43

    View full-size slide

  35. above the fold rendering
    • ͜Ε΋ݟ͑ͯͳ͍෦෼͸࣮͸SEO͕ͪΌΜͱ
    ࣮ߦ͞ΕΔͱ͸ݶΒͳ͍ɻ
    • Time To Interact Ͱ͖Δ·Ͱ εΫϩʔϧ͢Δͱ
    Կ΋ͳ͍ঢ়ଶʹͳΔɻ

    View full-size slide

  36. above the fold rendering
    • ͜Ε΋ݟ͑ͯͳ͍෦෼͸࣮͸SEO͕ͪΌΜͱ
    ࣮ߦ͞ΕΔͱ͸ݶΒͳ͍ɻ
    • Time To Interact Ͱ͖Δ·Ͱ εΫϩʔϧ͢Δͱ
    Կ΋ͳ͍ঢ়ଶʹͳΔɻ
    ͜Ε΋αʔϏεಛੑͰڐ༰Ͱ͖Δ΋ͷͳͷ͔͸ݕ౼
    ͷ༨஍͋Γ

    View full-size slide

  37. Server Side Rendering͕Ͱ
    ͖Ε͹First Meaningful Paint
    Λߴ଎ԽͰ͖Δ͕ɺ࠷దԽ͢
    Δʹ͸໘౗΋ଟ͍

    View full-size slide

  38. ٯʹݴ͑͹ɺ"ͪΌΜͱ"
    ࣮ࢪ͢Δͱ
    First Meaningful Paint Λߴ
    ଎Խͭͭ͠ɺSPAͷྑ͍ॴͲ
    Γ͕࣮ݱͰ͖Δɻ

    View full-size slide

  39. Users Voice
    Ͳͷҙݟ΋ཁ໿͢ΔͱɺFirst Meaningful Paint ͸
    ࣺͯͯͦͷޙͷTime To Interact·ͰΛߴ଎Խ͢Ε͹
    OKͰ͠ΐʁͱ͍͏࿩(΋͘͠͸ະདྷͷWeb΁ͷظ଴)

    View full-size slide

  40. ͜ΕΒ΁ͷAnswer:
    First Meaningful Paint Λ
    ࣺͯͯྑ͍ͳΒSSR͸
    ΍Βͳͯ͘OK

    View full-size slide

  41. First Meaningful Paint Λ
    ߴ଎Խ͢Δ΂͖͔Ͳ͏͔͸
    αʔϏεಛੑʹґଘ͢Δ
    ʢ༧໿ࡁΈͷډञ԰ͱ͔ඒ༰
    Ӄ୳ͯ͠Δ࣌ʹදࣔ஗͍ͱக
    ໋తʣ

    View full-size slide

  42. ͋ͱݸਓతʹ͸ύϑΥʔϚϯ
    εͰ͸ଥڠͨ͘͠ͳ͍

    View full-size slide

  43. Performance͸ऴΘΓ͕ͳ͍
    ֤ॴͰৗʹվળͷඞཁ͕͋Δ

    View full-size slide

  44. ࠷ۙͷWebͷτϨϯυ͸
    First Meaningful Paint
    ͷߴ଎Խ͸΋ͪΖΜɺͦͩ͜
    ͚͡Όͳ͘ͳ͖͍ͬͯͯΔɻ

    View full-size slide

  45. 'JSTU.FBOJOHGVM
    1BJOU
    7JTVBMMZSFBEZ 5JNF5P
    *OUFSBDUJWF
    +4MPBEFE

    'VMMZ-PBEFE
    BMMSFTPVSDFTBSF
    MPBEFE

    'JSTU.FBOJOHGVM1BJOU͔Β5JNF5P*OUFSBDUJWF͕཭Ε͗͢Δͱ
    ࠓ౓͸ݟ͑ͯΔͷʹૢ࡞Ͱ͖ͳ͍͕࣌ؒ૿͑ͯετϨεʹܨ͕Δ
    https://medium.com/@addyosmani/progressive-web-apps-with-react-js-part-2-
    page-load-performance-33b932d97cf2

    View full-size slide

  46. 'JSTU.FBOJOHGVM
    1BJOU
    7JTVBMMZSFBEZ 5JNF5P
    *OUFSBDUJWF
    +4MPBEFE

    'VMMZ-PBEFE
    BMMSFTPVSDFTBSF
    MPBEFE

    ͳͷͰɺ࠷ۙ͸$PEF4QMJUUJOHͯ͠ͳΔ΂͘+4ࣗମΛܰ͘͢Δɺ)551ͷ
    164)Λ͏·͘࢖ͬͯ࣍ͷϦιʔεΛࣄલΩϟογϡ͓ͯ͘͠ͳͲͷ5JNF5P
    *OUFSBDU·ͰΛ೗Կʹ଎͘͢Δ͔ͷվળࡦ͕ग़͍ͯΔɻ
    https://medium.com/@addyosmani/progressive-web-apps-with-react-js-part-2-
    page-load-performance-33b932d97cf2

    View full-size slide

  47. ͜Ε͚ͩ͡Όͳ͍

    View full-size slide

  48. /BWJHBUJPO4UBSU 'JSTU1BJOU 'JSTU$POUFOUGVM
    1BJOU
    Loading..
    'JSTU.FBOJOHGVM
    1BJOU
    4FSWFSͰͷϨεϙϯεվળ
    %#ΫΤϦνϡʔχϯά΍ωοτϫʔΫվળ౳

    View full-size slide

  49. /BWJHBUJPO4UBSU 'JSTU1BJOU 'JSTU$POUFOUGVM
    1BJOU
    Loading..
    'JSTU.FBOJOHGVM
    1BJOU
    $SJUJDBM3FOEFSJOH$44Ͱͷௐ੔ɺ
    -PBEJOH*OEJDBUPSʹΑΔ଴ͨͤͳ͍޻෉

    View full-size slide

  50. /BWJHBUJPO4UBSU 'JSTU1BJOU 'JSTU$POUFOUGVM
    1BJOU
    Loading..
    'JSTU.FBOJOHGVM
    1BJOU
    ॳظσʔλΛGFUDI͢ΔͷͰ͸ͳ͘ɺ+40/ͱͯ͠ຒΊࠐΉ
    ΋͘͠͸ɺॳظσʔλΛ)5.-ʹ͢Δ
    ʢ4FSWFS4JEF3FOEFSJOHʣ

    View full-size slide

  51. 'JSTU.FBOJOHGVM
    1BJOU
    7JTVBMMZSFBEZ 5JNF5P
    *OUFSBDUJWF
    +4MPBEFE

    'VMMZ-PBEFE
    BMMSFTPVSDFTBSF
    MPBEFE

    BCPWFUIFGPME֎ͷը૾ͷ஗ԆಡΈࠐΈ
    ޮ཰తͳϑΥʔϚοτͷબఆ

    View full-size slide

  52. 'JSTU.FBOJOHGVM
    1BJOU
    7JTVBMMZSFBEZ 5JNF5P
    *OUFSBDUJWF
    +4MPBEFE

    'VMMZ-PBEFE
    BMMSFTPVSDFTBSF
    MPBEFE

    +4ͷ஗Ԇϩʔυɺ+4DPEFTQMJUUJOHͳͲͰͷෆཁͳίʔυ
    ͷ࡟আ

    View full-size slide

  53. 'JSTU.FBOJOHGVM
    1BJOU
    7JTVBMMZSFBEZ 5JNF5P
    *OUFSBDUJWF
    +4MPBEFE

    'VMMZ-PBEFE
    BMMSFTPVSDFTBSF
    MPBEFE

    )551΍3FTPVSDF)JOUTʹΑΔϩʔυվળ

    View full-size slide

  54. 'JSTU.FBOJOHGVM
    1BJOU
    7JTVBMMZSFBEZ 5JNF5P
    *OUFSBDUJWF
    +4MPBEFE

    'VMMZ-PBEFE
    BMMSFTPVSDFTBSF
    MPBEFE

    )551΍3FTPVSDF)JOUTʹΑΔϩʔυվળ
    ύϑΥʔϚϯεͷվળ͸େมʂʂʂ
    Ͱ΋ͦ͜ʹݸਓతʹ͸ଥڠͨ͘͠ͳ͍

    View full-size slide

  55. ·ͱΊ
    • Server Side Rendering ͸ SEO ͚ͩ͡Όͳ͍ɺॳظදࣔ( First
    Meaningful Paint )ʹޮՌతͳࢪࡦ
    • ࣮ࡍʹ࢖͓͏ͱ͢ΔͱɺCPUෛՙ͕ߴ͍ॲཧͳͷͰɺαʔόଆ
    Ͱ৭ʑHack͕ඞཁ (Ωϟογϡ΍ATFϨϯμϦϯά౳)
    • Server Side Rendering ͸αʔϏεͷಛੑ࣍ୈͰ͠ͳͯ͘΋ྑ͍
    • ύϑΥʔϚϯεʹ͸ݶΓ͕ͳ͍ɺݸਓతʹ͸͔ͩΒͱ͍ͬͯଥ
    ڠͨ͘͠ͳ͍ɻ

    View full-size slide

  56. Thank you !!!

    View full-size slide