gpt4 book ai didi

node.js - Loadable.Capture 不报告任何模块

转载 作者:IT老高 更新时间:2023-10-28 23:21:35 26 4
gpt4 key购买 nike

这基本上是我的所有代码。我正在运行 Hapi 并尝试使用 react-loadable 来服务器渲染我的 React 应用程序。

我在代码 here 中添加了很多缺失的部分.

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: []。不管缓存。因此,即使我禁用缓存,刷新页面两次以查看路由器是否正常工作,模块仍然是空的。

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 []

我有点迷茫,因为它应该可以工作.. 一切看起来都很好。

最佳答案

今天不知怎么折腾了好久,终于解决了。

在获得模块之前,您必须正确编写可加载组件。官方文档声明here

Loadable({
loader: () => import('./Bar'),
modules: ['./Bar'],
webpack: () => [require.resolveWeak('./Bar')],
});

或者你可以使用提供的 babel 插件。但是,就我而言,我正在使用 ts-node 并且没有使用该 babel 插件的运气,因此只有选项 1 可用。

写在这里,因为有人会遇到类似的问题。

(p.s. lodable-component 也依赖 babel 插件)

关于node.js - Loadable.Capture 不报告任何模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51224604/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com