<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nayan Kyada</title>
    <description>The latest articles on DEV Community by Nayan Kyada (@nayankyada).</description>
    <link>https://dev.to/nayankyada</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2638501%2Fb0cc4a93-db40-4087-9ff8-b4c2debac8a1.jpg</url>
      <title>DEV Community: Nayan Kyada</title>
      <link>https://dev.to/nayankyada</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nayankyada"/>
    <language>en</language>
    <item>
      <title>Deleted a document in Sanity? How to recover content and prevent it</title>
      <dc:creator>Nayan Kyada</dc:creator>
      <pubDate>Fri, 27 Nov 2026 14:05:43 +0000</pubDate>
      <link>https://dev.to/nayankyada/deleted-a-document-in-sanity-how-to-recover-content-and-prevent-it-3558</link>
      <guid>https://dev.to/nayankyada/deleted-a-document-in-sanity-how-to-recover-content-and-prevent-it-3558</guid>
      <description>&lt;p&gt;Someone on your team deleted a document in Sanity, the page is now a 404 or a blank hole in your site, and nobody is sure whether it can come back. Stay calm and do not make more changes to the dataset until you have checked three places: document history, any backups, and your own exports. This post walks through that order, then the guardrails that stop it happening again.&lt;/p&gt;

&lt;h2&gt;
  
  
  First: stop the bleeding
&lt;/h2&gt;

&lt;p&gt;Before you try to restore anything, work out what the deletion actually broke. A deleted document in Sanity does not only vanish from the Studio. Anything that references it is now pointing at nothing, and on a Next.js site that usually means one of three symptoms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The page itself returns a 404 because the slug no longer resolves.&lt;/li&gt;
&lt;li&gt;Other pages that referenced it (authors, categories, related posts) render with missing data or throw.&lt;/li&gt;
&lt;li&gt;Navigation or sitemap entries disappear after the next revalidation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If pages are throwing errors, the fastest temporary fix is on the frontend: make the GROQ projection tolerate a missing reference, so the rest of the site stays up while you restore. I covered the failure mode in &lt;a href="https://nayankyada.com/blog/sanity-broken-references-crashing-nextjs-pages-find-and-fix-them" rel="noopener noreferrer"&gt;Sanity broken references crashing Next.js pages&lt;/a&gt;. Do that first if production is visibly broken, then recover the content.&lt;/p&gt;

&lt;p&gt;Also ask the editor exactly what they deleted: a draft, a published document, or both. Deleting in the Studio removes the published version and its draft together, which surprises people who thought they were only discarding changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where a deleted document in Sanity can come from
&lt;/h2&gt;

&lt;p&gt;Check these in order. Each one is quicker than the next.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Document history in the Studio.&lt;/strong&gt; Sanity keeps a revision history for documents, and how far back it goes depends on your plan. Confirm the retention window in your project settings rather than assuming. If the deletion was recent, this is the first place to look, and if you can reach the document's history you can often restore a previous revision.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sanity-managed backups.&lt;/strong&gt; Some plans include dataset backups. Check the project's settings in the Sanity management console to see whether any exist and how old they are.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Your own exports.&lt;/strong&gt; If anyone ever ran a dataset export (for a migration, a staging copy, or a handover), that file is a snapshot you can pull the document out of.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A staging dataset.&lt;/strong&gt; If you copied production to a staging dataset at some point, the document may still live there.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If none of those exist, the content is likely gone from Sanity. Your remaining options are the live site's cached HTML, a search engine cache, or the Wayback Machine, which can at least give you the text and structure to retype.&lt;/p&gt;

&lt;h2&gt;
  
  
  Restoring from an export
&lt;/h2&gt;

&lt;p&gt;If you have an export file, do not import it over production. Import it into a scratch dataset, find the document, and copy only that record across. This avoids overwriting everyone else's work since the export was taken.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Create a scratch dataset and load the old export into it&lt;/span&gt;
npx sanity dataset create restore-scratch
npx sanity dataset import backup-2026-10-01.tar.gz restore-scratch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then query the scratch dataset for the missing document by its slug or title. You can run a quick GROQ query from the Vision tool pointed at that dataset:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*[_type == "post" &amp;amp;&amp;amp; slug.current == "the-deleted-slug"][0]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you have the JSON, create it in production with the same &lt;code&gt;_id&lt;/code&gt; so existing references resolve again. Keeping the original &lt;code&gt;_id&lt;/code&gt; is the important part. A new ID means every document that referenced the old one is still broken. A short script with the Sanity client does it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// scripts/restore-document.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;createClient&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@sanity/client'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;restored&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'./restored-post.json'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SANITY_PROJECT_ID&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'production'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;apiVersion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'2026-01-01'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SANITY_WRITE_TOKEN&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;useCdn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// createIfNotExists keeps us from clobbering anything that was recreated meanwhile&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createIfNotExists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;restored&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Restored'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Images and files are stored as separate asset documents. If the old export included assets, the references usually still resolve as long as the asset documents still exist in production. If the assets were also deleted, you will need to re-upload them.&lt;/p&gt;

&lt;p&gt;After restoring, trigger a revalidation (publish nothing, just fire your webhook or revalidate the tag manually) so the Next.js site picks the document up again. If published changes are not appearing, see &lt;a href="https://nayankyada.com/blog/sanity-editors-publishing-changes-that-dont-show-on-the-live-site" rel="noopener noreferrer"&gt;Sanity editors publishing changes that don't show on the live site&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stop it happening again
&lt;/h2&gt;

&lt;p&gt;Recovery is a stressful afternoon. Prevention takes an hour. Here is what I put in place on sites where content is business-critical.&lt;/p&gt;

&lt;h3&gt;
  
  
  Remove the delete action where it is risky
&lt;/h3&gt;

&lt;p&gt;Editors rarely need to hard-delete a landing page, a service page, or a settings document. Remove the action for those types in the Studio config:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// sanity.config.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'sanity'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;protectedTypes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'page'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'siteSettings'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'navigation'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ...projectId, dataset, plugins, schema&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;actions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;schemaType&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class="n"&gt;protectedTypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;schemaType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;action&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s1"&gt;'delete'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a UI guard, not a security boundary. Someone with the right permissions can still delete through the API, so combine it with roles. I wrote up that side in &lt;a href="https://nayankyada.com/blog/how-i-set-up-sanity-document-level-permissions-with-role-based-access-control" rel="noopener noreferrer"&gt;Sanity roles and document-level permissions&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Give editors a safe alternative
&lt;/h3&gt;

&lt;p&gt;People delete because they have no other way to say "take this down". An unpublish and archive workflow solves that, and the content stays recoverable. The approach is in &lt;a href="https://nayankyada.com/blog/how-i-build-an-unpublish-and-archive-workflow-in-sanity" rel="noopener noreferrer"&gt;how to archive documents in Sanity&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Keep your own scheduled export
&lt;/h3&gt;

&lt;p&gt;Do not depend on whatever your plan happens to retain. A scheduled export to storage you control is cheap insurance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Run nightly from CI (GitHub Actions, cron, etc.)&lt;/span&gt;
npx sanity dataset &lt;span class="kd"&gt;export&lt;/span&gt; production &lt;span class="s2"&gt;"backups/production-$(date +%F).tar.gz"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Store the files somewhere that is not the same account as the project, keep a sensible number of them, and test a restore into a scratch dataset once. An untested backup is a guess.&lt;/p&gt;

&lt;h3&gt;
  
  
  Put it in the handover
&lt;/h3&gt;

&lt;p&gt;Backups, token ownership, and who holds admin access should be written down, not living in one person's head. That is part of what I list in the &lt;a href="https://nayankyada.com/blog/sanity-cms-project-handover-checklist-what-you-should-receive" rel="noopener noreferrer"&gt;Sanity project handover checklist&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to ask a developer you bring in
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What exactly is recoverable today, and how do you know?&lt;/li&gt;
&lt;li&gt;Will you restore with the original &lt;code&gt;_id&lt;/code&gt; so references stay intact?&lt;/li&gt;
&lt;li&gt;Will you restore into a scratch dataset first, not production?&lt;/li&gt;
&lt;li&gt;What will be in place afterwards: delete guards, roles, scheduled exports?&lt;/li&gt;
&lt;li&gt;Can you show me a restore test, not just describe one?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the answers are vague, the next deletion will be just as painful.&lt;/p&gt;

&lt;h2&gt;
  
  
  If you need a hand
&lt;/h2&gt;

&lt;p&gt;Recovering lost content, tightening permissions, and setting up exports is work I do as part of a Performance &amp;amp; Sanity audit, which starts from $800. Details are on the &lt;a href="https://dev.to/services#packages"&gt;services page&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>sanitycms</category>
      <category>sanitystudio</category>
      <category>headlesscms</category>
    </item>
    <item>
      <title>Duplicate without user-selected canonical: Next.js + Sanity fix</title>
      <dc:creator>Nayan Kyada</dc:creator>
      <pubDate>Thu, 26 Nov 2026 14:07:31 +0000</pubDate>
      <link>https://dev.to/nayankyada/duplicate-without-user-selected-canonical-nextjs-sanity-fix-1n9d</link>
      <guid>https://dev.to/nayankyada/duplicate-without-user-selected-canonical-nextjs-sanity-fix-1n9d</guid>
      <description>&lt;p&gt;If Search Console is showing "Duplicate without user-selected canonical" against pages on your Next.js + Sanity site, Google has found the same content at more than one URL and nobody told it which one to keep. Those pages are being dropped from the index, usually the ones you most want ranking. The cause is almost never Sanity itself. It is how routes, domains and metadata are wired in the frontend.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the status actually means
&lt;/h2&gt;

&lt;p&gt;Google crawls a URL, compares it to others it knows, and clusters near-identical pages. If your pages carry no canonical, or a canonical that points somewhere odd, Google chooses a winner on its own. Sometimes it picks the right one. Often it picks a vercel.app address, a URL with a query string, or the www version you stopped using two years ago.&lt;/p&gt;

&lt;p&gt;There is a sibling status, "Duplicate, Google chose different canonical than user". That one means you did set a canonical and Google disagreed. The fixes below cover both, but that second status points harder at causes 3 and 4.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it costs to ignore
&lt;/h2&gt;

&lt;p&gt;Duplicates split link signals and crawl budget across URLs, and the version Google picks may be one with no internal links pointing at it. The practical result is landing pages that stay unindexed or rank under the wrong address, and analytics that blur together. It also tends to hide other problems, so it is worth ruling out before you chase anything else. If your pages are missing from the index for other reasons, see my notes on &lt;a href="https://nayankyada.com/blog/nextjs-pages-not-indexed-by-google-causes-and-the-fix-path" rel="noopener noreferrer"&gt;Next.js pages not indexed by Google&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 1: no canonical, or a relative one with no metadataBase
&lt;/h2&gt;

&lt;p&gt;The most common finding on inherited sites: the metadata API is used for titles and descriptions but nobody set a canonical. The second most common: a canonical was set as a relative path, but metadataBase is missing from the root layout, so it resolves against the wrong origin.&lt;/p&gt;

&lt;p&gt;Fix it in two places. Set metadataBase once in the root layout using your production URL from an environment variable. Then set a self-referencing canonical per page from the Sanity slug:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/blog/[slug]/page.tsx&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Metadata&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"next"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;defineQuery&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"next-sanity"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"@/sanity/lib/client"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;POST_SEO_QUERY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;defineQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="sb"&gt;`*[_type == "post" &amp;amp;&amp;amp; slug.current == $slug][0]{ title, "slug": slug.current, seoDescription }`&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;generateMetadata&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;slug&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Metadata&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;slug&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;POST_SEO_QUERY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;slug&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;seoDescription&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;alternates&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;canonical&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sb"&gt;`/blog/${post.slug}`&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The root layout needs a metadataBase such as new URL(process.env.NEXT_PUBLIC_SITE_URL), with that variable set to your real production domain. Check the variable is set in the Production environment on Vercel, not only locally.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 2: preview and vercel.app URLs are crawlable
&lt;/h2&gt;

&lt;p&gt;If Google can reach your deployment URL or a staging domain, it will treat it as a full duplicate of production. Look at the URL Inspection tool: if the Google-selected canonical is a different hostname, this is your problem. Block non-production environments with authentication or a noindex header rather than relying on robots.txt alone. I cover that in detail in &lt;a href="https://nayankyada.com/blog/staging-site-indexed-by-google-fix-a-nextjs-sanity-setup" rel="noopener noreferrer"&gt;staging site indexed by Google&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 3: host and trailing slash variants all return 200
&lt;/h2&gt;

