Abdul Ahad Siddiqui
Imagine walking into a bakery with the sweet aroma of freshly baked pastries. Every time you order a pastry, it's baked just for you, with each ingredient carefully measured and mixed to create a delicious, warm treat. Now imagine another bakery where you walk in and find a shelf full of ready-made pastries, each wrapped and waiting for you to grab and go. Both bakeries have their charm, but they cater to different needs and preferences.
In the world of web development, these two bakeries represent the concepts of server-side rendering (SSR) and static site generation (SSG). These methodologies are at the core of modern web development, and NextJS, a popular React framework, has seamlessly blended them together, creating a delightful experience for developers and users alike.
SSR is like the first bakery, where each pastry is baked to order. In this approach, the server processes a user's request and generates the HTML content on the fly. This dynamic rendering allows the server to serve personalized content and ensure that search engines can crawl and index the content properly.
Here's an example of how you can use SSR with NextJS:
import { GetServerSideProps } from 'next';
export default function Home({ data }) {
return (
<div><h1>My SSR Page</h1><p>{data.message}</p></div>
);
}
export const getServerSideProps: GetServerSideProps = async () => {
const res = await fetch('https://api.example.com/data');
const data = await res.json();
return {
props: { data },
};
};
In this example, the
getServerSideProps
function fetches data from an API and passes it as props to the Home
component. NextJS will take care of running this function on the server side when a user requests the page, generating the HTML with the fetched data.SSG is like the second bakery, where the pastries are prepared and packaged beforehand. In this approach, the HTML pages are generated at build time, creating a set of static files that can be served to the user without any server-side processing. This method is ideal for content that doesn't change frequently, as it results in faster load times and improved performance.
Here's an example of how you can use SSG with NextJS:
import { GetStaticProps } from 'next';
export default function Home({ data }) {
return (
<div><h1>My SSG Page</h1><p>{data.message}</p></div>
);
}
export const getStaticProps: GetStaticProps = async () => {
const res = await fetch('https://api.example.com/data');
const data = await res.json();
return {
props: { data },
};
};
In this example, the
getStaticProps
function is used instead of getServerSideProps
. This function is run during build time, fetching the data and generating the static HTML pages that will be served to the user.NextJS is like a magical bakery that offers both fresh and pre-made pastries, delighting customers with the best of both worlds. The framework allows you to combine SSR and SSG in a single application, giving you the flexibility to choose the best approach for each page.
By leveraging the power of NextJS, you can create fast, dynamic,and highly optimized web applications that cater to a wide range of user needs and preferences. NextJS takes care of many performance optimizations and developer conveniences out of the box, making it a go-to choice for modern web development.
NextJS also brings an innovative feature called Incremental Static Regeneration (ISR) to the table, which allows you to update static content without rebuilding the entire site. It's like having a bakery that continuously adds freshly baked pastries to the pre-made selection, ensuring that the treats are always fresh and up-to-date.
Here's an example of how you can use ISR with NextJS:
import { GetStaticProps } from 'next';
export default function Home({ data }) {
return (
<div><h1>My ISR Page</h1><p>{data.message}</p></div>
);
}
export const getStaticProps: GetStaticProps = async () => {
const res = await fetch('https://api.example.com/data');
const data = await res.json();
return {
props: { data },
revalidate: 60, // Regenerate the page every 60 seconds
};
};
In this example, the
revalidate
property is added to the return object of the getStaticProps
function. This tells NextJS to regenerate the page at most once every 60 seconds if there's a request for it, ensuring that the content stays fresh while still benefiting from the performance gains of static site generation.By harnessing the power of NextJS and its blend of SSR, SSG, and ISR, you can create incredibly fast and dynamic web applications that provide a great user experience. It's like having the ultimate bakery that can cater to every customer's taste, whether they crave a warm, freshly baked pastry or a convenient pre-made treat.
NextJS has established itself as a nexus in the modern web development ecosystem, offering developers the flexibility and convenience to craft beautiful, performant, and scalable applications. So, step into the delightful world of NextJS and start baking your perfect blend of server-side rendering and static site generation today!
Hi there, I'm Abdul Ahad Siddiqui, a Software Engineer 🚀 from India. I'm a passionate learner who's always willing to learn and work across technologies and domains 💡.