これは本質的にすべての私のコードです。Hapi を実行していて、React アプリケーションをサーバーでレンダリングするために react-loadable を使用しようとしています。
ここのコードに不足している多くの部分を追加しました。
const location = req.url.pathname
const context = {}
const modules = []
const Router = () => (
<Switch>
<Route exact path="/" component={Home} />
<Route path="/login" component={Login} />
<Route path="/me" component={Profile} />
<Route component={NotFound} />
</Switch>
)
const App = () => (
<StaticRouter location={location} context={context}>
<main>
<Header />
<Router />
<Footer />
</main>
</StaticRouter>
)
const preloadables = [ Home, Login, Profile, NotFound ]
await Promise.all(preloadables.map(preloadable => preloadable.preload()))
const html = ReactDOMServer.renderToString(
<Loadable.Capture report={moduleName => modules.push(moduleName)}>
<App/>
</Loadable.Capture>
)
// render html
ブラウザでレンダリングされた React アプリを見ると、ページが正しくレンダリングされます。サーバーのキャッシュを無効にしない限り、「ルーター」の内容は表示されませんが。したがって、最初のリクエストはルーターをレンダリングせず、次のリクエストはレンダリングします。
サーバーでは、常にconsole.log('modules:', modules)
戻りますmodules: []
。キャッシュ関係なく。そのため、キャッシュを無効にしても、ルーターが動作していることを確認するためにページを 2 回更新しても、モジュールは空のままです。
npm run start:server
Warning: setState(...): Can only update a mounting component. This usually means you called setState() outside componentWillMount() on the server. This is a no-op.
Please check the code for the LoadableComponent component.
modules []
modules []
modules []
modules []
modules []
それは機能しているはずなので、少し迷っています..すべて問題ないようです。