&lt;p&gt;http, https, www and non-www, with and without a trailing slash: if more than one of these answers with a 200 instead of redirecting, you have duplicates by definition. Most hosts handle http to https, but the www decision is usually left open. Pick one and redirect the other permanently:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// next.config.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;NextConfig&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"next"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;nextConfig&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;NextConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;redirects&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"/:path*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;has&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"host"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"www.example.com"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"https://example.com/:path*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;permanent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="n"&gt;nextConfig&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next.js removes trailing slashes by default and redirects to the slash-less version, unless someone set trailingSlash to true in the config. Check that setting matches the form used in your canonicals, your sitemap and your internal links. A mismatch between them is a classic source of the "Google chose different canonical" variant.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 4: the same Sanity document is reachable at several routes
&lt;/h2&gt;

&lt;p&gt;This one is specific to CMS builds. A post lives at /blog/my-post, but it also resolves at /news/my-post, or /my-post, because a catch-all route queries by slug without checking the document type. Or a category change leaves old and new paths both live.&lt;/p&gt;

&lt;p&gt;The fix is to decide one canonical route per document type, make every other route return a 404 or a permanent redirect, and make sure the GROQ filter in each route includes the type. If slugs have changed over time, the redirect handling in &lt;a href="https://nayankyada.com/blog/sanity-slug-changes-causing-404s-how-to-manage-redirects-in-nextjs" rel="noopener noreferrer"&gt;Sanity slug changes causing 404s&lt;/a&gt; applies directly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 5: query strings, pagination and filters
&lt;/h2&gt;

&lt;p&gt;Listing pages with sort and filter parameters generate endless URL variants showing the same content. Canonical the filtered versions to the clean listing URL, and do not put parameterised URLs in the sitemap. For paginated lists, each page should canonical to itself, not to page one, otherwise Google may ignore the deeper pages and the posts only linked from them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 6: locale pages without self-canonicals
&lt;/h2&gt;

&lt;p&gt;On multilingual sites, a translated page with no canonical, or one pointing at the default language, gets folded into the original. Each locale version needs its own self-referencing canonical plus hreflang alternates. The traffic side of this is covered in &lt;a href="https://nayankyada.com/blog/sanity-multilingual-site-losing-traffic-hreflang-and-translation-fixes" rel="noopener noreferrer"&gt;Sanity multilingual site losing traffic&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to verify the fix
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Open the live page, view source, and confirm exactly one canonical link tag exists with an absolute production URL.&lt;/li&gt;
&lt;li&gt;Request the www, http and trailing-slash variants with curl -I and confirm each returns a 301 or 308 to the single preferred URL.&lt;/li&gt;
&lt;li&gt;Use URL Inspection on a few affected pages and compare the user-declared canonical with the Google-selected one. After a recrawl they should match.&lt;/li&gt;
&lt;li&gt;Confirm the sitemap only lists canonical URLs. If it lists stale or duplicate entries, see &lt;a href="https://nayankyada.com/blog/nextjs-sitemap-wrong-on-a-sanity-site-missing-pages-and-stale-urls" rel="noopener noreferrer"&gt;Next.js sitemap wrong on a Sanity site&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;In Search Console, open the status report and click Validate Fix. Expect the count to fall over weeks, not days.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What to ask a developer
&lt;/h2&gt;

&lt;p&gt;Ask which single URL form is canonical and where that decision is enforced: redirects, metadata and sitemap. Ask whether any Sanity document type can resolve at more than one route. Ask how non-production environments are kept out of the index. If the answer to any of these is a shrug, the problem will come back after the next release.&lt;/p&gt;

&lt;h2&gt;
  
  
  Need this fixed
&lt;/h2&gt;

&lt;p&gt;This is work I do: a canonical and indexing review is part of my &lt;a href="https://dev.to/services#packages"&gt;Performance and Sanity audit, from $800&lt;/a&gt;, and fixes can roll into the same engagement.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>sanitycms</category>
      <category>seo</category>
      <category>approuter</category>
    </item>
    <item>
      <title>Sanity GROQ query slow or timing out? Causes and the fix path</title>
      <dc:creator>Nayan Kyada</dc:creator>
      <pubDate>Wed, 25 Nov 2026 14:44:27 +0000</pubDate>
      <link>https://dev.to/nayankyada/sanity-groq-query-slow-or-timing-out-causes-and-the-fix-path-c18</link>
      <guid>https://dev.to/nayankyada/sanity-groq-query-slow-or-timing-out-causes-and-the-fix-path-c18</guid>
      <description>&lt;p&gt;If your Sanity GROQ query is slow, or fails intermittently on a large listing page, the content is rarely the problem. The query is. It worked with 40 documents during the build and now does far too much work on a dataset with thousands. This post covers the symptoms, what they cost if you leave them, and the fix path I follow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Symptoms of a slow GROQ query
&lt;/h2&gt;

&lt;p&gt;These are the signs I see on sites where queries have outgrown the original schema:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A listing or archive page that was fast at launch now takes seconds to render, and gets worse with every batch of new content.&lt;/li&gt;
&lt;li&gt;Builds slow down as the number of pages grows, even though no page changed.&lt;/li&gt;
&lt;li&gt;Occasional errors on one route only, usually a listing, search, or category page, while single-document pages are fine.&lt;/li&gt;
&lt;li&gt;Studio list views and Vision runs for the same query feel sluggish too, which tells you it is the query and not Next.js.&lt;/li&gt;
&lt;li&gt;Response bodies in the megabytes for a page that shows twelve cards.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If only the first page of a listing is slow, suspect the projection. If page 40 is slower than page 1, suspect offset pagination. If everything is slow, suspect the way the schema forces joins.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it costs to ignore
&lt;/h2&gt;

&lt;p&gt;A slow query hits you in three places. First, time to first byte on dynamic routes, which drags down LCP. Second, build time, because every statically generated page pays the same query cost. Third, your API usage: slow, heavy queries tend to be retried, refetched, and duplicated across components. I have covered that last effect in &lt;a href="https://nayankyada.com/blog/sanity-api-bill-spiking-why-requests-explode-on-nextjs-sites" rel="noopener noreferrer"&gt;why Sanity API requests explode on Next.js sites&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The quieter cost is editorial. Once a listing query starts timing out, someone adds a workaround such as a hardcoded list or a cache that never invalidates, and the content team stops trusting the site.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 1: the projection pulls whole documents
&lt;/h2&gt;

