Sanity client misconfigured in Next.js? apiVersion, useCdn and perspective fixes
If you inherited a Next.js site and the Sanity content is stale, leaking drafts, or oddly expensive to fetch, look at the Sanity client configuration before you touch anything else. Three settings, apiVersion, useCdn and perspective, cause a surprising share of the problems I see on inherited projects. They sit in one small file that nobody has opened since the first commit. Symptoms of a…
Sanity client misconfiguration is a common issue in Next.js projects, leading to stale content, duplicate entries, high API request counts, and inconsistent queries. The three main settings that cause problems are apiVersion, useCdn, and perspective. These settings are usually hardcoded in a small configuration file that nobody opens after the initial commit.
The most frequent issue is an outdated or missing apiVersion, which causes the query API to use old behaviour. Fixing this requires setting a fixed date string for the version, rather than relying on the current date.
Another common problem is incorrectly configuring useCdn. If set to false, every page build and cache miss will hit the live API, increasing latency and request counts. On the other hand, if set to true in draft or write-then-read code, the CDN may serve slightly older responses, causing stale previews and server actions.
The wrong perspective setting can also lead to issues. Using the wrong perspective (published, drafts, or raw) can result in unpublished content appearing on the live site, each document appearing twice, or the preview mode not working as expected.
Lastly, a single client trying to serve multiple roles (public pages, draft previews, and server-side writes) can cause problems. It is cleaner to have two clients with explicit settings for each role. By doing this, you can ensure that public pages only use the CDN and see published content, while draft mode fetches fresh reads and drafts layered over published content.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to