Issue with flickering previews in Slack thread, resolved by changing implementation.

8 replies
Last updated: Sep 7, 2023
Can anyone confirm if previews are supposed to flicker like this or if i'm doing something wrong (using the iframe plugin for previews, with the app router):
AI Update

Flickering in iframe previews with Next.js App Router is a common issue, and you're not alone in experiencing this! The flickering typically happens because the iframe is doing full page reloads instead of smooth content updates when you edit in Studio.

Why This Happens

The iframe pane plugin wasn't specifically designed for the modern App Router architecture, which can cause the preview to refresh the entire page when content changes rather than hot-reloading just the updated content. This creates the flickering effect you're seeing.

The Modern Solution: Use the Presentation Tool

Instead of the iframe pane plugin, I'd strongly recommend switching to the Presentation Tool with Visual Editing. This is Sanity's officially supported, modern approach for live previews and it's specifically built to work seamlessly with Next.js App Router.

The Presentation Tool gives you:

  • No flickering - Smooth, real-time content updates without page refreshes
  • Click-to-edit - Click any content element in the preview to jump straight to editing that field
  • Better performance - Purpose-built for modern frameworks like Next.js App Router
  • Draft Mode integration - Proper handling of draft content with Next.js conventions

Getting Started

  1. Update your Studio to v3.40.0 or later:
npm install sanity@latest
  1. Follow the Visual Editing with Next.js App Router guide to set it up

  2. Configure the Presentation Tool in your sanity.config.ts:

import { presentationTool } from 'sanity/presentation'

export default defineConfig({
  plugins: [
    presentationTool({
      previewUrl: {
        previewMode: {
          enable: '/api/draft-mode/enable',
        },
      },
    }),
  ],
})

The Presentation Tool appears in your Studio toolbar and embeds your app's preview directly in the interface. It handles all the complexity of live updates and draft content properly, eliminating the flickering issues you're experiencing with the iframe plugin.

If you want to learn more, there's also a full Visual Editing with Next.js course that walks through the entire setup.

It's doing a full refresh it seems rather than hot reloading
maybe you are returning null if the data is not there?
Right - that helps. Less flickery. Do you know if the page should still fully refresh to show the content?
I was expecting a hot reload content swap
Yes. I just changed my implementation from that to the one here https://www.sanity.io/guides/nextjs-app-router-live-preview and it works correctly now
Thanks for your help
No problem!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?