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

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

npn by npn
July 26, 2019
in Web development
Reading Time: 9 mins read
0
reacjt ssr nextjs

reacjt ssr nextjs

0
SHARES
858
VIEWS
Share on FacebookShare on Twitter

Contents

    • 0.1 READ ALSO
    • 0.2 What is the location of the MySQL databases?
    • 0.3 Top 10 Best WordPress SEO themes of 2022
  • 1 1. Create simple (Hello World) NextJs app
          • 1.0.0.0.1 Create empty Node.js project
          • 1.0.0.0.2 Install required dependencies
          • 1.0.0.0.3 Your package.json file will be like this
          • 1.0.0.0.4 Modify your package.json file like this
  • 2 2. What is NextJs page and how to create a NextJs page?
          • 2.0.0.0.1 Create pages directory and run Next.js script
          • 2.0.0.0.2 Create index page:
          • 2.0.0.0.3 Create about page
  • 3 3. Convenience of Server side rendering
          • 3.0.0.0.1 Now, you have known basic concept of NextJs page, let’s back into Server side rendering concept
          • 3.0.0.0.2 Linking between NextJs pages with next/link
Rate this post

Hello, this is my first post on CloudReports! From today, i will write as an author of CloudReports, i want to share my personal experiences about web development. If you want, you can create an author account and do it like me 🙂

READ ALSO

What is the location of the MySQL databases?

What is the location of the MySQL databases?

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

Top 10 Best WordPress SEO themes of 2022

March 16, 2022
499

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

NextJs Data fetching – Combine Server Side and Client Side fetching

Cloudreports – Next.js tutorials

Well, i was a backend develop for many years and just started with ReactJs for about month. Start working in a brand new context make me confused. Spending a week to learn about ReactJs, it’s syntax, build script and design flow… I can create a simple ReactJs project; not sure but i think i can understand basic concept of ReactJs.

After that, i start building my personal blog with ReactJs, every things seem okay till i read some blog’s post about SSR and i see that SSR benefits are needed for my blog 😀

Do you ever facing this scenario like me? And do you wondering that can i resolve this SSR problem?

Yes, although it took me a week but i did it with help of so many tutorial on internet (oh, with Google search too :v ) It’s not so difficult to implement ReactJs SSR.

ADVERTISEMENT

I think that i can finish my blog and start writing on it. I decided to research a bit about ReactJs advance, during that i found an awesome framework: NextJs. I was very happy to find out a greate framework but i also disapointed because all my works this month has been did very good before. I was reinvented the very good wheel 🙁

NextJs is a ReactJs base framework, it support both client side and server side rendering. One good thing is that NextJs is very easy to learn, it’s struct is simple enought for new bie to approach.

Now i will start a simple tutorial about NextJs, let’s start!

In this tutorial, we will learn about these topics:

  1. Create simple (Hello World) NextJs app
  2. What is NextJs page and how to create a NextJs page
  3. Convenience of Server side rendering

1. Create simple (Hello World) NextJs app

Create empty Node.js project
mkdir my-nextjs && cd my-nextjs
npm init -y
Install required dependencies
npm i -s react react-dom next
Your package.json file will be like this
{
    "name": "my-nextjs",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies": {
        "next": "^7.0.2",
        "react": "^16.7.0",
        "react-dom": "^16.7.0"
    }
}
Modify your package.json file like this
{
    "scripts": {
        "dev": "next",
        "build": "next build",
        "start": "next start"
    }
}

Let me explain these scripts above:

  • build Next.js will load and build bundle file for you, this process will build client side and server side bundle
  • start Run NextJs project based on built file (from build script). Note that you can’t run start without running build
  • dev This script is awesome and very convenience for development. It include build, run and folder watcher. This mean that every thing you did inside your project will trigger build, start and hot reload, you won’t have to manually restart your app nor reload the web page

Now your project is ready for development. Let’s start building a “Hello World” page

Take a look at your project file explorer, you may have a directory structure like this

Next.js starter project

Inside your project root, create a directory and named it as pages. This directory will be the place where will store all of our page source code. Next.js support many custom config, but at this time, we can’t change the behavior of the pages directory, it must be named as pages

2. What is NextJs page and how to create a NextJs page?

NextJs page is a ReactJs component, it responsible for creating output of a webpage. The example bellow will show you how to create a page

Create pages directory and run Next.js script
cd my-nextjs
mkdir pages
npm run dev
Create index page:

When we start the project by the command npm run dev Next.js will start serving at 127.0.0.1:3000. The home page (at path /) will be serve from code in file pages/index.js you can also named index file as index.jsx

Update index.js file content as bellow:

