Next.js Performance Question: Optimizing Data Loading Strategy
Iβm working on a Next.js app and need advice on the best approach for handling data loading in the following scenario:
Current Flow
-
User creates an event (data saved to DB)
-
Response data is stored in the Recoil state
-
The user is redirected to the event detail page
The Challenge
I have two distinct ways users access the event detail page:
Internal Navigation
-
User comes from event creation
-
Event data already exists in the Recoil state
-
Should use this data for instant display
External Access
-
User comes from social media links
-
Need to fetch data using URL ID
-
Should be server-side rendered for SEO and performance
The Problem
Using a server component would be ideal for SEO and performance, but it forces an API call on every page load - even when we already have the data in the state.
What Iβm Looking For
A solution that:
-
Uses existing state data when available
-
Only calls the API when necessary
-
Maintains good SEO
-
Optimizes performance
Has anyone solved a similar use case? Whatβs the recommended pattern for this in Next.js?