Skip to content

TaishiKobari/slides-ssr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

marp
true

SSR Streaming と React Server Components


reactwg/react-18#37 h:200px

https://github.com/josephsavona/rfcs/blob/server-components/text/0000-server-components.md h:200px


例としてこのコンポーネントを使ってます

const App = () => {
  const [count, setCount] = useState(0)
  const increment = useCallback(() => setCount((c) => c + 1), [])
  return (
    <div>
      <button onClick={increment}>increment</button>
      <p>count: {count}</p>
    </div>
  )
}

CSR

初めは 1 つの div だけ送信した後、JS で DOM 操作して UI を作るやり方

<html>
  <body>
    <div id="button_container"></div>
    //JS
    <script src="like-button.js"></script>
  </body>
</html>


SSR

<html>
  <body>
    <div id="button_container">
      // serverで生成したhtml
      <div data-reactroot="">
        <button>increment</button>
        <p>
          count:
          <!-- -->0
        </p>
      </div>
      //
    </div>

    //JS
    <script src="like-button.js"></script>
  </body>
</html>

初期描画 →hydrate→ 最終描画

h:600 bg right h:300px


従来の SSR の課題

  • HTML を返すためにすべての component を renderToString しないといけない
  • hydrate するためにすべての JS を読み込まないといけない

etc...

レスポンスの遅い API を叩いていたり、複雑な JS 処理を(カルーセルなど)もつ component があるとブロッカーとなりやすい


SSR Streaming

HTML 生成が終わったところから、順次 client に返却していく

h:400


SSR Streaming

読み込んだ JS から順次 hydrate していく

h:400


SSR Streaming

HTML が全て揃う前に hydrate 開始 緊急性によって hydrate する順番を入れ替える
h:300 h:300

巨大化する JS

import marked from 'marked'; // 35.9K (11.2K gzipped)
import sanitizeHtml from 'sanitize-html'; // 206K (63.3K gzipped)

function NoteWithMarkdown({text}) {
  const html = sanitizeHtml(marked(text));
  return (/* render */);
}

必要なのは数 byte の処理なのに 200kbyte を読み込まないといけない。。


React Server Components

  • Server Components(***.server.js)
  • Client Components(***.client.js)
    • Client Component の中では、Server Component を読み込めない

React Server Components

server 側でやる処理

native dom(div など)か Client Component に到達するまで、Server Component をレンダーする


こんなのがclientに送られるイメージ

{
  '$$typeof': Symbol(react.element),
  type: 'ClientComponent', // ClientComponent
  key: null,
  ref: null,
  props: { children: [
    {
  '$$typeof': Symbol(react.element),
  type: 'div', // div
  key: null,
  ref: null,
  _owner: null,
  _store: {}

    }
   ] },
  _owner: null,
  _store: {}
}

SSRとServer Componentsの比較

SSR Server Components
やり取りするもの HTML string React Object(JSON)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published