是否要在next.js的自定义_app组件中使用`getInitialProps`来禁用客户端渲染?

问题描述 投票:0回答:1

根据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,这将导致页面在服务器端呈现。但这是否意味着客户端渲染将不起作用(导航等)?如果不是,那么] >>

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

根据next.js官方文档:从'react'导入React从'next / app'类导入App MyApp扩展了App {// //如果对...有阻止数据要求,请取消注释此方法。 [

您没有正确地获得它,只有Automatic Static Optimization将被禁用,因为如果您正在使用getInitailProps,则意味着该页面不是静态的(需要从服务器获取其他数据以进行渲染),因此Next不能为此生成一个静态

html文件

reactjs server-side client-side next.js serverside-rendering
1个回答
0
投票
您没有正确地获得它,只有Automatic Static Optimization将被禁用,因为如果您正在使用getInitailProps,则意味着该页面不是静态的(需要从服务器获取其他数据以进行渲染),因此Next不能为此生成一个静态

html文件

© www.soinside.com 2019 - 2024. All rights reserved.