Partial Pre-rendering (PPR) is one of the standout features introduced in Next.js 15. This update brings significant enhancements to how pages are pre-rendered, giving developers more control over performance and user experience.
What is Partial Pre-rendering (PPR)?
PPR allows developers to selectively pre-render only parts of a page while leaving other parts to be rendered dynamically at runtime. This hybrid approach is particularly useful for applications with frequently changing content or user-specific data.
Instead of pre-rendering an entire page, PPR lets you decide which sections of the page to pre-render and which to fetch on demand. This optimizes build times and improves user experience by serving critical content faster.
Why is PPR Important?
Let’s say you’re building a travel planner app. Each user’s dashboard displays:
- A list of personalized travel recommendations (dynamic).
- A static section with travel tips and news.
- A map highlighting popular destinations (dynamic but cached).
Before PPR, pre-rendering dynamic content required complex workarounds, or developers had to rely on client-side rendering, which could hurt SEO and performance. With PPR, you can combine static and dynamic content seamlessly.
Workflow
How Does Partial Pre-rendering Work in Next.js 15?
Let’s walk through how PPR works, step by step, using our travel planner app as the example.
Step 1: Pre-render Static Content
Static content like travel tips and news can be pre-rendered during the build process. These sections don’t change frequently and can be served quickly to users.
export async function getStaticProps() {
const travelTips = await fetchTravelTips();
return {
props: {
travelTips,
},
};
}
export default function TravelTips({ travelTips }) {
return (
<section>
<h2>Travel Tips</h2>
<ul>
{travelTips.map((tip) => (
<li key={tip.id}>{tip.text}</li>
))}
</ul>
</section>
);
}
Step 2: Dynamic Content with PPR
Dynamic content, like personalized travel recommendations, is fetched during runtime but can still benefit from caching. With PPR, you can define these parts to be rendered dynamically.
export async function getServerSideProps(context) {
const userId = context.params.userId;
const recommendations = await fetchRecommendations(userId);
return {
props: {
recommendations,
},
};
}
export default function Recommendations({ recommendations }) {
return (
<section>
<h2>Your Travel Recommendations</h2>
<ul>
{recommendations.map((rec) => (
<li key={rec.id}>{rec.destination}</li>
))}
</ul>
</section>
);
}
Step 3: Combining Static and Dynamic Content
Now, we combine static and dynamic sections into one cohesive page. The static travel tips section is pre-rendered, while the recommendations are fetched on demand.
export default function Dashboard({ travelTips, recommendations }) {
return (
<main>
<TravelTips travelTips={travelTips} />
<Recommendations recommendations={recommendations} />
</main>
);
}
Step 4: Caching with ISR
To optimize the dynamic sections further, we can use Incremental Static Regeneration (ISR) for parts like the map of popular destinations. This ensures the map is updated periodically without rebuilding the entire app.
export async function getStaticProps() {
const popularDestinations = await fetchPopularDestinations();
return {
props: {
popularDestinations,
},
revalidate: 3600, // Revalidate every hour
};
}
export default function PopularDestinations({ popularDestinations }) {
return (
<section>
<h2>Popular Destinations</h2>
<ul>
{popularDestinations.map((dest) => (
<li key={dest.id}>{dest.name}</li>
))}
</ul>
</section>
);
}
Developer Impact of PPR in Next.js 15
Faster Builds: By pre-rendering only essential static content, build times are reduced significantly. Dynamic content is fetched as needed, making the process more efficient.
Improved User Experience: Critical content is delivered instantly, while dynamic sections load seamlessly in the background. This hybrid approach balances performance and interactivity.
SEO Benefits: Static sections are crawlable by search engines, improving your app’s discoverability. Dynamic content does not compromise your SEO efforts.
Conclusion
Partial Pre-rendering in Next.js 15 offers a flexible and efficient way to build modern applications. By combining static and dynamic rendering, PPR enhances performance, user experience, and SEO. Our travel planner app is just one example of how PPR can transform your development process.
As you explore this feature, think about the static and dynamic needs of your application. With PPR, Next.js 15 empowers you to deliver the best of both worlds!
Top comments (0)