,

Creating a makeshift Next.js from scratch with server-side rendering

Posted by









Makeshift Next.js from Scratch (Server-Side Rendering)

Makeshift Next.js from Scratch (Server-Side Rendering)

Next.js is a popular React framework for building server-side rendered applications. In this article, we will explore how to create a makeshift Next.js application from scratch, complete with server-side rendering.

Getting Started

To get started with our makeshift Next.js application, we will need to first set up a new project directory and install the necessary dependencies. We can do this by running the following commands in our terminal:

    
      mkdir makeshift-nextjs
      cd makeshift-nextjs
      npm init -y
      npm install react react-dom next
    
  

Once we have our project directory set up and the necessary dependencies installed, we can create our first page in the pages directory. For example, we can create a file called index.js with the following content:

    
      import React from 'react'

      const IndexPage = () => {
        return (
          <div>
            <h1>Welcome to Makeshift Next.js!</h1>
            <p>This is a makeshift Next.js application created from scratch.</p>
          </div>
        )
      }

      export default IndexPage
    
  

Server-Side Rendering

One of the key features of Next.js is its support for server-side rendering. This means that our pages are rendered on the server and sent to the client as HTML, which can improve performance and SEO. To enable server-side rendering in our makeshift Next.js application, we can use the getServerSideProps function in our page components. For example:

    
      import React from 'react'

      const IndexPage = ({ message }) => {
        return (
          <div>
            <h1>Welcome to Makeshift Next.js!</h1>
            <p>{message}</p>
          </div>
        )
      }

      export async function getServerSideProps() {
        // Fetch data from an API or database
        const message = 'This is a message from the server!'

        return {
          props: { message }
        }
      }

      export default IndexPage
    
  

Conclusion

Creating a makeshift Next.js application from scratch with server-side rendering is a great way to learn more about how Next.js works under the hood. By following the steps outlined in this article, you can gain a better understanding of the key concepts and features of Next.js and how to leverage them to build powerful server-rendered applications.


0 0 votes
Article Rating
27 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Saurav Varma
11 months ago

This pops up while I was writing the MVP checklist for open sourcing our framework. I am so hopeful that this is going to inspire people all around and also take away from the 'magic' that is associated with frameworks! Thanks, Tejas!

Branko Toshin
11 months ago

This is very interesting.

Would you mind explaining how hydration is working in this example?
Server is rendering page aka Component to the pipeable stream. And when react picks things up on a client it needs to do a hydration, but since client side react contains more code, for example whole <APP /> component while server just sends page back I am interested to know how hydration comes into play.

If APP component contains additional jsx besides Router etc, like <h1>Hello from the app</h1>, it doesnt cause any hydration issues and I am wondering why.

Is that because createRoot method is used instead of hydrateRoot?

Thanks a lot.

i like url
11 months ago

Creating library/framework should be series. Building reactjs from scratch, remix would be much appreciated for people like me but don't know where to start

Gaurav Bhandari
11 months ago

Masterpiece 🔥🔥

Kushal Dave
11 months ago

I usually not comment until I find something best content. For sure Tejas your content and way of explaining is extreme fabulous. Thanks for this and all other content of yours.

luck 578
11 months ago

Hey tejas why do you do React.useState()? why not just use useState()? is there any difference?

Moh Misaghi
11 months ago

very helpful thanks

Moh Misaghi
11 months ago

Thanks, I can not find the video "react from scratch" can you link it in the description please ?

Robert Soriano
11 months ago

Love this! Thank you.

Recursion
11 months ago

your makeshift react video from one of those conf was really eye opening and I already know this video is going to be fire, saved to my watch later. (comment for algo)

Shrijan Tripathi
11 months ago

Wow ❤😮

anshul
11 months ago

source coded please!! I would like to build something on top of this and write a post as well

arturo rugh
11 months ago

Thanks for sharing! Could u please add a git with the source code to the description? Many of us would appreciate it. Thanks

Mykola Vlasov
11 months ago

Thanks for the great video!
Do you have the initial + final versions somewhere in the github?

Ashok Ramesh
11 months ago

One of the best video about React. Keep it coming please… Subscribed.

Danilo Guedes Dev
11 months ago

Bro, that was a masterpiece… I have always wanted to go deep into a next.js source code to undestantand the Magik… tks a lot !! 🎉

Ameen Charoliya
11 months ago

This type of content can't be found anywhere. Frankly I always saw frameworks as user perspective but man from today I will dig deeper to understand how it works. I felt like I have a senior friend who's casually showing me how it's done. Thanks a lot Tejas. Please keep up the good work.

GustavoD
11 months ago

Thank you random Indian guy from the internet.

Pratik Tiwari
11 months ago

Please make similar content building things from scratch which is kind of like a black box for us

Vikrant Bhat
11 months ago

This is insightful Tejas! Thanks for sharing.