在next.js的自定义_app组件中使用`getInitialProps`是否会禁用客户端呈现?

根据next.js官方documentation

import React from 'react'
import App from 'next/app'

class MyApp extends App {
  // Only uncomment this method if you have blocking data requirements for
  // every single page in your application. This disables the ability to
  // perform automatic static optimization,causing every page in your app to
  // be server-side rendered.
  //
  // static async getInitialProps(appContext) {
  //   // calls page's `getInitialProps` and fills `appProps.pageProps`
  //   const appProps = await App.getInitialProps(appContext);
  //
  //   return { ...appProps }
  // }

  render() {
    const { Component,pageProps } = this.props
    return <Component {...pageProps} />
  }
}

export default MyApp

据我了解,如果我在getInitialProps文件中使用_app.js,这将导致页面在服务器端呈现。但这是否意味着客户端渲染将不起作用(导航等)?如果不是,那是什么意思

  

使应用中的每个页面都在服务器端呈现。

ilovemyf 回答:在next.js的自定义_app组件中使用`getInitialProps`是否会禁用客户端呈现?

您的设置不正确,只有Automatic Static Optimization会被禁用,原因是如果您使用的是getInitailProps,则意味着页面不是静态的(需要从服务器获取其他数据才能呈现),因此Next无法为其生成静态的 html文件

本文链接:https://www.f2er.com/2996953.html

大家都在问