Share
Embed
Transcript
38 slides
-
我々が良く知るRSC 10 import { Client } from “./client.tsx”; export const
SC = () => { return ( <div> <Client /> </div> ); }; server.tsx “use client”; export const Client = () => { return ( <p>client!</p> ); }; client.tsx import サーバーからクライアントをimportしてコンポーネントを使用できる。 -
フレームワーク無しのRSC 11 const Client = { /* 省略 */ };
export const SC = () => { return ( <div> <Client /> </div> ); }; server.tsx export const Client = () => { return ( <p>client!</p> ); }; client.tsx 独立 サーバーとクライアントは独立した、別々のプログラムになる。 -
フレームワーク無しのRSC 12 const Client = { /* 省略 */ };
export const SC = () => { return ( <div> <Client /> </div> ); }; server.tsx server.tsxはサーバー側用のReactランタイムで 実行。Clientはサーバー側でレンダリングしない。 Clientは「Clientというコンポーネント」の ままクライアント側に送られる。 クライアント側がClientのレンダリングを 担当。 -
フレームワーク無しのRSC 13 export const Client = () => { return
( <p>client!</p> ); }; client.tsx client.tsxはクライアント用のReactランタイムで 実行。 SC部分がただのHTMLとなった状態で クライアント側のランタイムに渡される。 こちらはClientの定義を知っており、 Clientをレンダリングできる。 -
フレームワーク無しのRSCの問題 14 const Client = { /* 省略 */ };
export const SC = () => { return ( <div> <Client /> </div> ); }; server.tsx export const Client = () => { return ( <p>client!</p> ); }; client.tsx 別々のプログラムで2つの定義を同期させる必要がある。非常にDX悪い。 -
Reactフレームワークの構造 18 サーバー用 Reactランタイム クライアント用 Reactランタイム Reactの機能 バンドラの機能 RSC規約に従った サーバー・クライアント分割
及びビルド フレームワーク の個性 ルーティング キャッシュ 最適化 など バンドラプラグイン -
置き換えの実例 置き換え前(実際のクライアント用モジュール) 'use client'; export function Greet({ name }: {
name: string }) { return <>Hello {name}</>; } 23 Waku v0.25.0 のソースコードから一部改変して引用(次のスライドも同じ) https://github.com/wakujs/waku/blob/473d481f0d926e05c9b0b8af5d2b777ff47eb70b/packages /waku/tests/vite-plugin-rsc-transform-internals.test.ts -
置き換えの実例 置き換え後(サーバー側から見える同モジュール) import { registerClientReference } from 'react-server-dom-webpack/server.edge’; export const
Greet = registerClientReference(()=>{ throw new Error('It is not possible to invoke a client function from the server: /src/App.tsx#Greet'); }, '/src/App.tsx', 'Greet'); 24 -
バンドラプラグインの実装① { name: 'rsc:use-client', async transform(code, id) { /* 中略
*/ const ast = await parseAstAsync(code) if (!hasDirective(ast.body, 'use client')) { delete manager.clientReferenceMetaMap[id] return } /* 以下略 */ 28 ‘use client’ を持つ ファイルが変換対象 -
バンドラプラグインの実装② const result = transformDirectiveProxyExport_(ast, { /* 省略 */ runtime:
(name, meta) => { let proxyValue = /* 省略 */ return ( `$$ReactServer.registerClientReference(` + ` ${proxyValue},` + ` ${JSON.stringify(referenceKey)},` + ` ${JSON.stringify(name)})` ) }, 29 各exportを registerClientRefere nce呼び出しに変換 -
Reactフレームワークの構造(再掲) 34 サーバー用 Reactランタイム クライアント用 Reactランタイム Reactの機能 バンドラの機能 RSC規約に従った サーバー・クライアント分割
及びビルド フレームワーク の個性 ルーティング キャッシュ 最適化 など バンドラプラグイン -
答え②: 境目はこんな感じ 35 サーバー用 Reactランタイム クライアント用 Reactランタイム Reactの機能 バンドラの機能 サーバー・クライアント分割とビルド
RSC規約 フレームワーク の個性 ルーティング キャッシュ 最適化 など バンドラプラグイン