export default () => <div>Hello World.</div>

You can also export React Component class from this file

import React from 'react'

export default class HomePage extends React.Component {
    render() {
        return <div>Hello World.</div>
    }
}

After save index.js file, NextJs will automatically rebuild and serving new content, let navigate your browser to 127.0.0.1:3000 to check the output.

The result will simply a blank page with text Hello World.

Now you can imagine how can we implement a page with Next.js. Now let’s create some extra page and try more with them

Create about page

Create file pages/about.jsx

Update about.jsx with content as bellow

import React from 'react'

export default class AboutPage extends React.Component {
    render() {
        return <div>About page</div>
    }
}

Navigate to 127.0.0.1:3000/about and check

The result is: About page

As you can see, there is a simple rule for creating NextJs page: “The page file (base) name will be the uri path”. For example:

127.0.0.1:3000/about: file pages/about.js or pages/about.jsx

127.0.0.1:3000/contact: file pages/contact.js or pages/contact.jsx

Another rule for NextJs pages:

You can also use directory as NextJs page path:

127.0.0.1:3000/about: pages/about/index.js or pages/about/index.jsx

127.0.0.1:3000/contact: pages/contact/index.js or pages/contact/index.jsx

This directory design is also good for deep uri path, for example, you have this page to build: 127.0.0.1:3000/user/login so you can create your user login file by two ways:

  • Create file: pages/user/login.jsx
  • Create file: pages/user/login/index.jsx

Note: in generality, when NextJs server received a request, it will check inside pages directory to see if have the file or directory with name the same as the request path (without query string), for example: request path: /directory/sub1/sub2 NextJs will looking for pages/directory/sub1/sub2.jsx or pages/directory/sub1/sub2/index.jsx

3. Convenience of Server side rendering

Now, you have known basic concept of NextJs page, let’s back into Server side rendering concept

You know, Server side rendering (SSR) mean that, html code of a page will be build and response from server; browser just need to parse and build dom-tree from the code that they received.

So, why we need NextJs (even though ReactJs) for this classic flow? Why don’t we just use handlebar, pugjs or mustache?

The answer was not exactly to the question, but it can explain some thing relevant. When a developer mention to ReactJs, AngularJs, Vuejs, they often mean about Client Side Rendering (CSR). In real life, the aim of these above tools is just for making an easier life for frontend developer, they help working with client side code simpler. In other words, when we say: ReactJs, VueJs… we are saying about Client Side Rendering. So, when we say “ReactJs Server Side Rendering”, we are mean about thing that include both SSR and CSR;

Linking between NextJs pages with next/link

In example above, we have two pages: / and /about, let’s try linking to /about from the index page.

We need to update index.js file. First, import next/link

import Link from 'next/link'

Place a link inside the returning template:

<Link href='/about'><a>About</a></Link>
import React from 'react'
import Link from 'next/link'

export default class HomePage extends React.Component {
    render() {
        return (
            <div>
                Hello World.<br />
                <Link href='/about'><a>About</a></Link>
            </div>
        )
    }
}

Navigate to 127.0.0.1 then click link about . Take a look at browser loading indicator while you are clicking on that link. You will see that browser won’t have to reload whole html source of about page. Instead, it will send xhr request to load the needed part for displaying about page. This behavior help saving network bandwidth and also decrease page load time.

Now, you are in about page, click reload button or press ctrl + r, the browser reload whole about page and display exact what it did before. This is how SSR work.

This example is not demonstrate all benefits of SSR, it’s too simple to express all SSR features. When we have complicated apps, with multi client resources (css, js…) and complicated components struct, then NextJs really make the difference.

Now we have completed a basic tutorial about NextJs, if you have any question please let me know; or if i have wrote some thing wrong, please let me know too.

We will try to resolve some “real life” problems with NextJs in other posts. Thank you!

Examples of this blog are posted here: https://github.com/cloudreports/my-nextjs/tree/basic

References:
https://nextjs.org
https://nextjs.org/docs/#routing

Tags: Next.js
ShareTweetShare
Next Post

NextJs + KoaJs Create custom NextJs server with KoaJs

npn

npn

Related Posts

What is the location of the MySQL databases?
Linux

What is the location of the MySQL databases?

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

Top 10 Best WordPress SEO themes of 2022

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

Gmail – Gmail Sign Up – Gmail Login

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

Configuring VS Code for Node/JavaScript Development

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

How does Nodejs solve the problem of high concurrency?

July 18, 2021
1.3k
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
1k
Next Post
NextJs + KoaJs Create custom NextJs server with KoaJs

NextJs + KoaJs Create custom NextJs server with KoaJs

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