Mastering Next.js: My Journey from SSG to SSR Through Trial and Error

HARSH SINGH - Jul 29 - - Dev Community

Hi Guys! In this blog i wanna share with you the next new project that I've been working on for a good amount of time. It really takes me back to when i started my journey with MERN stack when i used to struggle in every project that I made but after having gone through all that hardships I can still say that getting stuck on some topics still freaks me out a little bit.

I want to share with all of you about the difference between SSG and SSR.
I was doing a minor project of making a blog site and i wanted to generate the blogs on the build time but It was not genrating and after 1 week of searching and wandering on internet learning about the difference I finally understood what is the difference.

Introduction

Next.js is a powerfull React framework developed by vercel. It has several built-in features to enhance development and user experience.
Some of the features are:

  • Hybrid Static & Server Rendering : It supports both SSR and SSG, allowing developers to choose best of any according to their use-case.
  • API Routes : It allows you to build API endpoints in the nextjs app itself eliminating the need for a separate backend service for simple API tasks.
  • Automatic Code Splitting and Optimization : It splits the code and only send necessary javasript to browser and has many built-in optimization like Image optimization, faster builds etc.
  • File-system Routing : It has a file sytem routing(whichever you are using app or page router) automatically creates routes.
  • TypeScript Support : It is recommended to code in typescript as it catches many errors in the runtime only which if not used may cause problems in build time.

SSG VS SSR

Server Side Generation (SSG)

SSG generates static HTML pages at the build time. The content is pre-rendered which means it doesn't change until you rebuild the site.
It is best for pages that doesn't change too frequetly like the Marketing pages or documentation which doesn't change too frequently.
It basically is generated during the build time and are static so they load extremely quickly.

In this to get the Dynamic path that you want to render on build time you can use the getStaticParams. You can read more about this here. Link

Server Side Rendering(SSR)

SSR generates HTML pages on each request. The server renders the HTML dynamically for each incoming request. It is best for pages that need to be dynamic and up-to-date on every request. This includes user dashboards, news feeds, and other content that changes frequently or is user-specific.
Since HTML is generated on each request so it is slower than SSG but it ensures that the content is always fresh and up-to-date.

Final Thought

My journey with Next.js, SSG, and SSR has been filled with learning opportunities. Each error and challenge has deepened my understanding and improved my problem-solving skills. I encourage you to experiment with both SSG and SSR in your projects to see how they can best serve your needs.

Thank you for reading! I hope my experiences help you in your Next.js journey. Feel free to share your own experiences or ask questions in the comments below. Let's continue learning and growing together as a community. Happy coding!

. .
Terabox Video Player