Next.js App Router SDK Best Practices
Developer preview
Best practices#
Use TypeScript. Give each parameter the type
ComponentParameter<T>, and give the slots a union of string literals.Make all parameters optional. Use
?for each parameter type. A parameter can beundefined, also when the Uniform component definition marks it as required.Keep client components small.
"use client"makes the component and all its children client components. Put it only on the smallest components that use the browser, for example a quirk button or a score display. Keep page layouts and content components as server components.Use edge mode for personalization above the fold. In lite mode, the visitor sees the default variant first, and then the change. Edge mode shows the correct variant on the first paint. Refer to Edge mode execution.
Do not read
searchParams, cookies or headers on published pages. These calls make the route dynamic. Then Next.js renders it on each request. The middleware already writes the request state into the code.List only the query strings that change the content. Each value of a listed query string makes a new cached page. Refer to Keep query strings in the route.
Use ISR. Start with an empty
generateStaticParams, so each page renders on its first visit. Prerender only the most important pages. Refer to Static generation (ISR).Make the static params agree with the middleware. Give
createUniformStaticParamsthe samerewrite,locale,edgeModeanddefaultConsentvalues as the middleware. Else the prebuilt pages are not used.Set
defaultConsentinuniform.server.config.ts. Your file replaces the default configuration. Without the option in your file, the default consent isfalse.Do not put content for search engines behind visibility rules. Components with visibility rules render only in the browser.
Use
flattenValuesfor assets and content blocks. UseflattenValuesfrom@uniformdev/canvasfor asset parameters. Refer to asset parameters.Return a fallback from
resolveComponent. Return a fallback component for unknown component types. Then a new type in Uniform does not break the page.Secure the webhook handler. Set
UNIFORM_PREVIEW_SECRET,UNIFORM_WEBHOOK_SECRET, or the two variables. Refer to Secure the webhook.