No Result
View All Result
CloudReports
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
No Result
View All Result
CloudReports
No Result
View All Result
Home Web development

NextJs Data fetching – Combine Server Side and Client Side fetching

npn by npn
July 26, 2019
in Web development
Reading Time: 7 mins read
0
fetching data in nextjs

fetching data in nextjs

0
SHARES
3.2k
VIEWS
Share on FacebookShare on Twitter

Contents

  • 1 READ ALSO
  • 2 What is the location of the MySQL databases?
  • 3 Top 10 Best WordPress SEO themes of 2022
        • 3.0.0.1 Create NextJs app with data fetching
        • 3.0.0.2 Install isomorphic-unfetch
        • 3.0.0.3 Import isomorphic-unfetch
        • 3.0.0.4 Note:
        • 3.0.0.5 Move to another page that have getInitialProps
        • 3.0.0.6 Some notes about using getInitialProps
Rate this post

Hi, in the previous post, i have introduced about NextJs and do a simple tutorial about NextJs. The app we created in previous post is very simple, we are just created a simple app with fixed content from code. You may wondering how to make a dynamic web app with NextJs, how data are controlled and how we can display data from api with NextJs?

READ ALSO

What is the location of the MySQL databases?

What is the location of the MySQL databases?

April 24, 2022
158
Top 10 Best WordPress SEO themes of 2022

Top 10 Best WordPress SEO themes of 2022

March 16, 2022
211

Check out these tutorials about Next.js

Next.js + Ant Design with less – Advanced Nextjs and Ant design scaffolding

Convert HTML template into NextJs app

Create custom self-api Next.js api server

NextJs + KoaJs Create custom NextJs server with KoaJs

[Tutorial] Create react based server side rendering app with NextJS

Cloudreports – Next.js tutorials

In this post, i will explain about NextJs data fetching, we will use the built-in method that shipped with NextJs in it’s core: getInitialProps

NextJs come with a handy method for fetching data both in Server Side and Client Side. As i have wrote in previous post, NextJs support both server side rendering and client side rendering. So data fetching must be implemented in both two sides. With NextJs, we don’t have to implement separate data fetching for server side nor client side.

NextJs build process include two sub processes: Build Server Side code, Build Client Side code. About Data Fetching, we just need to write a part of code, then it will be build into both Server Side and Client Side version.

Let’s try getInitialProps:

Create NextJs app with data fetching

Assumption that in previous example, we have to display a list of countries that provides from this api: https://cloudreports.net/sample/api/countries.json

Because we are doing data fetching, we need to use an extra library: isomorphic-unfetch, you can use any other http client library

ADVERTISEMENT
Install isomorphic-unfetch
import 'isomorphic-unfetch'
npm i isomorphic-unfetch -s
Import isomorphic-unfetch

We will create a static method called getInitialProps

static async getInitialProps() {

}

We use async here because our http request is asynchronous and it need to be completed before return any data.

Calling http request and wait for it’s response:

static async getInitialProps() {
    let res = await fetch('https://cloudreports.net/sample/api/countries.json')
    let contriesObj = await res.json()
    console.log(contriesObj)
    return contriesObj
}
Note:
  • You should use try catch in case of broken api link or network error
  • getInitialProps must return a javascript object, you can’t return a string or number. If do so, NextJs will throw error in this page.
  • In this example, we use isomorphic-unfetch and json method returning a js object parsed from api result
  • console.log is just for testing api result
  • getInitialProps won’t run until you reload the page contain it. In this case, we need to reload 127.0.0.1:3000

The final index.jsx file will be this:

import React from 'react'
import Link from 'next/link'
import 'isomorphic-unfetch'

export default class HomePage extends React.Component {
    static async getInitialProps() {
        let res = await fetch('https://cloudreports.net/sample/api/countries.json')
        let contriesObj = await res.json()
        console.log(contriesObj)
        return {countries: contriesObj}
    }

    render() {
        return (
            <div>
                Hello World.<br />
                <Link href='/about'><a>About</a></Link>
            </div>
        )
    }
}

The result:

The console result when we reload 127.0.0.1:3000

Now, let me explain a bit about getInitialProps

getInitialProps is a special method that only existed in NextJs. it only take affect in page level components. When server received a request, NextJs will check corresponding component to see if it have getInitialProps method. If the current component don’t have this method, NextJs will treat it as a normal component and start rendering.