&lt;p&gt;The most common mistake is a listing that expands everything because the same fragment is reused on every page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Broken: listing query that expands references and returns full bodies
*[_type == "post"] | order(publishedAt desc) {
  ...,
  author-&amp;gt;,
  categories[]-&amp;gt;,
  related[]-&amp;gt;{ ..., author-&amp;gt; },
  body
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A card needs a title, slug, date, image, and maybe an author name. Returning &lt;code&gt;body&lt;/code&gt; as Portable Text for every post means shipping the whole article text just to render a headline. The fix is a purpose-built projection per view, which I describe in &lt;a href="https://nayankyada.com/blog/how-i-handle-conditional-groq-projections-to-cut-query-payload-by-60" rel="noopener noreferrer"&gt;GROQ conditional projections&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 2: nested joins multiply the work
&lt;/h2&gt;

&lt;p&gt;Every &lt;code&gt;-&amp;gt;&lt;/code&gt; dereference is a lookup. One level on twelve documents is fine. Three levels on a hundred documents, with arrays of references inside each, becomes thousands of lookups per request. The pattern usually comes from a schema where relationships were modelled for editor convenience rather than for the pages that read them. If that sounds familiar, &lt;a href="https://nayankyada.com/blog/how-i-model-sanity-references-to-avoid-n1-queries-in-nextjs-rsc" rel="noopener noreferrer"&gt;modelling references to avoid N+1 queries&lt;/a&gt; and &lt;a href="https://nayankyada.com/blog/how-i-structure-sanity-schemas-to-avoid-query-waterfalls-in-nextjs" rel="noopener noreferrer"&gt;structuring schemas to avoid query waterfalls&lt;/a&gt; go deeper.&lt;/p&gt;

&lt;p&gt;The practical rule: dereference only the one or two fields the view shows, and never dereference inside a reference you already dereferenced on a listing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 3: offset pagination on a large set
&lt;/h2&gt;

&lt;p&gt;Slicing with a large start index forces the query to order and skip everything before it. The deeper the page, the slower it gets, and archive pages at the bottom of a blog are the first to time out. Cursor pagination keeps the cost flat.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// lib/sanity/queries/posts.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;defineQuery&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"next-sanity"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;POSTS_PAGE_QUERY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;defineQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`
  *[
    _type == "post" &amp;amp;&amp;amp;
    defined(slug.current) &amp;amp;&amp;amp;
    (
      publishedAt &amp;lt; $lastPublishedAt ||
      (publishedAt == $lastPublishedAt &amp;amp;&amp;amp; _id &amp;gt; $lastId)
    )
  ] | order(publishedAt desc, _id asc) [0...12] {
    _id,
    title,
    "slug": slug.current,
    publishedAt,
    "imageUrl": mainImage.asset-&amp;gt;url,
    "authorName": author-&amp;gt;name
  }
`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/blog/page.tsx (fetch helper)&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"@/lib/sanity/client"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="no"&gt;POSTS_PAGE_QUERY&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"@/lib/sanity/queries/posts"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getPostsPage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;lastPublishedAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"9999-12-31T00:00:00Z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;lastId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="no"&gt;POSTS_PAGE_QUERY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;lastPublishedAt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lastId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"post"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first page passes a far-future date, and every following page passes the last item's &lt;code&gt;publishedAt&lt;/code&gt; and &lt;code&gt;_id&lt;/code&gt;. The &lt;code&gt;_id&lt;/code&gt; tiebreaker matters: without it, posts sharing a timestamp get skipped or repeated.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 4: filters and text matching that scan everything
&lt;/h2&gt;

&lt;p&gt;A filter that starts with &lt;code&gt;_type ==&lt;/code&gt; is the cheapest thing you can give the query. Filters that rely on &lt;code&gt;match&lt;/code&gt; across several fields, or on computed values, or that call &lt;code&gt;references()&lt;/code&gt; across the whole dataset, make the query evaluate far more than it needs to. On-site search built this way is the typical casualty. If search is a core feature, send it to a search service or use embeddings rather than stretching GROQ. I have written up both approaches in &lt;a href="https://nayankyada.com/blog/how-i-sync-sanity-cms-to-algolia-search-in-a-nextjs-app" rel="noopener noreferrer"&gt;syncing Sanity to Algolia&lt;/a&gt; and &lt;a href="https://nayankyada.com/blog/how-i-add-semantic-search-to-a-nextjs-site-using-sanity-embeddings" rel="noopener noreferrer"&gt;semantic search with Sanity embeddings&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 5: the same heavy query runs repeatedly
&lt;/h2&gt;

&lt;p&gt;Even a reasonable query hurts if layout, page, and metadata each run it separately, or if nothing is tagged for caching. Wrap shared reads in one function, tag them, and let revalidation on publish do the invalidating instead of fetching fresh on every request. The webhook side is in &lt;a href="https://nayankyada.com/blog/sanity-webhook-revalidation-batching-tags-nextjs-isr" rel="noopener noreferrer"&gt;Sanity webhook revalidation with tag batching&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to verify the fix
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Paste the original query into the Vision tool in Studio and note the time and response size.&lt;/li&gt;
&lt;li&gt;Change one thing at a time: trim the projection, then remove nested dereferences, then switch to cursor pagination. Note the numbers after each change.&lt;/li&gt;
&lt;li&gt;Log the duration around &lt;code&gt;client.fetch&lt;/code&gt; in the route and compare production-like data, not a ten-document dev dataset.&lt;/li&gt;
&lt;li&gt;Run a build and compare total time. A listing that was queried once per page should now be a handful of small requests.&lt;/li&gt;
&lt;li&gt;Check deep pages, not just page one.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If the payload is still large after this, the schema is the issue, and that is a restructure rather than a query tweak. I cover how to do that safely in &lt;a href="https://nayankyada.com/blog/restructure-a-sanity-schema-on-a-live-site-without-breaking-pages" rel="noopener noreferrer"&gt;restructuring a Sanity schema on a live site&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to ask a developer
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Which specific queries are slow, and what are their timings before and after?&lt;/li&gt;
&lt;li&gt;Will you give each view its own projection, or keep one shared fragment?&lt;/li&gt;
&lt;li&gt;How will you paginate, and what happens to page 50?&lt;/li&gt;
&lt;li&gt;Do you need schema changes, and if so, how will live content be migrated without downtime?&lt;/li&gt;
&lt;li&gt;How will queries be cached and revalidated after a publish?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A developer who answers with measurements is worth more than one who proposes a new CMS. For broader diagnosis, see &lt;a href="https://nayankyada.com/blog/sanity-website-slow-how-to-find-the-cause-and-what-a-fix-costs" rel="noopener noreferrer"&gt;how to find the cause of a slow Sanity website and what a fix costs&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Work I do
&lt;/h2&gt;

&lt;p&gt;Query profiling and cleanup is part of the Performance and Sanity audit, which starts from $800. Details are on the &lt;a href="https://dev.to/services#packages"&gt;services page&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>sanitycms</category>
      <category>groq</category>
      <category>nextjs</category>
      <category>performance</category>
    </item>
    <item>
      <title>Open Graph image not showing on a Next.js + Sanity site: rescue</title>
      <dc:creator>Nayan Kyada</dc:creator>
      <pubDate>Tue, 24 Nov 2026 15:03:45 +0000</pubDate>
      <link>https://dev.to/nayankyada/open-graph-image-not-showing-on-a-nextjs-sanity-site-rescue-dlb</link>
      <guid>https://dev.to/nayankyada/open-graph-image-not-showing-on-a-nextjs-sanity-site-rescue-dlb</guid>
      <description>&lt;p&gt;If your Open Graph image is not showing on a Next.js + Sanity site, links shared on LinkedIn, Slack, WhatsApp or X look bare: a grey box, a favicon, or the wrong picture from an old campaign. It is almost never a Sanity bug. It is a metadata wiring problem that nobody noticed because nobody shares their own links to test them.&lt;/p&gt;

&lt;p&gt;This post covers the symptoms, what leaving it broken costs, the fix path, and what to ask a developer who says it is a quick job.&lt;/p&gt;

&lt;h2&gt;
  
  
  Symptoms that point to a broken Open Graph setup
&lt;/h2&gt;

&lt;p&gt;These are the patterns I see on inherited sites:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every page shows the same image, usually the homepage one, or none at all.&lt;/li&gt;
&lt;li&gt;The preview works on staging but not on the live domain.&lt;/li&gt;
&lt;li&gt;The title and description appear, but the image slot is empty.&lt;/li&gt;
&lt;li&gt;A fixed image shows in the debugger tool but old links still look wrong.&lt;/li&gt;
&lt;li&gt;Blog posts that editors gave a hero image still share without one.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The quickest check takes thirty seconds. Open the page, view source, and search for &lt;code&gt;og:image&lt;/code&gt;. If there is no tag, the problem is in your code. If there is a tag, copy the URL into a browser. If it is relative, returns a 404, or downloads a 12 MB file, you have found it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it costs to ignore
&lt;/h2&gt;

&lt;p&gt;A link with no image gets noticeably less attention in a feed than one with a clean card. For a marketing site that depends on social posts, newsletters, and people pasting links into chats, that is a quiet tax on every campaign. It also makes the site look unmaintained to a prospect who pastes your URL into Slack before a call.&lt;/p&gt;

&lt;p&gt;It is not a direct ranking factor, so I would not call it an emergency. But it is cheap to fix and visible to every non-technical stakeholder, which is why it tends to be the thing that finally triggers a call.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 1: no metadataBase, so URLs are relative
&lt;/h2&gt;

&lt;p&gt;Open Graph crawlers need absolute URLs. In the App Router, if you set images without a base URL, Next.js has to guess, and on some deployments it resolves to a preview or localhost-style host. Set &lt;code&gt;metadataBase&lt;/code&gt; once in the root layout, from an environment variable that is the production domain.&lt;/p&gt;

&lt;p&gt;This is also the setup that causes the related mess where a staging host leaks into tags. If that sounds familiar, read &lt;a href="https://nayankyada.com/blog/staging-site-indexed-by-google-fix-a-nextjs-sanity-setup" rel="noopener noreferrer"&gt;staging site indexed by Google&lt;/a&gt;, because the same environment variable bug often causes both.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 2: generateMetadata never reads the Sanity image
&lt;/h2&gt;

&lt;p&gt;On many inherited builds, metadata is hardcoded in the layout, or the page-level &lt;code&gt;generateMetadata&lt;/code&gt; fetches the title and description but skips the image field. Editors fill in a social image in the Studio and nothing happens.&lt;/p&gt;

&lt;p&gt;The fix is to project the image in the same GROQ query you already use for the page, then build a 1200 by 630 URL from it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/blog/[slug]/page.tsx&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Metadata&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"next"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"@/sanity/lib/client"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;urlFor&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"@/sanity/lib/image"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sb"&gt;`*[_type == "post" &amp;amp;&amp;amp; slug.current == $slug][0]{
  title,
  excerpt,
  "social": coalesce(socialImage, mainImage)
}`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;generateMetadata&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;slug&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Metadata&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;slug&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;slug&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;social&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;asset&lt;/span&gt;
    &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nf"&gt;urlFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;social&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;width&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;height&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;630&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"crop"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"jpg"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"/og-default.jpg"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;excerpt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;openGraph&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;excerpt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;images&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;630&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;twitter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"summary_large_image"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;images&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;coalesce&lt;/code&gt; gives editors a dedicated social image but falls back to the hero. The default image path keeps pages without any image from sharing blank. Because &lt;code&gt;params&lt;/code&gt; is a Promise in current Next.js, it has to be awaited, and older code that reads it directly will fail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 3: the image is wrong for a crawler
&lt;/h2&gt;

&lt;p&gt;Even with a valid URL, the card can fail. Common reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The file is an original upload, several megabytes, and the crawler gives up. Always request a sized, compressed version through the image pipeline as above.&lt;/li&gt;
&lt;li&gt;The format is something the platform does not render. JPEG or PNG is the safe choice for social cards, so ask for it explicitly rather than relying on automatic format negotiation, which can hand a crawler AVIF or WebP.&lt;/li&gt;
&lt;li&gt;The crop cuts off the subject. If your Studio uses hotspot data, the Sanity URL builder respects it when you crop. If it does not, see &lt;a href="https://nayankyada.com/blog/sanity-image-url-hotspot-not-working-four-causes-and-fixes" rel="noopener noreferrer"&gt;Sanity image-url hotspot not working&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Cause 4: the page is behind something the crawler cannot pass
&lt;/h2&gt;

&lt;p&gt;Password-protected staging, Vercel deployment protection, or a bot-blocking firewall rule will block the scrape. The preview then fails even though the tags are perfect. Test with the platform debugger against the exact production URL. If the debugger reports a fetch error rather than a missing tag, look at protection and firewall settings, not code.&lt;/p&gt;

&lt;p&gt;The same family of issues, where a crawler sees something different than a browser does, is covered in &lt;a href="https://nayankyada.com/blog/nextjs-pages-not-indexed-by-google-causes-and-the-fix-path" rel="noopener noreferrer"&gt;Next.js pages not indexed by Google&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 5: stale caches on the platform side
&lt;/h2&gt;

&lt;p&gt;Social platforms cache the first scrape of a URL, sometimes for days. After deploying the fix, re-scrape the page using the platform's own debugger tools. If a platform will not refresh, share the link with a harmless query string once to force a new fetch. Do not conclude the fix failed before you have done this.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to verify the fix
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;View source on a live page and confirm &lt;code&gt;og:image&lt;/code&gt; is an absolute production URL.&lt;/li&gt;
&lt;li&gt;Open that image URL directly and check it loads fast and is the right crop.&lt;/li&gt;
&lt;li&gt;Run three different page types through the debugger tools: homepage, a blog post, and a page with no image set.&lt;/li&gt;
&lt;li&gt;Paste a link into Slack and LinkedIn as a last visual check.&lt;/li&gt;
&lt;li&gt;Check that the image alt and descriptive fields exist on the Sanity side too. Missing alt text is a separate but common companion problem, covered in &lt;a href="https://nayankyada.com/blog/sanity-images-missing-alt-text-fix-an-accessibility-audit-fail" rel="noopener noreferrer"&gt;Sanity images missing alt text&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What to ask a developer
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Is &lt;code&gt;metadataBase&lt;/code&gt; set from an environment variable, and which value does production use?&lt;/li&gt;
&lt;li&gt;Which Sanity field feeds the social image, and what is the fallback when it is empty?&lt;/li&gt;
&lt;li&gt;Does the share image get a fixed 1200 by 630 crop in a web-safe format, or is the original file served?&lt;/li&gt;
&lt;li&gt;Can you show me the debugger result for three page types, not just the homepage?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A good answer is specific and fast. A vague answer about plugins or waiting for caches suggests they have not looked at the tags.&lt;/p&gt;

&lt;h2&gt;
  
  
  When this is part of a bigger problem
&lt;/h2&gt;

&lt;p&gt;Broken share cards are usually one symptom of metadata that was never treated as a real feature: missing canonicals, wrong sitemap entries, no structured data. If you suspect that, this is work I do. The &lt;a href="https://dev.to/services#packages"&gt;performance and Sanity audit, from $800&lt;/a&gt;, covers metadata, rendering, and Core Web Vitals in one pass, with a prioritised fix list you can hand to any developer.&lt;/p&gt;

</description>
      <category>sanitycms</category>
      <category>nextjs</category>
      <category>seo</category>
      <category>approuter</category>
    </item>
    <item>
      <title>Third-party scripts slowing your Next.js site: find and fix them</title>
      <dc:creator>Nayan Kyada</dc:creator>
      <pubDate>Mon, 23 Nov 2026 15:05:30 +0000</pubDate>
      <link>https://dev.to/nayankyada/third-party-scripts-slowing-your-nextjs-site-find-and-fix-them-1096</link>
      <guid>https://dev.to/nayankyada/third-party-scripts-slowing-your-nextjs-site-find-and-fix-them-1096</guid>
      <description>&lt;p&gt;Third-party scripts slowing your Next.js site is the most common performance problem I see after the code itself has been cleaned up. The pages are server-rendered, the Sanity images are optimised, and the scores are still poor because a tag manager is loading eleven things nobody remembers approving. This post covers how to find the offenders, what ignoring them costs, and the fix path.&lt;/p&gt;

&lt;h2&gt;
  
  
  Symptoms of third-party scripts slowing your Next.js site
&lt;/h2&gt;

&lt;p&gt;The pattern is consistent. Lighthouse on a clean local build looks fine, but field data in Search Console or Vercel Analytics is worse. The usual signs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;INP is poor even though the React code is small. Clicks on menus and filters feel delayed by a beat.&lt;/li&gt;
&lt;li&gt;LCP is fine in development and bad in production, because production has the marketing tags and development does not.&lt;/li&gt;
&lt;li&gt;Total Blocking Time is high in Lighthouse, and the trace shows long tasks attributed to domains you do not own.&lt;/li&gt;
&lt;li&gt;The site gets slower every quarter without a single code deploy, because someone in marketing keeps adding tags in the tag manager.&lt;/li&gt;
&lt;li&gt;A cookie banner, chat bubble, or A/B testing tool causes visible layout shift or a flash of changed content.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That last point matters. Because tags are managed outside the repository, the slowdown does not show up in a pull request. Nobody reviewed it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it costs to ignore it
&lt;/h2&gt;

&lt;p&gt;Google uses real-user Core Web Vitals as a ranking signal, and slow interaction hurts conversion before it hurts rankings. If you want the business case, I wrote about &lt;a href="https://nayankyada.com/blog/marketing-website-performance-what-fast-pages-are-actually-worth" rel="noopener noreferrer"&gt;what fast pages are actually worth&lt;/a&gt;. The less obvious cost is that developers get blamed. A team spends weeks rewriting components to shave kilobytes while a single chat widget holds the main thread for hundreds of milliseconds. That is wasted budget.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Inventory every script that actually loads
&lt;/h2&gt;

&lt;p&gt;Start with what loads, not what you think loads. Open the live site in an incognito window, open DevTools, and look at the Network tab filtered to JS. Sort by size and group by domain. Then open the Performance panel, record a page load plus a few real interactions, and use the bottom-up view grouped by domain.&lt;/p&gt;

&lt;p&gt;You will usually find three categories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scripts someone still needs: analytics, consent management, payment.&lt;/li&gt;
&lt;li&gt;Scripts nobody remembers: an old heatmap trial, a retired ad pixel, a survey tool from a campaign two years ago.&lt;/li&gt;
&lt;li&gt;Scripts that load other scripts: the tag manager container pulling in a dozen more.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Get the marketing owner in a room and decide, tag by tag, keep or remove. Deleting is the cheapest optimisation there is, and in my experience it usually accounts for the biggest single improvement.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Load the keepers correctly
&lt;/h2&gt;

&lt;p&gt;In the App Router, scripts belong in the layout, loaded through the framework rather than pasted into the head as raw tags. The official &lt;code&gt;@next/third-parties&lt;/code&gt; package ships components for Google Tag Manager and Google Analytics that handle loading order for you.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/layout.tsx&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;GoogleTagManager&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"@next/third-parties/google"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Script&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"next/script"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;RootLayout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ReactNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt; &lt;span class="n"&gt;lang&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"en"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;GoogleTagManager&lt;/span&gt; &lt;span class="n"&gt;gtmId&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"GTM-XXXXXXX"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Non-critical script: wait until the browser is idle */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Script&lt;/span&gt;
        &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"https://example.com/heatmap.js"&lt;/span&gt;
        &lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"lazyOnload"&lt;/span&gt;
      &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;strategy&lt;/code&gt; prop is the lever. &lt;code&gt;afterInteractive&lt;/code&gt; is the default for most tags and loads after hydration starts. &lt;code&gt;lazyOnload&lt;/code&gt; waits until the browser is idle, which suits heatmaps, surveys and anything that does not need to run before the first interaction. Reserve &lt;code&gt;beforeInteractive&lt;/code&gt; for things that genuinely must run first, such as a consent check, and use it rarely because it blocks.&lt;/p&gt;

&lt;p&gt;One caveat: moving a script to &lt;code&gt;lazyOnload&lt;/code&gt; can break tracking if the tag needs to fire on page view. Test with the vendor's debug tool, not by assuming.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Load widgets on interaction, not on page load
&lt;/h2&gt;

&lt;p&gt;Chat bubbles are the worst offenders. They ship a large bundle so a button can sit in the corner that perhaps two percent of visitors click. Use a facade: render a lightweight button, and load the real widget only when someone asks for it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// components/chat-launcher.tsx&lt;/span&gt;
&lt;span class="s2"&gt;"use client"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"react"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;ChatLauncher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;loadWidget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"script"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"https://example.com/chat-widget.js"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;
      &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"button"&lt;/span&gt;
      &lt;span class="n"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;loadWidget&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="n"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"fixed bottom-4 right-4 rounded-full bg-black px-4 py-3 text-white"&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;loading&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;"Loading chat..."&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Chat with us"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The button has a fixed size and position, so there is no layout shift when the real widget replaces it. The same idea works for embedded video, maps and social feeds. The &lt;code&gt;@next/third-parties&lt;/code&gt; package also provides facade-style embeds for YouTube and Google Maps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Audit what the tag manager injects
&lt;/h2&gt;

&lt;p&gt;A tag manager container is a script that loads other scripts, and it is also where the problem regrows. Open the container and look for tags firing on all pages that only matter on one. Restrict them with triggers. Remove tags with no recent activity. Agree a rule that any new tag needs an owner and a reason, because otherwise the container becomes a dumping ground.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Verify with field data, not a single Lighthouse run
&lt;/h2&gt;

&lt;p&gt;Lighthouse is a lab test and varies run to run. After changes, check that total blocking time drops in the trace, then watch real-user INP and LCP over the following weeks in Search Console or your analytics. Field data lags by roughly a month in Search Console, so do not panic early. If INP is still poor after the scripts are tamed, the problem is probably your own code, and &lt;a href="https://nayankyada.com/blog/inp-for-react-apps-profiling-and-fixing-long-tasks" rel="noopener noreferrer"&gt;profiling INP in React apps&lt;/a&gt; is the next step. For the overall order of operations, see &lt;a href="https://nayankyada.com/blog/fix-core-web-vitals-on-a-nextjs-sanity-site-fix-order-and-timeline" rel="noopener noreferrer"&gt;fixing Core Web Vitals on a Next.js and Sanity site&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to ask a developer
&lt;/h2&gt;

&lt;p&gt;If you hire help, these questions separate people who profile from people who guess:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Can you show me which domains consume the most main-thread time on my site today?&lt;/li&gt;
&lt;li&gt;Which tags can be removed, and who signs off?&lt;/li&gt;
&lt;li&gt;How will you stop the tag manager from regrowing the problem?&lt;/li&gt;
&lt;li&gt;What will you measure afterwards, and over what period?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A good answer includes a before and after trace and a list of tags with a decision beside each. If you want to know what a paid review should contain, I broke that down in &lt;a href="https://nayankyada.com/blog/nextjs-website-audit-what-a-paid-one-covers-and-how-to-judge-it" rel="noopener noreferrer"&gt;what a Next.js website audit covers&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is work I do. My Performance &amp;amp; Sanity audit starts from $800 and includes a third-party script inventory with a prioritised fix list; details are on the &lt;a href="https://dev.to/services#packages"&gt;services and packages page&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>performance</category>
      <category>corewebvitals</category>
      <category>react</category>
    </item>
    <item>
      <title>Draft content showing on your live Next.js site: Sanity fix</title>
      <dc:creator>Nayan Kyada</dc:creator>
      <pubDate>Sun, 22 Nov 2026 12:45:07 +0000</pubDate>
      <link>https://dev.to/nayankyada/draft-content-showing-on-your-live-nextjs-site-sanity-fix-13n1</link>
      <guid>https://dev.to/nayankyada/draft-content-showing-on-your-live-nextjs-site-sanity-fix-13n1</guid>
      <description>&lt;p&gt;Draft content showing on a live Next.js site is one of the more alarming things an owner can discover. A half-written page, an old price, or an embargoed announcement appears on production while the editor swears they never clicked Publish. On Sanity sites this is almost always a configuration problem in the front end, not a Studio bug, and it is fixable in a day or two.&lt;/p&gt;

&lt;h2&gt;
  
  
  Symptoms that point to leaking drafts
&lt;/h2&gt;

&lt;p&gt;The pattern varies, but these are the ones I see most often on inherited Sanity + Next.js projects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A page appears at its URL before anyone published it, usually with placeholder copy.&lt;/li&gt;
&lt;li&gt;Listing pages (blog index, team, case studies) show items that are not live, or show the same item twice.&lt;/li&gt;
&lt;li&gt;Edits appear on the live site the moment an editor types, with no Publish click.&lt;/li&gt;
&lt;li&gt;The site shows draft content for some visitors and not others, depending on which server instance or cache entry they hit.&lt;/li&gt;
&lt;li&gt;Search engines have indexed a URL for a page the team believes was never released.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The duplicate-item symptom is the giveaway. In Sanity, an edited published document has a second copy with an ID starting with &lt;code&gt;drafts.&lt;/code&gt;. If your query sees both, you get two cards.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it costs to ignore
&lt;/h2&gt;

&lt;p&gt;The obvious risk is embarrassment: unreleased pricing, legal copy that has not been approved, or a staff member's profile before they have started. The quieter risk is SEO. Draft pages that get crawled become indexed URLs, and when they are later unpublished they turn into 404s or soft 404s. I covered the cleanup side in &lt;a href="https://nayankyada.com/blog/nextjs-soft-404-errors-on-a-sanity-site-causes-and-fixes" rel="noopener noreferrer"&gt;Next.js soft 404 errors on a Sanity site&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There is also a trust cost internally. Once editors learn that saving can put content live, they stop using drafts at all, or they write in a separate Google Doc. That defeats the point of having a CMS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 1: the client perspective is not set
&lt;/h2&gt;

&lt;p&gt;Sanity's API has the concept of a perspective. The published perspective returns only published documents. The raw perspective returns everything, drafts included. On older API versions the default behaviour can be raw, which is exactly what you do not want on a public site. Many inherited projects pin an old &lt;code&gt;apiVersion&lt;/code&gt; from the day the project was created and never touched it.&lt;/p&gt;

&lt;p&gt;The fix is to be explicit instead of relying on defaults:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/sanity/lib/client.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;createClient&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'next-sanity'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;NEXT_PUBLIC_SANITY_PROJECT_ID&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;NEXT_PUBLIC_SANITY_DATASET&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;apiVersion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'2025-02-19'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;useCdn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;perspective&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'published'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This client has no token and no way to see drafts. That is the point. Production pages should only ever use this one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 2: GROQ queries that assume only published documents exist
&lt;/h2&gt;

&lt;p&gt;Even with a correct perspective, I often find queries written for a raw client, or a second client created in a utility file that nobody remembers. A defensive filter is cheap insurance on listing queries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*[_type == "post" &amp;amp;&amp;amp; defined(slug.current) &amp;amp;&amp;amp; !(_id in path("drafts.**"))]
  | order(publishedAt desc) {
    _id,
    title,
    "slug": slug.current
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I would not rely on this filter alone, because the perspective is the real control. But it makes the query safe if someone later swaps clients, and it makes the intent obvious to the next developer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 3: a token on the public client
&lt;/h2&gt;

&lt;p&gt;If the production client has a read token, it can be configured to see drafts, and a lot of tutorials do exactly that. A token should exist only in a separate server-side client used when Draft Mode is on. If the token ever reaches browser code, that is a bigger problem, covered in &lt;a href="https://nayankyada.com/blog/sanity-api-token-exposed-in-a-nextjs-site-how-to-check-and-fix-it" rel="noopener noreferrer"&gt;Sanity API token exposed in a Next.js site&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The clean pattern is one fetch helper that switches on Draft Mode:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/sanity/lib/fetch.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;draftMode&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'next/headers'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'./client'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sanityFetch&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;unknown&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;isEnabled&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;draftMode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isEnabled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;withConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;useCdn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SANITY_API_READ_TOKEN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;perspective&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'drafts'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'no-store'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'sanity'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The token is only read on the server, only when the visitor holds a Draft Mode cookie, and that branch opts out of caching.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 4: a cached preview response served to everyone
&lt;/h2&gt;

&lt;p&gt;This one is the hardest to spot because it is intermittent. If a draft fetch ends up in the Next.js data cache or at the CDN, the next anonymous visitor receives it. It happens when preview logic is bolted on with a query-string flag or a custom header rather than real Draft Mode, or when the draft branch forgets to disable caching. The fix is the helper above: never cache the draft branch, and never decide preview by a URL parameter that a CDN might treat as the same key.&lt;/p&gt;

&lt;p&gt;If editors also report the opposite problem, publishing without seeing changes, read &lt;a href="https://nayankyada.com/blog/sanity-editors-publishing-changes-that-dont-show-on-the-live-site" rel="noopener noreferrer"&gt;Sanity editors publishing changes that don't show on the live site&lt;/a&gt;, since the two share a caching root.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to verify the fix
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;In the Studio, edit a published page and do not publish. Load the live URL in a private window. It must show the old copy.&lt;/li&gt;
&lt;li&gt;Create a brand-new document and leave it as a draft. Its URL must return a 404 and it must be missing from listings and the sitemap.&lt;/li&gt;
&lt;li&gt;Turn on Draft Mode through your preview route. The draft should now appear, and a second private window should still not see it.&lt;/li&gt;
&lt;li&gt;Search the codebase for every &lt;code&gt;createClient&lt;/code&gt; call and every use of a token. There should be one public client and one token-bearing path.&lt;/li&gt;
&lt;li&gt;Check Google Search Console for URLs that should never have been public, and request removal if needed.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What to ask a developer
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Which perspective does each client in the codebase use, and why?&lt;/li&gt;
&lt;li&gt;Where does the read token live, and can it reach the browser?&lt;/li&gt;
&lt;li&gt;How is Draft Mode implemented, and is the draft branch uncached?&lt;/li&gt;
&lt;li&gt;Do listing queries, sitemap generation, and static params all go through the same published-only client?&lt;/li&gt;
&lt;li&gt;Can they show you the drafts test above passing on staging before it ships?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A vague answer to the first question usually means the project has more than one leak. For a deeper walk-through of preview itself, see &lt;a href="https://nayankyada.com/blog/sanity-draft-mode-preview-nextjs-app-router-vercel-edge-config" rel="noopener noreferrer"&gt;Sanity Draft Mode preview with the App Router and Edge Config&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting it fixed
&lt;/h2&gt;

&lt;p&gt;This is work I do. A draft-leak review normally fits inside my &lt;a href="https://dev.to/services#packages"&gt;Performance &amp;amp; Sanity audit&lt;/a&gt;, which starts from $800 and covers clients, tokens, caching, and indexing in one pass, with the fixes scoped afterwards.&lt;/p&gt;

</description>
      <category>sanitycms</category>
      <category>nextjs</category>
      <category>groq</category>
      <category>draftmode</category>
    </item>
    <item>
      <title>Locked out of your Sanity project? Recover access from an ex-developer</title>
      <dc:creator>Nayan Kyada</dc:creator>
      <pubDate>Sat, 21 Nov 2026 12:52:45 +0000</pubDate>
      <link>https://dev.to/nayankyada/locked-out-of-your-sanity-project-recover-access-from-an-ex-developer-3cm5</link>
      <guid>https://dev.to/nayankyada/locked-out-of-your-sanity-project-recover-access-from-an-ex-developer-3cm5</guid>
      <description>&lt;p&gt;If you are locked out of your Sanity project, the usual story is simple: a freelancer or agency created it under their own login, the site went live, and now nobody on your team can open the management console. The content still renders on the website, so it feels fine, until you need a new editor, a new domain, or a bill gets paid late. This post covers how to confirm what you actually own, how to get access back, and how to make sure it cannot happen again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Symptoms that you don't own your Sanity project
&lt;/h2&gt;

&lt;p&gt;The warning signs show up in small, boring moments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You open the Studio URL and can log in, but you cannot invite a new editor.&lt;/li&gt;
&lt;li&gt;The management console at sanity.io/manage shows nothing for your company email.&lt;/li&gt;
&lt;li&gt;Invoices or plan emails go to a personal address you don't recognize.&lt;/li&gt;
&lt;li&gt;The website deploy uses an API token nobody can identify or regenerate.&lt;/li&gt;
&lt;li&gt;Adding your new domain to the allowed CORS origins is impossible because only the old developer can edit them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Being able to edit content is not the same as owning the project. Editors can write and publish without ever holding the keys. The keys are admin roles on the project, ideally under an organization your company controls.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it costs to ignore it
&lt;/h2&gt;

&lt;p&gt;The risk is rarely dramatic on day one. It is slow and expensive on the day something changes. A developer's account can be closed, a card can expire, or a person can simply stop answering messages. When the plan lapses or the project is deleted, the dataset goes with it, and the live site is only as safe as the last export someone made.&lt;/p&gt;

&lt;p&gt;There is also a security side. Tokens created by a past developer are still valid until someone revokes them. If one of those leaked into a public repo, you want to know. The post on &lt;a href="https://nayankyada.com/blog/sanity-api-token-exposed-in-a-nextjs-site-how-to-check-and-fix-it" rel="noopener noreferrer"&gt;an exposed Sanity API token in a Next.js site&lt;/a&gt; covers how to check for that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: find out who actually owns what
&lt;/h2&gt;

&lt;p&gt;Before you email anyone, gather facts. You need four answers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The project ID. It is visible in the site's code (the Sanity client config or the public environment variables) and in the Studio configuration. It is not a secret.&lt;/li&gt;
&lt;li&gt;The dataset names in use, usually production, sometimes staging.&lt;/li&gt;
&lt;li&gt;Which organization, if any, the project belongs to.&lt;/li&gt;
&lt;li&gt;Who has admin roles right now.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The project ID is the key that lets you ask Sanity support about the right project. If your repository is accessible, search it for the client config. If not, the browser's network tab on the live site will show requests to an API host that begins with the project ID.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: ask for admin, not just access
&lt;/h2&gt;

&lt;p&gt;If the previous developer is still reachable, the cleanest fix takes ten minutes. Ask them to do these in the management console:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Invite your company email as an administrator on the project.&lt;/li&gt;
&lt;li&gt;Move the project into an organization your company owns, if the console offers it for your plan, or create one and add yourself as owner.&lt;/li&gt;
&lt;li&gt;Update the billing contact to your finance address.&lt;/li&gt;
&lt;li&gt;Confirm in writing that they no longer need access, then remove themselves, or ask you to remove them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Be specific about administrator. A common mistake is accepting an editor invite, feeling reassured, and discovering six months later that you can't create tokens or manage CORS. If you need a refresher on what each role can do, the post on &lt;a href="https://nayankyada.com/blog/how-i-set-up-sanity-document-level-permissions-with-role-based-access-control" rel="noopener noreferrer"&gt;Sanity roles and document-level permissions&lt;/a&gt; explains the model.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: if the developer is gone
&lt;/h2&gt;

&lt;p&gt;When nobody answers, work through these in order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check whether anyone at your company already has an admin role under a different email. Old agency contacts and former employees are the usual surprises.&lt;/li&gt;
&lt;li&gt;Look for transactional emails from Sanity in shared inboxes, such as billing receipts or invitations. They tell you which address owns the account.&lt;/li&gt;
&lt;li&gt;Contact Sanity support with the project ID, your company details, and evidence that you own the website and domain. Support decides case by case, so expect questions and don't expect an instant answer.&lt;/li&gt;
&lt;li&gt;In parallel, protect the content. Even a partial export beats nothing.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The parallel part matters. Don't wait for support before you start planning a fallback. If you can get any admin-level login, even temporary, take a full export straight away.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Run from any machine where you are logged in with project access&lt;/span&gt;
npx sanity login
npx sanity dataset list
npx sanity dataset &lt;span class="kd"&gt;export&lt;/span&gt; production ./backup-production.tar.gz

&lt;span class="c"&gt;# Check which origins the project currently allows&lt;/span&gt;
npx sanity cors list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The export includes documents and, by default, the assets. Store the archive somewhere your company controls, not only on one person's laptop.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: reset every credential
&lt;/h2&gt;

&lt;p&gt;Once you hold admin access, assume every credential the old developer touched is compromised, even if only by neglect:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create new read and write API tokens, scoped to what each integration needs.&lt;/li&gt;
&lt;li&gt;Replace them in your hosting environment variables and webhook secrets, then redeploy.&lt;/li&gt;
&lt;li&gt;Delete the old tokens only after the new deploy is confirmed working, so the site doesn't go down mid-change.&lt;/li&gt;
&lt;li&gt;Review CORS origins and remove domains you don't recognize.&lt;/li&gt;
&lt;li&gt;Review webhooks and remove any that point at endpoints you don't control.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A quick sanity check on the website side is to confirm that only server code reads the write token:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// lib/sanity/server-client.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;createClient&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'next-sanity'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;serverClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;NEXT_PUBLIC_SANITY_PROJECT_ID&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;NEXT_PUBLIC_SANITY_DATASET&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;apiVersion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'2025-01-01'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;useCdn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="c1"&gt;// Never prefix this variable with NEXT_PUBLIC_&lt;/span&gt;
  &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SANITY_API_WRITE_TOKEN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the write token variable has a public prefix anywhere in the repo, treat that as an incident and rotate it immediately.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: make it impossible to repeat
&lt;/h2&gt;

&lt;p&gt;After recovery, put the following in place so the project survives the next staff change:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The project lives under an organization owned by your company, with at least two admins.&lt;/li&gt;
&lt;li&gt;Billing goes to a shared finance address, not a person.&lt;/li&gt;
&lt;li&gt;Tokens are documented: name, purpose, where it is stored, who can rotate it.&lt;/li&gt;
&lt;li&gt;A scheduled export exists, even a monthly manual one, stored outside the project.&lt;/li&gt;
&lt;li&gt;Hosting and repository access are under company accounts too.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the same list I'd expect in a proper handover, and the &lt;a href="https://nayankyada.com/blog/sanity-cms-project-handover-checklist-what-you-should-receive" rel="noopener noreferrer"&gt;Sanity project handover checklist&lt;/a&gt; goes through it item by item. If the codebase itself is also in doubt, &lt;a href="https://nayankyada.com/blog/take-over-an-existing-nextjs-project-after-your-developer-disappears" rel="noopener noreferrer"&gt;taking over a Next.js project after your developer disappears&lt;/a&gt; covers the repository and hosting side.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to ask a developer you bring in
&lt;/h2&gt;

&lt;p&gt;Whoever you hire to clean this up should be able to answer these without hedging:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Which organization will the project sit under when you finish, and who are its admins?&lt;/li&gt;
&lt;li&gt;How will you prove the export is complete and restorable into a fresh dataset?&lt;/li&gt;
&lt;li&gt;Which tokens exist today, and which will you rotate?&lt;/li&gt;
&lt;li&gt;Will you work in a staging dataset first, rather than editing production?&lt;/li&gt;
&lt;li&gt;What do I get in writing at the end?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A developer who wants to keep the project under their own account "for convenience" is repeating the exact problem you are fixing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting help
&lt;/h2&gt;

&lt;p&gt;This is work I do: recovering or consolidating Sanity ownership, rotating credentials, taking a verified backup, and checking the site still builds and publishes cleanly afterward. It usually fits inside my &lt;a href="https://dev.to/services#packages"&gt;Performance and Sanity audit from $800&lt;/a&gt;, and if you need a developer on a longer engagement, the details are on the &lt;a href="https://dev.to/hire-sanity-developer"&gt;hire a Sanity developer&lt;/a&gt; page.&lt;/p&gt;

</description>
      <category>sanitycms</category>
      <category>sanitystudio</category>
      <category>headlesscms</category>
      <category>hiring</category>
    </item>
    <item>
      <title>Sanity images missing alt text: fix an accessibility audit fail</title>
      <dc:creator>Nayan Kyada</dc:creator>
      <pubDate>Fri, 20 Nov 2026 13:21:03 +0000</pubDate>
      <link>https://dev.to/nayankyada/sanity-images-missing-alt-text-fix-an-accessibility-audit-fail-3p6m</link>
      <guid>https://dev.to/nayankyada/sanity-images-missing-alt-text-fix-an-accessibility-audit-fail-3p6m</guid>
      <description>&lt;p&gt;An accessibility audit, a lawyer's letter, or a Lighthouse report has told you your Sanity-powered site has images with missing alt text. It is usually a bigger problem than a handful of pages, because the cause sits in the schema and the front-end component, not in what editors typed. This post covers how to find every gap, how to stop new ones, and what to ask a developer to do.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Sanity images end up with no alt text
&lt;/h2&gt;

&lt;p&gt;A plain Sanity image field stores the asset reference and the hotspot and crop data. It does not store a description. If the original developer never added an alt field to the schema, editors had nowhere to write one.&lt;/p&gt;

&lt;p&gt;There are three common situations I find on inherited sites:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The schema has no alt field at all, so every image renders with no description.&lt;/li&gt;
&lt;li&gt;The schema has an alt field, but it is optional, so editors skip it under deadline.&lt;/li&gt;
&lt;li&gt;The schema and content are fine, but the GROQ projection never fetches alt, or the React component never passes it to the image element.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The third one is the most annoying, because editors did the work and it never reached the page. Check it first, since it costs the least to fix.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it costs to ignore
&lt;/h2&gt;

&lt;p&gt;Missing alt text is not only a compliance concern. Screen reader users get nothing from those images. Image search has less context for ranking them. Several markets now have accessibility regulations that apply to commercial sites, and a vague audit finding turns into a formal request faster than most owners expect. I am not a lawyer, so treat this as a reason to look, not legal advice.&lt;/p&gt;

&lt;p&gt;The cost also grows with content. Every new article published into a broken schema adds more images to fix by hand later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: measure the gap with GROQ
&lt;/h2&gt;

&lt;p&gt;Open the Vision tool in Studio and count the damage per document type. For a top-level image field:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*[_type == "post" &amp;amp;&amp;amp; defined(mainImage.asset) &amp;amp;&amp;amp; (!defined(mainImage.alt) || mainImage.alt == "")]{
  _id,
  title,
  "slug": slug.current
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Repeat for each document type that has an image. Then do the harder part: images inside Portable Text bodies and page builder blocks. Those live in arrays, so you need a query per block type, or a small script that walks documents and reports any image object with an asset and no alt. A script that exports to CSV is also the right deliverable to hand to editors, because they can fix pages in priority order.&lt;/p&gt;

&lt;p&gt;Be honest about the result. If thousands of images lack alt text, nobody is going to hand-write all of it this week. Prioritise by traffic: homepage, top landing pages, and the most visited articles first.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: enforce alt in the schema
&lt;/h2&gt;

&lt;p&gt;Making the field required for every image is tempting but wrong, because decorative images should have no description. The better pattern is a required alt field with an explicit decorative escape hatch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// schemaTypes/objects/accessibleImage.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;defineField&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;defineType&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'sanity'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;accessibleImage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;defineType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'accessibleImage'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Image'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'image'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;hotspot&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nf"&gt;defineField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'decorative'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Decorative image (no description needed)'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'boolean'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;defineField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'alt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Alt text'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'string'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Describe what the image shows, as you would to someone on the phone.'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;hidden&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;decorative&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;validation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Rule&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="nc"&gt;Rule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;custom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;asset&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;decorative&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;
          &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;asset&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;decorative&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'Add alt text, or mark the image as decorative.'&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then replace the raw &lt;code&gt;image&lt;/code&gt; type with &lt;code&gt;accessibleImage&lt;/code&gt; across documents and blocks. Existing content will show validation warnings in Studio, which doubles as a to-do list for editors. If you want a refresher on how rules like this behave, my &lt;a href="https://nayankyada.com/blog/how-i-write-custom-validation-rules-in-sanity-schemas" rel="noopener noreferrer"&gt;custom validation rules guide&lt;/a&gt; covers the details. Changing a field type on a live dataset has its own risks, so read &lt;a href="https://nayankyada.com/blog/restructure-a-sanity-schema-on-a-live-site-without-breaking-pages" rel="noopener noreferrer"&gt;restructuring a Sanity schema on a live site&lt;/a&gt; before touching production.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: make sure alt reaches the page
&lt;/h2&gt;

&lt;p&gt;Fetch both fields in the query, then render them correctly in the component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// components/SanityImage.tsx&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Image&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'next/image'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;urlFor&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@/sanity/lib/image'&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Props&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;asset&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;_ref&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;alt&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;decorative&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;
  &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;
  &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
  &lt;span class="n"&gt;priority&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;SanityImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;priority&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;asset&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;alt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;decorative&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;alt&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Image&lt;/span&gt;
      &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;urlFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;width&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;height&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="n"&gt;alt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;alt&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="n"&gt;priority&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;priority&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your projection must include &lt;code&gt;alt&lt;/code&gt; and &lt;code&gt;decorative&lt;/code&gt; on every image, including those nested in blocks. This is the step teams forget when they add a field later. If you use typed queries, &lt;a href="https://nayankyada.com/blog/how-i-set-up-sanity-typegen-for-fully-typed-groq-queries-in-typescript" rel="noopener noreferrer"&gt;Sanity TypeGen&lt;/a&gt; will flag the missing property at build time instead of in an audit.&lt;/p&gt;

&lt;p&gt;One more place to check is Portable Text. Inline images in article bodies are rendered by a custom component, and that component often ignores alt. My &lt;a href="https://nayankyada.com/blog/how-i-build-sanity-portable-text-custom-components-in-nextjs" rel="noopener noreferrer"&gt;Portable Text components walkthrough&lt;/a&gt; shows where to wire it in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: verify
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Re-run the GROQ gap queries and confirm the count falls as editors work through the list.&lt;/li&gt;
&lt;li&gt;Crawl the live site with an accessibility checker, such as Lighthouse or axe, and filter for image alt findings.&lt;/li&gt;
&lt;li&gt;View source on a few key pages and confirm the alt attribute is present, with an empty value only on genuinely decorative images.&lt;/li&gt;
&lt;li&gt;Publish a test document without alt and confirm Studio blocks or warns on it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Automated tools only catch missing alt, not bad alt. "image123.jpg" passes the check and helps nobody, so spot-check quality on your top pages by hand.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to ask a developer
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Will you add the alt field and decorative toggle to every image type, including blocks and Portable Text?&lt;/li&gt;
&lt;li&gt;How will you deal with the existing images: a CSV export for editors, a bulk script, or both?&lt;/li&gt;
&lt;li&gt;Do you update the GROQ projections and components, or only the schema?&lt;/li&gt;
&lt;li&gt;How do you stop it from regressing, for example with validation and typed queries?&lt;/li&gt;
&lt;li&gt;Can you scope this alongside the wider findings, since alt text rarely comes alone?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If editors resist the extra field, the problem may be wider than alt text. I covered that in &lt;a href="https://nayankyada.com/blog/sanity-studio-editor-experience-why-editors-avoid-it-and-the-fix" rel="noopener noreferrer"&gt;why editors avoid Sanity Studio&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is work I do. A &lt;a href="https://dev.to/services#packages"&gt;Performance &amp;amp; Sanity audit&lt;/a&gt;, from $800, covers schema, queries, and front-end output, and alt text coverage is part of what I check.&lt;/p&gt;

</description>
      <category>sanitycms</category>
      <category>sanitystudio</category>
      <category>groq</category>
      <category>nextjs</category>
    </item>
    <item>
      <title>Embedded Sanity Studio in Next.js causing problems? Fixes</title>
      <dc:creator>Nayan Kyada</dc:creator>
      <pubDate>Thu, 19 Nov 2026 13:40:09 +0000</pubDate>
      <link>https://dev.to/nayankyada/embedded-sanity-studio-in-nextjs-causing-problems-fixes-2f63</link>
      <guid>https://dev.to/nayankyada/embedded-sanity-studio-in-nextjs-causing-problems-fixes-2f63</guid>
      <description>&lt;p&gt;If your embedded Sanity Studio in Next.js is making builds slower, showing your site header on top of the editing UI, or somehow ending up in your public JavaScript, the cause is almost always structural rather than a bug in either tool. The default setup from the getting-started guides works, but it leaves the Studio sharing a layout, a build and a import graph with your marketing site. This post covers the symptoms I see most, what they cost if ignored, and how to separate the two cleanly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Symptoms of an embedded Studio gone wrong
&lt;/h2&gt;

&lt;p&gt;These are the complaints that usually reach me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Studio at &lt;code&gt;/studio&lt;/code&gt; has your site navbar, cookie banner or chat widget floating over it.&lt;/li&gt;
&lt;li&gt;Fonts and global CSS bleed in, so Studio buttons and inputs look slightly off.&lt;/li&gt;
&lt;li&gt;Builds got noticeably longer after the Studio was added, or a schema tweak means redeploying the whole site.&lt;/li&gt;
&lt;li&gt;Public pages ship more JavaScript than they should, and the bundle analyser shows Sanity Studio packages in a chunk it has no business being in.&lt;/li&gt;
&lt;li&gt;Visiting &lt;code&gt;/studio&lt;/code&gt; returns a redirect loop, a 404, or a login screen from your own auth layer.&lt;/li&gt;
&lt;li&gt;Google has crawled &lt;code&gt;/studio&lt;/code&gt; or it appears in your sitemap.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each of these has a different cause. Work through them in order.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 1: the Studio inherits your root layout
&lt;/h2&gt;

&lt;p&gt;In the App Router, a route at &lt;code&gt;app/studio/[[...tool]]/page.tsx&lt;/code&gt; is wrapped by &lt;code&gt;app/layout.tsx&lt;/code&gt;. Whatever you put there, such as header, footer, analytics scripts, Tailwind resets or a font provider, wraps the Studio too. Tailwind's base styles in particular can restyle Studio elements in subtle ways.&lt;/p&gt;

&lt;p&gt;The fix is route groups with separate root layouts. Move your public pages into &lt;code&gt;app/(site)/&lt;/code&gt; and the Studio into &lt;code&gt;app/(studio)/&lt;/code&gt;, delete the top-level &lt;code&gt;app/layout.tsx&lt;/code&gt;, and give each group its own.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/(studio)/studio/[[...tool]]/page.tsx&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;NextStudio&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'next-sanity/studio'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'../../../../sanity.config'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;dynamic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'force-static'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;viewport&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'next-sanity/studio'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;StudioPage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;NextStudio&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/(studio)/layout.tsx&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;StudioLayout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ReactNode&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt; &lt;span class="n"&gt;lang&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"en"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="n"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note the Studio layout does not import &lt;code&gt;globals.css&lt;/code&gt;. That is the point. Your site layout keeps its own &lt;code&gt;html&lt;/code&gt; and &lt;code&gt;body&lt;/code&gt; with fonts, analytics and styles. Check the relative import path against your own folder depth; the config import is the part people get wrong after moving files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 2: the Studio config leaks into public bundles
&lt;/h2&gt;

&lt;p&gt;This is the expensive one. &lt;code&gt;sanity.config.ts&lt;/code&gt; imports &lt;code&gt;sanity&lt;/code&gt;, plugins and every schema type. If any frontend file imports from it, even just to read &lt;code&gt;projectId&lt;/code&gt;, the bundler may pull Studio code into a route that real visitors load.&lt;/p&gt;

&lt;p&gt;The broken pattern looks harmless:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// lib/sanity.client.ts  (broken)&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;createClient&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'next-sanity'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'../sanity.config'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;apiVersion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'2025-01-01'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;useCdn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The fix is a small env module that both sides read from, with nothing Studio-related in it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// sanity/env.ts&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;projectId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;NEXT_PUBLIC_SANITY_PROJECT_ID&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;dataset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;NEXT_PUBLIC_SANITY_DATASET&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;apiVersion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'2025-01-01'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then &lt;code&gt;sanity.config.ts&lt;/code&gt; and your client both import from &lt;code&gt;sanity/env.ts&lt;/code&gt;, and nothing in the frontend imports &lt;code&gt;sanity.config.ts&lt;/code&gt;. Schema type files should be the same: import them only from the config, never from page components. If you need a TypeScript type, use generated types rather than importing a schema definition. I covered the bundle side of this in the &lt;a href="https://nayankyada.com/blog/how-i-cut-nextjs-bundle-size-by-analysing-and-reducing-chunk-bloat" rel="noopener noreferrer"&gt;Next.js bundle size analysis post&lt;/a&gt;; run the analyser before and after and confirm no &lt;code&gt;sanity&lt;/code&gt; package appears in public route chunks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 3: your proxy or auth blocks the Studio
&lt;/h2&gt;

&lt;p&gt;In Next.js 16, the old &lt;code&gt;middleware.ts&lt;/code&gt; convention is now &lt;code&gt;proxy.ts&lt;/code&gt;. If it has a catch-all matcher for redirects, locale prefixes or auth, it will happily rewrite &lt;code&gt;/studio&lt;/code&gt; into &lt;code&gt;/en/studio&lt;/code&gt; or bounce it to a login page. Studio also loads assets and makes requests under its base path, so a partial exclusion causes half-working screens.&lt;/p&gt;

&lt;p&gt;Exclude the Studio path from the matcher, and make sure &lt;code&gt;basePath&lt;/code&gt; in &lt;code&gt;sanity.config.ts&lt;/code&gt; is exactly &lt;code&gt;/studio&lt;/code&gt; to match the route. Multilingual sites are the most common victims here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 4: the Studio is coupled to your deploy pipeline
&lt;/h2&gt;

&lt;p&gt;When the Studio lives in the same repo and app, any schema change triggers a full site build. On a large site that can mean editors wait on a long build to get one new field. Studio compilation also adds to build time on its own. If this is your pain, read my write-up on &lt;a href="https://nayankyada.com/blog/nextjs-build-taking-too-long-on-a-sanity-site-causes-and-the-fix" rel="noopener noreferrer"&gt;slow Next.js builds on Sanity sites&lt;/a&gt; first, because the content fetching is usually the bigger cost.&lt;/p&gt;

&lt;p&gt;If the coupling is still a problem, you have two options:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Keep embedding, but split into a monorepo with the Studio as its own app and its own deploy. Editors get a Studio URL on your domain and schema changes ship independently.&lt;/li&gt;
&lt;li&gt;Deploy the Studio with Sanity's own hosting using the Sanity CLI and drop it from the Next.js app entirely.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The trade-off: you lose the single-domain convenience, and preview and Presentation setups need their origins configured. For a team with a handful of editors and a small site, I would keep it embedded and just fix causes 1 to 3.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 5: the Studio is visible to search engines
&lt;/h2&gt;

&lt;p&gt;A Studio that is crawlable is not a security hole, since it needs a login, but it is clutter and occasionally shows up in search results. Add a &lt;code&gt;Disallow: /studio&lt;/code&gt; line in your robots file, make sure the sitemap generator only lists content from Sanity, and do not link to the Studio from public pages. If a staging copy is also exposed, the &lt;a href="https://nayankyada.com/blog/staging-site-indexed-by-google-fix-a-nextjs-sanity-setup" rel="noopener noreferrer"&gt;staging indexed by Google guide&lt;/a&gt; covers the broader fix.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to verify the fix
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Open &lt;code&gt;/studio&lt;/code&gt; in a private window. No site header, no analytics requests in the network tab, and Studio styles look native.&lt;/li&gt;
&lt;li&gt;Run your bundle analyser and search the public route chunks for Studio packages. There should be none.&lt;/li&gt;
&lt;li&gt;Load a public page and check the network tab: no Studio chunks downloaded.&lt;/li&gt;
&lt;li&gt;Change a schema field and time how long the deploy takes. Compare to before.&lt;/li&gt;
&lt;li&gt;Fetch &lt;code&gt;/robots.txt&lt;/code&gt; and confirm the Studio path is disallowed.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What it costs to ignore
&lt;/h2&gt;

&lt;p&gt;A leaked config quietly adds weight to every page visitors load, which shows up in Core Web Vitals. A Studio that looks broken makes editors avoid it, which is a bigger problem than it sounds; see &lt;a href="https://nayankyada.com/blog/sanity-studio-editor-experience-why-editors-avoid-it-and-the-fix" rel="noopener noreferrer"&gt;why editors avoid the Studio&lt;/a&gt;. And a Studio tangled into your site layout makes every handover harder.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to ask a developer
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Does the Studio have its own root layout, separate from the public site?&lt;/li&gt;
&lt;li&gt;Can you show me the bundle report proving no Studio code reaches public routes?&lt;/li&gt;
&lt;li&gt;Is the Studio excluded from the proxy matcher and from the sitemap?&lt;/li&gt;
&lt;li&gt;Can schema changes ship without a full site rebuild, and if not, why not?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the kind of cleanup I do as part of a &lt;a href="https://dev.to/services#packages"&gt;Performance and Sanity audit&lt;/a&gt;, which starts from $800 and includes the bundle and Studio structure review.&lt;/p&gt;

</description>
      <category>sanitystudio</category>
      <category>nextjs</category>
      <category>approuter</category>
      <category>bundlesize</category>
    </item>
    <item>
      <title>Next.js sitemap wrong on a Sanity site: missing pages and stale URLs</title>
      <dc:creator>Nayan Kyada</dc:creator>
      <pubDate>Wed, 18 Nov 2026 13:16:26 +0000</pubDate>
      <link>https://dev.to/nayankyada/nextjs-sitemap-wrong-on-a-sanity-site-missing-pages-and-stale-urls-50nk</link>
      <guid>https://dev.to/nayankyada/nextjs-sitemap-wrong-on-a-sanity-site-missing-pages-and-stale-urls-50nk</guid>
      <description>&lt;p&gt;If your Next.js sitemap is wrong on a Sanity site, you will see it in Search Console first: new pages never get discovered, deleted pages keep getting crawled, or the sitemap lists URLs that redirect or 404. It is one of the quietest SEO bugs on a headless build, because the site looks fine to editors and the file nobody opens is the one that is broken.&lt;/p&gt;

&lt;p&gt;This post covers the usual causes, in the order I check them, and how to confirm the fix. If your problem is that pages are in the sitemap but Google still refuses to index them, that is a different issue, covered in &lt;a href="https://nayankyada.com/blog/nextjs-pages-not-indexed-by-google-causes-and-the-fix-path" rel="noopener noreferrer"&gt;Next.js pages not indexed by Google&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a wrong sitemap looks like
&lt;/h2&gt;

&lt;p&gt;Open /sitemap.xml on the live domain and compare it with what editors have published. The symptoms fall into four groups:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Recently published pages are absent for days or weeks.&lt;/li&gt;
&lt;li&gt;URLs for deleted or unpublished documents are still listed.&lt;/li&gt;
&lt;li&gt;URLs point to the wrong host, such as a staging or preview domain.&lt;/li&gt;
&lt;li&gt;Pages marked noindex, redirected pages, or empty template routes appear in the list.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each group has a different cause, so do not rewrite the whole file before you know which one you have.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 1: the sitemap was built once and never refreshed
&lt;/h2&gt;

&lt;p&gt;A sitemap route with no dynamic data is rendered at build time and served as a static file. If the file queries Sanity but nothing triggers a rebuild or revalidation, it stays frozen at the last deploy. Editors publish ten pages, and the sitemap still reflects last month.&lt;/p&gt;

&lt;p&gt;The fix is to tag the fetch so your publish webhook can invalidate it. If you already revalidate pages from Sanity, add the sitemap tag to the same handler. The webhook setup is in &lt;a href="https://nayankyada.com/blog/how-i-wire-sanity-webhooks-to-nextjs-isr-revalidation-with-hmac-verification" rel="noopener noreferrer"&gt;Sanity webhooks and Next.js ISR revalidation with HMAC verification&lt;/a&gt;, and batching is covered in &lt;a href="https://nayankyada.com/blog/sanity-webhook-revalidation-batching-tags-nextjs-isr" rel="noopener noreferrer"&gt;Sanity webhook revalidation: batching tags&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 2: the GROQ query filters too much or too little
&lt;/h2&gt;

&lt;p&gt;This is the most common one. Someone wrote the query when the schema had three fields, and it has not changed since. Typical mistakes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Requiring a field such as a publish date that older documents never had, so they silently drop out.&lt;/li&gt;
&lt;li&gt;Querying only one document type and forgetting landing pages, case studies, or locale variants.&lt;/li&gt;
&lt;li&gt;Not excluding drafts, so URLs for unpublished work appear.&lt;/li&gt;
&lt;li&gt;Ignoring a noindex toggle in the SEO fields.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is the pattern I start from, in app/sitemap.ts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/sitemap.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;MetadataRoute&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"next"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"@/sanity/lib/client"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;BASE_URL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;NEXT_PUBLIC_SITE_URL&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;SITEMAP_QUERY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sb"&gt;`*[
  _type in ["page", "post", "caseStudy"] &amp;amp;&amp;amp;
  defined(slug.current) &amp;amp;&amp;amp;
  !(_id in path("drafts.**")) &amp;amp;&amp;amp;
  coalesce(seo.noIndex, false) != true
]{
  _type,
  "slug": slug.current,
  _updatedAt
}`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;PREFIX&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"/blog"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;caseStudy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"/work"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sitemap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MetadataRoute&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Sitemap&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;docs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;_type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;slug&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;_updatedAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;SITEMAP_QUERY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"sitemap"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;docs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sb"&gt;`${BASE_URL}${PREFIX[doc._type] ?? ""}/${doc.slug}`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;lastModified&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_updatedAt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The coalesce matters: without it, documents that never had the noIndex field can be dropped by the comparison. Test the query in Vision inside Studio and count the results against the number of published documents you expect. If the numbers disagree, the query is your bug.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 3: the wrong base URL
&lt;/h2&gt;

&lt;p&gt;If the sitemap lists a staging host, an env var is wrong in production, or the build was made with a preview value. Check where NEXT_PUBLIC_SITE_URL comes from in each Vercel environment. Hardcoding a fallback to localhost or a vercel.app domain is a trap: it works in development and quietly ships. If the staging site itself ended up in search results, see &lt;a href="https://nayankyada.com/blog/staging-site-indexed-by-google-fix-a-nextjs-sanity-setup" rel="noopener noreferrer"&gt;staging site indexed by Google&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 4: URL shapes that do not match the routes
&lt;/h2&gt;

&lt;p&gt;The sitemap builds a path from the slug, but the route might differ. Examples I see on inherited projects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Slugs stored with a leading slash, producing double slashes.&lt;/li&gt;
&lt;li&gt;Trailing slash settings that disagree between the sitemap and the router.&lt;/li&gt;
&lt;li&gt;Locale-prefixed routes, where the sitemap lists unprefixed URLs.&lt;/li&gt;
&lt;li&gt;Slugs that were changed in Sanity, with the old URL redirected but the sitemap still emitting a stale value from another document that references it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every sitemap URL should return a 200 directly, with no redirect. A list full of redirected or 404 URLs trains Google to trust the file less. If you suspect broken routes, &lt;a href="https://nayankyada.com/blog/nextjs-soft-404-errors-on-a-sanity-site-causes-and-fixes" rel="noopener noreferrer"&gt;Next.js soft 404 errors on a Sanity site&lt;/a&gt; walks through the diagnosis, and &lt;a href="https://nayankyada.com/blog/sanity-slug-changes-causing-404s-how-to-manage-redirects-in-nextjs" rel="noopener noreferrer"&gt;slug changes causing 404s&lt;/a&gt; covers redirects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 5: the file is too big or has no index
&lt;/h2&gt;

&lt;p&gt;One sitemap file can hold up to 50,000 URLs. Large catalogues or multilingual sites with many variants can pass that. Next.js has a generateSitemaps function in the sitemap file for splitting the output into several files. Check the current Next.js docs for the exact signature before you use it, because the arguments passed to the sitemap function changed in recent major versions. You also need robots.txt to point at the sitemap location, so confirm it with a quick look at /robots.txt.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to verify the fix
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Count the URLs in /sitemap.xml and compare with the published document count from a GROQ count query in Vision.&lt;/li&gt;
&lt;li&gt;Publish a test page and confirm it shows up after your webhook fires, without a redeploy.&lt;/li&gt;
&lt;li&gt;Unpublish it and confirm it leaves the file.&lt;/li&gt;
&lt;li&gt;Crawl every sitemap URL with a crawler and check that each returns 200 with no redirect and no noindex tag.&lt;/li&gt;
&lt;li&gt;Submit the sitemap in Search Console and watch the discovered-URL count. Reading it takes a few days, so do not judge it the same afternoon.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What to ask a developer
&lt;/h2&gt;

&lt;p&gt;If you hire someone to fix this, ask which document types are covered and why, how the sitemap is invalidated on publish, and how locales and noindex pages are handled. A good answer names the GROQ filter and the cache tag. A vague answer about regenerating on deploy means the same problem will return.&lt;/p&gt;

&lt;p&gt;This is work I do as part of a &lt;a href="https://dev.to/services#packages"&gt;Performance and Sanity audit&lt;/a&gt;, which starts from $800 and covers the sitemap, robots, redirects, and indexing checks together. For the wider picture of preserving rankings during changes, &lt;a href="https://nayankyada.com/blog/website-redesign-without-losing-seo-a-sanity-nextjs-checklist" rel="noopener noreferrer"&gt;website redesign without losing SEO&lt;/a&gt; is the checklist I use.&lt;/p&gt;

</description>
      <category>seo</category>
      <category>sanitycms</category>
      <category>nextjs</category>
      <category>groq</category>
    </item>
    <item>
      <title>Next.js soft 404 errors on a Sanity site: causes and fixes</title>
      <dc:creator>Nayan Kyada</dc:creator>
      <pubDate>Tue, 17 Nov 2026 13:42:14 +0000</pubDate>
      <link>https://dev.to/nayankyada/nextjs-soft-404-errors-on-a-sanity-site-causes-and-fixes-1d1j</link>
      <guid>https://dev.to/nayankyada/nextjs-soft-404-errors-on-a-sanity-site-causes-and-fixes-1d1j</guid>
      <description>&lt;p&gt;If Google Search Console shows "Soft 404" against URLs on your Next.js and Sanity site, the cause is almost always the same: a page that has no content to show still answers with HTTP 200. The server says the page is fine, the page says nothing is here, and Google believes the page.&lt;/p&gt;

&lt;p&gt;This post covers how that happens on Sanity-backed sites, how to confirm it, and the fix order I use. It sits next to &lt;a href="https://nayankyada.com/blog/nextjs-pages-not-indexed-by-google-causes-and-the-fix-path" rel="noopener noreferrer"&gt;Next.js pages not indexed by Google&lt;/a&gt;, which covers the wider indexing picture.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a soft 404 looks like on a Sanity site
&lt;/h2&gt;

&lt;p&gt;Typical symptoms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Search Console lists URLs under Page indexing as "Soft 404".&lt;/li&gt;
&lt;li&gt;Visiting a deleted or mistyped slug shows a blank layout, an empty heading, or a friendly "Nothing found" message, but the network tab says 200.&lt;/li&gt;
&lt;li&gt;Old URLs from unpublished or renamed documents stay in Google's report for months.&lt;/li&gt;
&lt;li&gt;Category or tag pages with zero posts render a header and an empty grid.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A real 404 is a status code, not a message. Visitors cannot tell the difference, which is why this survives for years unnoticed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why ignoring it costs you
&lt;/h2&gt;

&lt;p&gt;Google drops soft 404 URLs from the index, which is fine for truly dead pages. The damage is elsewhere. Crawl budget goes to dead URLs, your Search Console reports fill with noise so real problems hide, and if the empty page is a page you wanted indexed (for example a document that fails to resolve because of a query bug), you lose it silently. If you recently changed slugs, also read &lt;a href="https://nayankyada.com/blog/sanity-slug-changes-causing-404s-how-to-manage-redirects-in-nextjs" rel="noopener noreferrer"&gt;Sanity slug changes causing 404s&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 1: the page never calls notFound()
&lt;/h2&gt;

&lt;p&gt;This is the most common one. The GROQ query returns null for an unknown slug, and the component handles null by rendering less, not by signalling a 404.&lt;/p&gt;

&lt;p&gt;Broken pattern:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/blog/[slug]/page.tsx&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@/sanity/lib/client'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;postQuery&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@/sanity/lib/queries'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;PostPage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;slug&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;slug&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;postQuery&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;slug&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;// Unknown slug: post is null, page still returns 200&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fix: call notFound() as soon as the data is missing, and do the same in generateMetadata so the metadata does not render a default title for a page that does not exist.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/blog/[slug]/page.tsx&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;notFound&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'next/navigation'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@/sanity/lib/client'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;postQuery&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@/sanity/lib/queries'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;dynamicParams&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;PostPage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;slug&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;slug&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;postQuery&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;slug&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;notFound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add an app/not-found.tsx so the 404 page is branded and useful. In the App Router, notFound() renders the closest not-found file and sets the 404 status.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 2: a try/catch that swallows the failure
&lt;/h2&gt;

&lt;p&gt;I often see data helpers that wrap the Sanity fetch in try/catch and return an empty object on error. A network failure, a wrong token, or a bad query then looks identical to a missing page, and the empty object is truthy, so a null check never fires. Let genuine errors throw so you get a 500 and an alert, and reserve notFound() for the case where the query succeeded and returned nothing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 3: streaming after the status line is sent
&lt;/h2&gt;

&lt;p&gt;If the page wraps its main data fetch inside a Suspense boundary and calls notFound() inside that boundary, the response may already have started streaming with a 200. Next.js cannot change the status afterwards. It adds a noindex meta tag to the streamed output instead, which Google generally respects, but you lose the clean 404.&lt;/p&gt;

&lt;p&gt;The rule I follow: fetch the document that decides whether the page exists before any Suspense boundary, in the page component itself. Stream the slow, secondary sections (related posts, comments, recommendations) behind Suspense. Check the response status in a terminal after changing this, because it is easy to assume it works.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 4: empty listing pages
&lt;/h2&gt;

&lt;p&gt;Category, tag, author, or paginated pages with no results are the second large source. Decide a policy per route:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A category document exists but has no posts: return 200 with a useful message, or 404 if you do not want it indexed.&lt;/li&gt;
&lt;li&gt;Page number beyond the last page: return 404.&lt;/li&gt;
&lt;li&gt;Tag slug that does not exist in Sanity: 404.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For pagination, check the total count from the same query and call notFound() when the page is out of range. A page=9999 URL should never return 200.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 5: catch-all routes and missing generateStaticParams control
&lt;/h2&gt;

&lt;p&gt;Catch-all segments such as app/[...slug]/page.tsx accept every URL. Without a null check they answer 200 for anything. Even with the check, make sure the GROQ filter includes the document type and a defined slug, otherwise a draft or a wrong document type can match. Also exclude drafts from published queries, since drafts with the same slug can leak into results.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to verify the fix
&lt;/h2&gt;

&lt;p&gt;Check the status code, not the page. Use a terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -s -o /dev/null -w &lt;span class="s2"&gt;"%{http_code}\n"&lt;/span&gt; https://example.com/blog/this-slug-does-not-exist
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expect 404. Then test a real slug (200), an unpublished document slug (404), an out-of-range page number (404), and an empty category. In Search Console, use URL Inspection on a few flagged URLs, then validate the fix in the Page indexing report. Re-crawl takes days to weeks, so do not expect the report to clear immediately.&lt;/p&gt;

&lt;p&gt;For a full pass, crawl the site with any crawler that reports status codes and compare against your sitemap. Every sitemap URL must return 200. If your sitemap contains slugs that now 404, fix the sitemap query as well, and use redirects for URLs that moved rather than leaving them dead. Broken document links are a related failure mode covered in &lt;a href="https://nayankyada.com/blog/sanity-broken-references-crashing-nextjs-pages-find-and-fix-them" rel="noopener noreferrer"&gt;Sanity broken references crashing Next.js pages&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to ask a developer
&lt;/h2&gt;

&lt;p&gt;If you are hiring someone to fix this, ask:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Which routes can return a page for data that does not exist, and how will you list them?&lt;/li&gt;
&lt;li&gt;Will you verify status codes with real requests, not just by looking at the page?&lt;/li&gt;
&lt;li&gt;How do empty listings and out-of-range pagination behave after the change?&lt;/li&gt;
&lt;li&gt;Will redirects be added for URLs that used to be live and now 404?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A developer who answers with status codes and a route list understands the problem. One who offers only to add a nicer 404 design does not.&lt;/p&gt;

&lt;p&gt;This is part of the work covered in a &lt;a href="https://dev.to/services#packages"&gt;Performance &amp;amp; Sanity audit, from $800&lt;/a&gt;, which includes a route-by-route status code check. If your Search Console report is full of soft 404s and you want it traced and fixed, that is work I do, and you can see the options on the &lt;a href="https://nayankyada.com/services" rel="noopener noreferrer"&gt;services page&lt;/a&gt;. For a general look at what such a review covers, see &lt;a href="https://nayankyada.com/blog/nextjs-website-audit-what-a-paid-one-covers-and-how-to-judge-it" rel="noopener noreferrer"&gt;what a paid Next.js website audit includes&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>sanitycms</category>
      <category>nextjs</category>
      <category>approuter</category>
      <category>seo</category>
    </item>
    <item>
      <title>Sanity editors pasting from Word or Google Docs: clean up broken Portable Text</title>
      <dc:creator>Nayan Kyada</dc:creator>
      <pubDate>Mon, 16 Nov 2026 13:00:11 +0000</pubDate>
      <link>https://dev.to/nayankyada/sanity-editors-pasting-from-word-or-google-docs-clean-up-broken-portable-text-598d</link>
      <guid>https://dev.to/nayankyada/sanity-editors-pasting-from-word-or-google-docs-clean-up-broken-portable-text-598d</guid>
      <description>&lt;p&gt;If your editors write in Word or Google Docs and paste into Sanity, you have probably seen it: a random gap in the middle of an article, a heading that looks wrong, a list that renders as plain text. This is broken Portable Text from pasted content, and it is one of the most common reasons editors lose trust in a Sanity site. It is fixable, and mostly in the schema and the renderer rather than in the editors' habits.&lt;/p&gt;

&lt;h2&gt;
  
  
  What pasted content does to Portable Text
&lt;/h2&gt;

&lt;p&gt;Portable Text is structured data, not HTML. When someone pastes rich text, Studio tries to map each piece onto your schema: paragraphs become blocks, bold becomes a mark, headings become styles. Anything that does not map cleanly still gets stored, and that is where problems start.&lt;/p&gt;

&lt;p&gt;The symptoms I see on inherited sites:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Large vertical gaps caused by empty blocks, usually from blank lines in the source document.&lt;/li&gt;
&lt;li&gt;Heading levels that skip, for example an h4 under an h2, because the source used styles your design never expected.&lt;/li&gt;
&lt;li&gt;Lists that lose their nesting or turn into paragraphs starting with a dash character.&lt;/li&gt;
&lt;li&gt;Links with tracking redirect URLs, such as the ones Google Docs sometimes wraps links in.&lt;/li&gt;
&lt;li&gt;Whole sections silently missing on the live site because the frontend does not know a style or mark name.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of these throw an error. That is why they sit in production for months.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it costs to ignore
&lt;/h2&gt;

&lt;p&gt;The visible cost is editors who spend twenty minutes per article fixing formatting by hand, then stop using Studio for anything beyond typos. The less visible cost is SEO: skipped heading levels and empty elements weaken page structure, and long runs of empty blocks hurt readability and layout stability. If you also see editors working around the Studio in other ways, read my notes on &lt;a href="https://nayankyada.com/blog/sanity-studio-editor-experience-why-editors-avoid-it-and-the-fix" rel="noopener noreferrer"&gt;why editors avoid Sanity Studio and the fix&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: measure how bad it is
&lt;/h2&gt;

&lt;p&gt;Before changing anything, count the damage. This GROQ query finds documents containing empty text blocks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*[_type == "post" &amp;amp;&amp;amp; count(body[_type == "block" &amp;amp;&amp;amp; length(pt::text(@)) == 0]) &amp;gt; 0]{
  _id,
  title,
  "emptyBlocks": count(body[_type == "block" &amp;amp;&amp;amp; length(pt::text(@)) == 0])
} | order(emptyBlocks desc)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it in Vision, the query tool inside Studio. Swap the document type and field name for your own. You can build similar queries for unexpected styles by comparing each block's style against the list you actually support. If your queries get more involved, my guide to &lt;a href="https://nayankyada.com/blog/advanced-groq-queries-joins-coalesce-and-array-flattening-in-sanity" rel="noopener noreferrer"&gt;advanced GROQ queries&lt;/a&gt; covers the patterns.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: restrict the schema so bad structure has nowhere to land
&lt;/h2&gt;

&lt;p&gt;Most rescued schemas I open have a block type with every default style and decorator enabled, plus a dozen custom blocks nobody uses. Trim it to what the design really renders. Fewer allowed styles means pasted headings get normalised instead of stored as something your frontend cannot style.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// sanity/schemaTypes/blockContent.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;defineType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;defineArrayMember&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'sanity'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;blockContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;defineType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'blockContent'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Body'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'array'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nf"&gt;defineArrayMember&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'block'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Paragraph'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'normal'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Heading 2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'h2'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Heading 3'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'h3'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Quote'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'blockquote'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;lists&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Bullet'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'bullet'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Numbered'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'number'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;decorators&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Strong'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'strong'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Emphasis'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'em'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;annotations&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'link'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'object'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Link'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'href'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'url'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'URL'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;defineArrayMember&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'image'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The rule I follow: if the design system has no style for it, the schema does not offer it. Studio then maps unsupported source formatting onto the closest allowed style instead of preserving something your site cannot render. Test with a real document from your editors rather than a clean sample, because that is where the surprises are.&lt;/p&gt;

&lt;p&gt;If this schema has also grown into a sprawl of block types, the same logic applies at page level; see &lt;a href="https://nayankyada.com/blog/sanity-page-builder-out-of-control-how-to-rescue-it" rel="noopener noreferrer"&gt;rescuing an out-of-control page builder&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: make the frontend fail loudly in development
&lt;/h2&gt;

&lt;p&gt;Silent failure is what let this hide. The Portable Text renderer for React lets you define handlers for unknown types, marks, block styles and lists. Use them to log in development and render a safe fallback in production, so a stray style produces a paragraph rather than a hole. I would not show editors a visible error banner on the live site, but a console warning plus a preview-only highlight makes problems obvious during review. Component patterns are in my write-up on &lt;a href="https://nayankyada.com/blog/how-i-build-sanity-portable-text-custom-components-in-nextjs" rel="noopener noreferrer"&gt;Portable Text custom components in Next.js&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: clean existing content with a migration script
&lt;/h2&gt;

&lt;p&gt;Schema changes do not rewrite old documents. For the backlog, use a script that removes empty blocks and normalises styles. Always run it against a copy of the dataset first, as the content is the asset you cannot rebuild.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// scripts/clean-empty-blocks.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;createClient&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@sanity/client'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SANITY_PROJECT_ID&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SANITY_DATASET&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;apiVersion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'2025-01-01'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SANITY_WRITE_TOKEN&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;useCdn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Span&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Block&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;_type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="nc"&gt;Span&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;style&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;isEmptyBlock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'block'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;children&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;every&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;docs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Block&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sb"&gt;`*[_type == "post" &amp;amp;&amp;amp; defined(body)]{_id, body}`&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;docs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;cleaned&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;isEmptyBlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cleaned&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;patch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;cleaned&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Cleaned'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two cautions. First, this patches published documents directly, so check whether drafts exist and handle them too, or editors will overwrite your fix when they publish. Second, keep the write token out of the repo and revoke it afterwards. Exposed tokens are a separate problem; I cover that in &lt;a href="https://nayankyada.com/blog/sanity-api-token-exposed-in-a-nextjs-site-how-to-check-and-fix-it" rel="noopener noreferrer"&gt;checking for an exposed Sanity API token&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: give editors a short paste routine
&lt;/h2&gt;

&lt;p&gt;Technical fixes handle most cases, but a one-page guide helps: paste as plain text for body copy, apply styles inside Studio, and add images through the image block rather than pasting them. This is the kind of setup work covered in &lt;a href="https://nayankyada.com/blog/cms-for-marketing-teams-what-to-set-up-before-editors-touch-it" rel="noopener noreferrer"&gt;what to configure before editors touch a CMS&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to verify the fix
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The GROQ query from step 1 returns zero documents.&lt;/li&gt;
&lt;li&gt;A real pasted Google Docs article renders with correct heading order and no gaps.&lt;/li&gt;
&lt;li&gt;No unknown style or mark warnings appear in the development console on your five longest articles.&lt;/li&gt;
&lt;li&gt;Editors confirm that publishing from a pasted draft no longer needs manual cleanup.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What to ask a developer
&lt;/h2&gt;

&lt;p&gt;Ask how they will measure the damage first, whether they test with real editor content, whether the migration runs on a dataset copy, and how drafts are handled. A developer who only proposes tightening the schema will leave old content broken. One who only writes a script will let the mess return.&lt;/p&gt;

&lt;p&gt;This is work I do as part of a &lt;a href="https://dev.to/services#packages"&gt;Performance and Sanity audit, from $800&lt;/a&gt;, which covers the schema, the renderer and a content cleanup plan for your site.&lt;/p&gt;

</description>
      <category>sanitycms</category>
      <category>sanitystudio</category>
      <category>portabletext</category>
      <category>groq</category>
    </item>
  </channel>
</rss>
