最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NextJS: getInitialProps method - Stack Overflow

programmeradmin10浏览0评论

Working with NextJS and saw an example of a page:

class IndexPage extends Component {
  static async getInitialProps(context) {
    return {};
  }
  render() {
    return <div>hello world</div>;
  }
}
export default withRouter(IndexPage);

What exactly does the getInitialProps method in NextJS?

Working with NextJS and saw an example of a page:

class IndexPage extends Component {
  static async getInitialProps(context) {
    return {};
  }
  render() {
    return <div>hello world</div>;
  }
}
export default withRouter(IndexPage);

What exactly does the getInitialProps method in NextJS?

Share Improve this question edited Nov 7, 2018 at 2:55 Tu Nguyen 10.2k4 gold badges29 silver badges52 bronze badges asked Sep 2, 2018 at 12:03 Willem van der VeenWillem van der Veen 36.6k18 gold badges205 silver badges176 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 16

getInitialProps is usually an async function which is good for asynchronous operations at the server and then it passes data to the page as props.

It can run both on the server and on the browser(if you use Link for example).

My conclusion would be to use getInitialProps to fetch data when your ponent acts as a Page, and you want to provide the data as Props.

Docs: https://nextjs/learn/basics/fetching-data-for-pages

Notice that to load data when the page loads, we use getInitialProps which is an async static method. It can asynchronously fetch anything that resolves to a JavaScript plain Object, which populates props.

Data returned from getInitialProps is serialized when server rendering, similar to a JSON.stringify. Make sure the returned object from getInitialProps is a plain Object and not using Date, Map or Set.

For the initial page load, getInitialProps will execute on the server only. getInitialProps will only be executed on the client when navigating to a different route via the Link ponent or using the routing APIs.

Note: getInitialProps can not be used in children ponents. Only in pages.

getInitialProps

  • is used to asynchronously fetch some data, which then populates props.
  • For the initial page load, getInitialProps will execute on the server only.
  • will only be executed on the client when navigating to a different route via the next/link ponent or by using next/router.
  • can not be used in children ponents, only in the default export of every page.

If you're using Next.js 9.3 or newer, it is remended that you use getStaticProps or getServerSideProps instead of getInitialProps.

These new data fetching methods allow you to have a granular choice between static generation and server-side rendering.

Source: https://nextjs/docs/api-reference/data-fetching/getInitialProps

Motivation for using getInitialProps:

All of these answers are correct, but I would like to add that NextJS does server-side rendering. Which means that if you want to fetch data before showing the user something you can't use ponentDidMount (because that happens after rendering).

So you need to use getInitialProps because it is executed first, and after that NextJS is rendering the page. Then NextJS takes the ponent's HTML that is produced and sends it to the browser.

"getIntialProps" is usually used to fetch data from server. It runs on server and client both but with one basic difference ie: to make it work on client side the route has to be hit either from "router.push(/routename)" or by next js 'Link ' ponent.

All this data can be returned to ponent in from of props.

Note: "getIntialProps" does not have access to application props.

getInitialProps is used when you would like your page to request data on server-side instead of client-side, it allows to take advantage of SEO.

I will try my best to explain this question:

First you have to ask yourself what is server-side rendering and why we need server-side rendering ?

server-side-rendering in React mean the data is rendering from the server not the client, its important to note that ReactJs (not NextJS) always renders anything in browser not server.

So when user fill the address in browser address bar, the request from browser get sent to server, and information pass between server to client in React (not next) just a bunch of javascript and a div tag (width id="app" most of the time), and that is a problem with SEO.

Because there are not much information for search engine crawler (Google) to parse (it will ask itself, reall ? a signle div tag with a bunch of JS ? really ? what the heck is this ?? naaahh I am tired of this I will go index the other page and leave this page next time... so on), and search engine feel difficult to understand the content of that site, mean it is NOT optimized for search engine (rememeber SEO = search engine optimization).

So that is why we need to render the data from the server so that it will make search engine crawler feel fortable to parse the information of the site for sure.

And to render data from the server in NextJs we need to pass props from getIntitialProps. This getIntitialProps is where we will render the data (for example, a lits of product, the name of product, the price...) from server side.

发布评论

评论列表(0)

  1. 暂无评论