我用Next.js 9.3.1.
在带有 SSR 的旧应用程序中。我可以getInitialProps在 HOC 组件中运行(而不是在页面中),所以我可以从 HOC 组件和页面中的服务器获取数据。像这样https://gist.github.com/whoisryosuke/d034d3eaa0556e86349fb2634788a7a1
例子 :
export default function withLayout(ComposedComponent) {
return class WithLayout extends Component {
static async getInitialProps(ctx) {
console.log('ctxlayout fire');
const { reduxStore, req } = ctx || {}
const isServer = !!req
reduxStore.dispatch(actions.serverRenderClock(isServer))
if (isServer) await reduxStore.dispatch(navigationActions.getListMenuAction('menu'));
// Check if Page has a `getInitialProps`; if so, call it.
const pageProps = ComposedComponent.getInitialProps && await ComposedComponent.getInitialProps(ctx);
// Return props.
return { ...pageProps }
}
render() {
return …Run Code Online (Sandbox Code Playgroud) static get server-side-rendering higher-order-components next.js