dynamicParams = false for Cache Components & non-inheriting behavior
#84991
Replies: 1 comment 7 replies
-
|
Isn't this solved with import { locale } from 'next/root-params'
import { notFound } from 'next/navigation'
export async function getLocale(): Promise<string> {
const resolved = await locale()
if (!['en', 'de'].includes(resolved)) {
notFound()
}
return resolved;
}If the validation is expensive for whatever reason (maybe you have to go the database or a remote service), you can just cache it. To me this is far more flexible than rigid exports like I'm including an explainer about root-params for people that don't know about them, because they are still undocumented: What is next/root-params ??With a layout like the following:
You can just do: import { tenant, locale } from 'next/root-params'These are magically created functions that you need to await: const resolvedLocale = await locale()
const resolvedTenant = await tenant()and they are available anywhere in the app from that path downwards (no There are two prerequisites though:
Example: export async function generateStaticParams() {
return locales.map((locale) => ({ locale, tenant: 'knownTenantId' }));
}Warning If you do the above, and if your routes are fully or partially static, Next.js will literally generate separate ISR cache entries for each root param combination. This might balloon disk usage (or external cache if you're using an adapter). Scaling and cost planning can be a challenge for some kinds of apps. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
dynamicParams = falseis a useful feature for running validation of dynamic segments before rendering of the app starts. I think this pattern is especially useful when working with top-level dynamic segments like[locale], which are common for apps that support internationalization.However, when using the
cacheComponentsfeature, you currently see this error:Based on the comment from @sebmarkbage at #71927 (comment), it seems like a different alternative is planned. There was a follow-up discussion with @wyattjoh in #84738 which prompted the creation of this discussion.
A related gotcha of using
dynamicParams = falseis that it enforces inheriting this behavior for the route tree. For apps that use this at a top-level segment, this requires you to definegenerateStaticParamsfor all nested dynamic segments you might have—otherwise you'll run into 404s by default (e.g./[locale]/stories/[slug]→ any[slug]will by default be a 404 if./stories/layout.tsxdoesn't exportgenerateStaticParams—see also #84738).From my perspective, an ideal feature would:
Potential solutions:
dynamicParams = falseforcacheComponents(are there any API constraints here?)dynamicParams = falseto not inherit and ask developers to provide this for all levels where it should apply (breaking change, but with automated upgrade path)interceptor.tsthat runs before rendering, but allow it to not opt the corresponding pages into dynamic rendering (ref)Regarding the last item, it could look like this:
Alternatives today:
These are a few thoughts from my position, if the Next.js team could share more insights on this topic and potential constraints, I'd be really curious to discuss some ideas!
Beta Was this translation helpful? Give feedback.
All reactions