NextJS is a framework for creating pre-rendered React websites.
NextJS offers us both SSR(Server SIde Rendering) and SSG(Static Site Generation)
At this point lemme describe these terms...
SSR - React components are rendered into Html pages on the server on each request
SSG - Components are rendered onto Html pages at build time
This means react components are already rendered into Html pages before they reach the browser.
- on the flip side, without Next JS react renders components into the browser -> Client-Side Rendering.
So what are the benefits of SSG & SSR?
- Pre-rendering improves performance
- Better SEO(Search engine optimization) - response from the server is a fully rendered Html page meaning there are no blank pages.
So what are the prerequisites to building with Next JS?
- A basic to a good understanding of React.
- NodeJS installed on your pc - preferably version 10.13 and newer.
nice to have
- a good understanding of git version control.
while I might not dive into building a simple web app with NextJs, I found a thread of amazing tutorials you can learn from.
Built on top of React, Next.js has some incredible features such as server-side rendering, page routing & an impeccable developer experience.
If you want to learn more check out the resources below 🔽14:11 PM - 13 Apr 2021
getStaticProps, ** getServerSideProps*, and **getStaticPaths* - when you build your next app for production, next is gonna prefetch any code in the background that might be needed when the user clicks on a link in the page. It does this by looking at all the link components on the current page and prefetches all the code for any of those pages.
getStaticProps fetches data at runtime,
getStaticPaths - specifies dynamic routes to pre-render pages based on data.
getServerSideProps - fetches data on each request.
Optimized Image component - another cool concept introduced in NextJs v10; the
Image component extends the Html
<img/> element and optimizes it by allowing for resizing, optimizing, and serving images in modern formats like WebP when the browser supports it. The neat thing about it is; this component comes with lazy loading enabled by default. That means your page speed isn't penalized for images outside the viewport. Images load as they are scrolled into the viewport.
Incremental Static Regeneration - NextJs generates and regenerates new pages on the fly when data is updated. Say we update data in the backend for a web app; there's no change on the first visit after the content is changed. The page visit however triggers NextJs to re-fetch the page data in the background. The static page is regenerated ready for the next visit.
Fallback pages - these are placeholder pages/content whilst NextJs fetches data for the page. Say you visit a new page that was created by adding content to the data. The best practice is to have a fallback page show instead of a 404 page. Once the data is fetched, NextJs pumps it into the page component so the page can be rendered.
Fast refresh - this feature gives you instantaneous feedback on edits made to your components. It is enabled by default in the latest versions.
Better error handling - If you make a syntax error during development, you can fix it and save the file again. The error will disappear automatically, without losing the component state.
Typescript - NextJs provides typescript support out-of-the-box.
Go ahead and try NextJs, then lemme know how your experience was.
You can find a few examples of NextJs apps on my github. here's an example.
A Next.Js web app with Contentful Headless CMS.
And here's the production version of the same.