User seeks help to view specific documents on live Sanity site

3 replies
Last updated: Dec 23, 2024
Hi, I'm getting Sanity started but trying to figure out how to view some specific document types on the live site - e.g. a button on certain document that I know has a specific production URL, so it just can be opened in e.g. in a new tab in the browser. I have the presentation plugin configured and working but it's turning out to be hard for the content editors to navigate there to view how what they've done looks like. It would be nice to on certain documents add a button that opens the corresponding production page with the same slug etc. Any directions in the documentation on how to configure this on specific document level would be appreciated 🙂
AI Update

You have a few great options for adding "View on Site" functionality to your Sanity Studio documents!

You can create a custom document action that adds a button to the document toolbar. This is perfect for a straightforward "Open in new tab" button.

Here's how to implement it:

// documentActions.js
export const ViewOnSiteAction = (props) => {
  const slug = props.draft?.slug?.current || props.published?.slug?.current
  
  return {
    label: 'View on Site',
    icon: () => '🌐',
    onHandle: () => {
      // Construct your production URL
      const url = `https://yoursite.com/${slug}`
      window.open(url, '_blank')
    }
  }
}

// In your sanity.config.ts
import {defineConfig} from 'sanity'

export default defineConfig({
  // ... other config
  document: {
    actions: (prev, context) => {
      // Add the action to specific document types
      if (context.schemaType === 'post' || context.schemaType === 'page') {
        return [...prev, ViewOnSiteAction]
      }
      return prev
    }
  }
})

This gives you a clean button in the document toolbar that opens your production URL in a new tab.

Option 2: Iframe Pane Plugin

If you want to show the preview within Studio (not just a link), check out the sanity-plugin-iframe-pane. This displays your production URL in a split pane view right next to the editor, with built-in buttons to copy the URL or open in a new tab.

Install it with:

npm i sanity-plugin-iframe-pane

This gives editors a side-by-side editing and preview experience, which might actually be more intuitive than navigating to the Presentation tool.

Option 3: Document Badges

While document badges are primarily for displaying status information next to the toolbar, they're less suitable for interactive elements like clickable links. They're better suited for visual indicators rather than navigation.

Why Not Just Use Presentation Tool?

I totally understand your concern! The Presentation tool is powerful for Visual Editing but can be overkill if editors just want to quickly check how something looks on the live site. A custom document action or iframe pane gives them immediate, one-click access without extra navigation.

The document action approach is probably your best bet for simplicity—it's just a button that does exactly what you need, and you can customize it per document type to handle different URL structures.

take a look at this plugin: https://www.sanity.io/plugins/iframe-pane
Thanks, I think this is what I'm looking for!
Worked like a charm, thanks for the tip!

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?