You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create a dedicated API for providing additional context during on-demand page generation
Improve data fetching efficiency for CMS-driven websites using dynamic routes
Reduce unnecessary database queries when generating pages at runtime
Enable early termination of invalid page generation requests
Maintain the clear separation between static and dynamic rendering models
Non-Goals
Replace or modify the existing dynamicParams functionality
Expose full request headers or cookies to static generation
Create a completely new rendering model
Solve all CMS integration challenges
Provide a full CMS framework or integration layer
Background
Currently, Next.js with dynamicParams: true allows generating pages on-demand when they don't exist statically. However, the only context available during this generation is the URL parameters, which creates significant inefficiencies for CMS-driven websites:
When building CMS-powered sites with complex content structures (multiple content types, hierarchical organization), developers must often:
Query multiple collections/tables to find the right content based only on URL slugs
Implement complex filtering logic across different content types
Generate pages that might not be needed if additional context was available earlier
Current alternatives:
Using middleware to rewrite URLs with additional parameters (cumbersome and limited)
Moving all logic to API routes (loses benefits of static generation)
Implementing complex caching layers outside of Next.js
Over-fetching data and filtering client-side (inefficient)
Real-world impact: For a site with 5 content types and 10,000 entries, the current approach might require checking multiple collections for each dynamic route, resulting in significant database load and slower page generation.
Proposal
We propose a new API called pageGenerationContext that would allow developers to provide and access additional context during on-demand page generation:
Implementation:
// In next.config.js
module.exports = {
experimental: {
pageGenerationContext: true
}
}
// In middleware.js or a similar configuration file
export function definePageGenerationContext(request) {
// This runs before attempting page generation
return {
contentType: getContentType(request),
contentId: getContentID(request),
generateToken: getGenerateToken(request);
};
}
// In page.js
export default async function Page({ params, generationContext }) {
// Verify the generation token before any operations
if (!verifyGenerateToken(generationContext.generateToken)) {
return notFound();
}
// Now we can fetch data much more efficiently
const { contentType, contentId, generateToken } = generationContext;
// Direct, efficient query
const content = await getContent(contentType, contentId);
if (!content) return notFound();
// Render page
}
Technical considerations:
The context object should be serializable
Size limits should be enforced (e.g., 8KB max)
The API should maintain the security boundary between static and dynamic rendering
The context should be available during both initial generation and revalidation
Contribution interest:
I'm interested in contributing to the discussion and potentially helping with implementation if the Next.js team finds this proposal valuable. I could help with:
Refining the API design
Creating proof-of-concept implementations
Writing documentation
Testing with real-world CMS scenarios
This feature would significantly improve the developer experience and performance for CMS-driven Next.js applications while maintaining the framework's architectural principles.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
Non-Goals
dynamicParams
functionalityBackground
Currently, Next.js with
dynamicParams: true
allows generating pages on-demand when they don't exist statically. However, the only context available during this generation is the URL parameters, which creates significant inefficiencies for CMS-driven websites:When building CMS-powered sites with complex content structures (multiple content types, hierarchical organization), developers must often:
Current alternatives:
Real-world impact: For a site with 5 content types and 10,000 entries, the current approach might require checking multiple collections for each dynamic route, resulting in significant database load and slower page generation.
Proposal
We propose a new API called
pageGenerationContext
that would allow developers to provide and access additional context during on-demand page generation:Implementation:
Technical considerations:
Contribution interest:
I'm interested in contributing to the discussion and potentially helping with implementation if the Next.js team finds this proposal valuable. I could help with:
This feature would significantly improve the developer experience and performance for CMS-driven Next.js applications while maintaining the framework's architectural principles.
Beta Was this translation helpful? Give feedback.
All reactions