?? Solution: Incremental Static Regeneration (ISR) + WebSockets
Wagner Santos
Senior Frontend Engineer | React | Web developer | TypeScript | JavaScript | AWS
To solve this, we use Next.js with ISR to optimize the initial load and WebSockets to update the data in real time without requiring a full page refresh.
?? Implementation
1?? Optimizing Initial Load with ISR
Incremental Static Regeneration (ISR) allows generating static pages periodically, improving performance without requiring Server-Side Rendering (SSR) for every request.
export async function getStaticProps() {
const res = await fetch("https://api.example.com/data");
const data = await res.json();
return {
props: { data },
revalidate: 60,
};
}
export default function Dashboard({ data }) {
return (
<div>
<h1>Sales Dashboard</h1>
<p>Total Sales: {data.totalSales}</p>
</div>
);
}
? Advantage: The page loads super fast because Next.js generates a static HTML file that updates every 60 seconds.
2?? Real-Time Data Updates with WebSockets
To allow users to see new sales without refreshing the page, we use WebSockets to receive updates automatically.
领英推荐
import { useState, useEffect } from "react";
import io from "socket.io-client";
const socket = io("https://api.example.com");
export default function Dashboard({ initialData }) {
const [data, setData] = useState(initialData);
useEffect(() => {
socket.on("new-sale", (newSale) => {
setData((prevData) => ({
...prevData,
totalSales: prevData.totalSales + newSale.value,
}));
});
return () => socket.off("new-sale");
}, []);
return (
<div>
<h1>Sales Dashboard</h1>
<p>Total Sales: {data.totalSales}</p>
</div>
);
}
? Advantage: When a new sale occurs, the backend emits an event via WebSockets, and the data updates instantly for all users.
?? Results
? The dashboard loads quickly since it doesn’t rely on SSR for every request. ? Users see new sales in real time without needing to refresh the page. ? The backend load is reduced by avoiding unnecessary API calls.
This is a common issue in companies that work with real-time metrics, and Next.js provides an efficient solution.
Have you faced something similar at work? Let’s discuss in the comments! ??
#NextJS #ReactJS #WebDevelopment #Performance #RealtimeData #react #development
#dev #web
Lead Fullstack Engineer | Typescript Software Engineer | Nestjs | Nodejs | Reactjs | AWS | Rust
3 周Insightful
Full Stack Software Engineer | Front-end focused | ReactJS | React Native | NodeJS | AWS
3 周Valuable content!
Senior Frontend Developer | Mobile Developer | React | React Native | Flutter | Fastlane
3 周Great insights, Wagner! ?? This approach is super efficient—congrats on the awesome post! ????
Data Engineer | Python | SQL | PySpark | Databricks | Azure Certified: 5x
4 周Such a thoughtful post—thanks! ??