Using Live Preview on Sanity and Next 13.4
Introduction
Have you ever found yourself wondering how your website will look before hitting the publish button? Do you want to make sure everything is perfect before presenting it to the world? Well, now you can with Live Preview on Sanity and Next 13.4.
Live Preview is a feature that allows you to see how your website will look in real-time. It provides you with an interactive preview of your content, so you can make changes and see how they affect the overall design of your website. It is a handy tool for web developers, content creators, and anyone who wants to ensure that their website looks and functions as intended.
In this article, we will explore the benefits of using Live Preview on Sanity and Next 13.4, how it works, and how to get started.
Understanding Sanity and Next 13.4
If you're new to Sanity and Next 13.4, it's essential to have a basic understanding of what they are and how they work together. Sanity is a headless CMS that allows you to create, manage, and distribute content to any platform or device. Next 13.4 is a popular React framework for building web applications. When used together, Sanity and Next 13.4 provide a powerful platform for creating dynamic, responsive websites.
One of the advantages of using Sanity and Next 13.4 is that they allow you to separate the content from the presentation layer. This means that you can focus on creating high-quality content without having to worry about how it will look on the website. The content is stored in Sanity, and Next 13.4 pulls the content from Sanity and uses it to generate the website.
Setting up Live Preview
On the sanity side,
we use a custom preview component.
There are three things to take care of.
- Resolving preview URLs
- sanity desk structure
- preview component
Resolving preview URLs
// resolveProductionUrl.tsimport type { SanityDocument } from 'sanity';const previewSecret = '__some__secret__text__';const remoteUrl = `__PROD__URL__`;const localUrl = `http://localhost:3000`;function getSlug(slug: any) {if (!slug) return '/';if (slug.current) return slug.current;return '/';}export default function resolveProductionUrl(doc: SanityDocument) {const baseUrl =window.location.hostname === 'localhost' ? localUrl : remoteUrl;const previewUrl = new URL(baseUrl);const slug = doc.slug;previewUrl.pathname = `/api/draft`;previewUrl.searchParams.append(`secret`, previewSecret);previewUrl.searchParams.append(`slug`, getSlug(slug));return previewUrl.toString().replaceAll('%2F', '/');}
Sanity Desk Structure
// structure.tsimport { FaHome, FaFile, FaFileWord, FaQuestionCircle } from 'react-icons/fa';import { StructureBuilder } from 'sanity/desk';import { PreviewIFrame } from './component/preview';export const structure = (S: StructureBuilder) =>S.list().title('Content').items([S.listItem().title('Main Page').icon(FaHome).child(S.document().views([S.view.form(),S.view.component(PreviewIFrame).options({ tes: 'ss' }).title('Preview'),]).schemaType('mainPage').documentId('mainPage'),),S.divider(),S.documentTypeListItem('page').title('Page').icon(FaFile),S.documentTypeListItem('blog').title('Blog').icon(FaFileWord),S.documentTypeListItem('faq').title('FAQs').icon(FaQuestionCircle),]);export const defaultDocumentNode = (S: StructureBuilder) =>S.document().views([S.view.form(),S.view.component(PreviewIFrame).options({}).title('Preview'),]);
Preview Component
// studio/component/preview.tsximport {Box,Button,Card,Flex,Spinner,Text,ThemeProvider,} from '@sanity/ui';import { AiOutlineReload } from 'react-icons/ai';import { BiLinkExternal } from 'react-icons/bi';import { useEffect, useState, useRef } from 'react';import resolveProductionUrl from '../resolveProductionUrl';export function PreviewIFrame(props: any) {const { options, document } = props;const [id, setId] = useState(1);const { displayed } = document;const [displayUrl, setDisplayUrl] = useState('');const iframe = useRef<HTMLIFrameElement>(null);function handleReload() {if (!iframe?.current) return;setId(id + 1);}useEffect(() => {function getUrl() {const productionUrl = resolveProductionUrl(displayed) ?? '';setDisplayUrl(productionUrl);}getUrl();}, [displayed]);if (displayUrl === '')return (<ThemeProvider><Flex padding={5} align="center" justify="center"><Spinner /></Flex></ThemeProvider>);return (<ThemeProvider><Flex direction="column" style={{ height: `100%` }}><Card padding={2} borderBottom><Flex align="center" gap={2}><Box flex={1}><Text size={0} textOverflow="ellipsis">{displayUrl}</Text></Box><Flex align="center" gap={1}><ButtonfontSize={[1]}padding={2}icon={AiOutlineReload}title="Reload"text="Reload"aria-label="Reload"onClick={() => handleReload()}/><ButtonfontSize={[1]}icon={BiLinkExternal}padding={[2]}text="Open"tone="primary"onClick={() => window.open(displayUrl)}/></Flex></Flex></Card><Card tone="transparent" padding={0} style={{ height: `100%` }}><Flex align="center" justify="center" style={{ height: `100%` }}><iframekey={id}ref={iframe}title="preview"style={{ width: '100%', height: `100%`, maxHeight: `100%` }}src={displayUrl}referrerPolicy="origin-when-cross-origin"frameBorder={0}/></Flex></Card></Flex></ThemeProvider>);}
On the Next.js Side of things
creating an API route to configure the draft mode
enabling drafts mode
// src/app/api/draft/route.tsimport { draftMode } from 'next/headers';import { SANITY_PREVIEW_SECRET } from '~/config';import { nativeRedirect } from '~/lib/utils';export async function GET(request: Request) {const { searchParams } = new URL(request.url);const secret = searchParams.get('secret');const slug = searchParams.get('slug');// SANITY_PREVIEW_SECRET is the same secret from above resolve preivew url.if (secret !== SANITY_PREVIEW_SECRET || !slug) {return new Response('Invalid token', { status: 401 });}const draft = draftMode();draft.enable();return nativeRedirect(slug);}
disabling draft mode
// src/app/api/disable-draft/route.tsimport { draftMode } from 'next/headers';import { nativeRedirect } from '~/lib/utils';export async function GET(request: Request) {const { searchParams } = new URL(request.url);const draft = draftMode();draft.disable();return nativeRedirect('/');// there was a bug using redirect on this next version// but now its patched up you can simply use// redirect from "next/navigation";}
As there was a bug on using redirects on next 13.4 which is fixed by the way, that made me use this custom redirect helper
// lib/utilsexport const nativeRedirect = (path: string) =>new Response(null, {status: 307,headers: {Location: path,},});
using Preview on the route.
import { Metadata } from 'next';import { draftMode } from 'next/headers';import { notFound } from 'next/navigation';import { getMetaData } from '~/helper';import { getClient } from '~/lib/sanity';import { mainPageQuery } from '~/lib/sanity.query';import { MainPage } from '~/schema';import { MainPageBlock } from './component';import { PreviewWrapper } from './preview';const getMainPageData = (preview?: boolean) =>getClient(preview).fetch<MainPage>(mainPageQuery);export const generateMetadata = async (): Promise<Metadata> => {const data = await getMainPageData();if (!data) return {};return getMetaData(data);};export default async function HomePageWrapper() {const { isEnabled } = draftMode();const mainPage = await getMainPageData(isEnabled);if (!mainPage) notFound();if (!isEnabled) return <MainPageBlock data={mainPage} />;return (<PreviewWrapperinitialData={mainPage}query={mainPageQuery}queryParams={{}}/>);}
Live Preview in Action