If a page component contain getInitialProps method, NextJs will call getInitialPropsfirst, the result returned from this method will be assign to the component as it’s props. That is why getInitialProps must return an object, otherwise the merge action will cause error!

Because NextJs will pass getInitialProps‘s result as component props, we can use it in our render method

Let’s modify the render method to display all available countries:

render() {
    return (
        <div>
            Hello World.<br />
            <Link href='/about'><a>About</a></Link>
            <h2>Country list</h2>
            <ul>
                {this.props.countries.map((country, i) => {
                    return (
                        <li key={'country-' + i}>{country.name}</li>
                    )
                })}
            </ul>
        </div>
    )
}
The result as expected!
Move to another page that have getInitialProps

We have index page with getInitialProps , what happen if our about page have getInitialProps too? Is getInitialProps work on xhr request? Let’s check

Modify about page so it will fetch https://cloudreports.net/sample/api/countries.json and display a list of country codes:

import React from 'react'
import Link from 'next/link'

export default class AboutPage extends React.Component {
    static async getInitialProps() {
        let res = await fetch('https://cloudreports.net/sample/api/countries.json')
        let contriesObj = await res.json()
        return {countries: contriesObj}
    }
    render() {
        return (
            <div>
                About page<br />
                <Link href='/'><a>home</a></Link>
                <h2>Country codes</h2>
                <ul>
                    {this.props.countries.map((country, i) => {
                        return (
                            <li key={'country-' + i}>{country.code}</li>
                        )
                    })}
                </ul>
            </div>
        )
    }
}

Now navigate to 127.0.0.1:3000 and open developer tool ( network tab)

NextJs sample with developer tool

click on about link

as you can see, the loading indicator didn’t animated, browser just send xhr request to fetch countries.json (as api endpoint). Every thing work as expected!

Some notes about using getInitialProps
  • We usually use this method to fetch data from external resource (apis), so don’t for get async
  • Always place code inside try catch so we can handle unexpected error
  • getInititalProps must return an object, this object will be passed in to component props
  • This method will be built into both client and server version, so be careful when expose secure information here

There are some advance and notable tips about using getInitialProps, i will go deep into these tips in other post. Thank you for reading and please tell me if you have any problems!

Tags: Next.js
ShareTweetShare
Previous Post

Create custom self-api Next.js api server

Next Post

Convert HTML template into NextJs app

npn

npn

Related Posts

What is the location of the MySQL databases?
Linux

What is the location of the MySQL databases?

April 24, 2022
158
Top 10 Best WordPress SEO themes of 2022
Web development

Top 10 Best WordPress SEO themes of 2022

March 16, 2022
211
Gmail – Gmail Sign Up – Gmail Login
Web development

Gmail – Gmail Sign Up – Gmail Login

August 30, 2021
6.5k
Configuring VS Code for Node/JavaScript Development
Javascript

Configuring VS Code for Node/JavaScript Development

August 2, 2021
809
How does Nodejs solve the problem of high concurrency?
Javascript

How does Nodejs solve the problem of high concurrency?

July 18, 2021
888
How to create a self-signed SSL certificate for Apache on Ubuntu 16.04
Linux

How to create a self-signed SSL certificate for Apache on Ubuntu 16.04

July 18, 2021
682
Next Post
Convert HTML template into NextJs app

Convert HTML template into NextJs app

Discussion about this post

No Result
View All Result

Categories

  • Android (1)
  • Ant Design tutorial (7)
  • App/Game (2)
  • Javascript (16)
  • Layout and Routing (2)
  • Linux (9)
  • PC & LAPTOP (6)
  • PERSONAL FINANCES (1)
  • React (13)
  • SQL (2)
  • TECHNOLOGY & DIGITAL (7)
  • The Basics (5)
  • Web development (37)

Search

No Result
View All Result

Categories

  • Android (1)
  • Ant Design tutorial (7)
  • App/Game (2)
  • Javascript (16)
  • Layout and Routing (2)
  • Linux (9)
  • PC & LAPTOP (6)
  • PERSONAL FINANCES (1)
  • React (13)
  • SQL (2)
  • TECHNOLOGY & DIGITAL (7)
  • The Basics (5)
  • Web development (37)
No Result
View All Result
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner