After learning how to add a new page to our Getting Started website, this guide will help you set up a Live Preview allowing you to see your website in action.
Live Preview lets users preview entry content across multiple channels before saving or publishing it to a live website. You can edit an entry and preview the content changes side by side in real-time.
To enable live preview in our website you need to perform the following steps:

REACT_APP_CONTENTSTACK_PREVIEW_TOKEN=<YOUR_PREVIEW_TOKEN>
Follow these steps to set up live preview through the code:
const getLivePreviewHostByRegion = (region: string) => {
switch (region) {
case "US":
return "rest-preview.contentstack.com";
case "EU":
return "eu-rest-preview.contentstack.com";
case "AZURE_NA":
return "azure-na-rest-preview.contentstack.com";
case "AZURE_EU":
return "azure-eu-rest-preview.contentstack.com";
default:
return "rest-preview.contentstack.com";
}
};
const getHostByRegion = (region: string) => {
switch (region) {
case "US":
return "app.contentstack.com";
case "EU":
return "eu-app.contentstack.com";
case "AZURE_NA":
return "azure-na-app.contentstack.com";
case "AZURE_EU":
return "azure-eu-app.contentstack.com";
case "GCP_NA":
return "gcp-na-api.contentstack.com";
default:
return "app.contentstack.com";
}
};
const {
REACT_APP_CONTENTSTACK_API_KEY,
REACT_APP_CONTENTSTACK_DELIVERY_TOKEN,
REACT_APP_CONTENTSTACK_ENVIRONMENT,
REACT_APP_CONTENTSTACK_REGION,
REACT_APP_CONTENTSTACK_PREVIEW_TOKEN
} = process.env;
const Stack = Contentstack.Stack({
api_key: REACT_APP_CONTENTSTACK_API_KEY as string,
delivery_token: REACT_APP_CONTENTSTACK_DELIVERY_TOKEN as string,
environment: REACT_APP_CONTENTSTACK_ENVIRONMENT as string,
region: region,
live_preview: {
enable: true,
host: getLivePreviewHostByRegion(REACT_APP_CONTENTSTACK_REGION as string),
preview_token: REACT_APP_CONTENTSTACK_PREVIEW_TOKEN as string
}
});
npm install @contentstack/live-preview-utils
import ContentstackLivePreview from "@contentstack/live-preview-utils";ContentstackLivePreview.init({
stackSdk: Stack
});
export const onEntryChange = ContentstackLivePreview.onEntryChange;
Here’s what you did above:
import { onEntryChange } from "../sdk/utils";
useEffect(() => {
onEntryChange(() => {
fetchInitialData(dispatch, setLoading);
});
}, [dispatch]);
Here's what you did above:
import { onEntryChange } from "../../sdk/utils";
useEffect(() => {
onEntryChange(() => {
fetchMenuPageData(dispatch, setLoading);
});
}, [dispatch]);
Here's what you did above:
The changes done above are in your local machine. Let’s see how to deploy this using Launch to take it live.
git add . -v

git commit -m "Add Live Preview changes"
git push origin main

You have successfully pushed your changes to Git. Now let’s deploy the new changes using Launch.
To deploy the changes you added to your application, perform the following set of steps:

REACT_APP_CONTENTSTACK_PREVIEW_TOKEN=YOUR_PREVIEW_TOKENTo start viewing your website in the Live Preview panel, perform the following steps:


Live edit tags allow you to navigate to the field that contains the website content being previewed within the Live Preview pane. When you click on the “Edit” button beside a content block in the preview pane, you will be redirected to the corresponding field within the entry. If the field holds reference to another entry, you will be redirected to the referenced entry's editor page.
For example of Live Edit Tags let us consider the header entry and the Respective code changes to Enable Live edit Tag
{
"logo": {
"title": "Header Logo.png",
"url": "https://images.contentstack.io/v3/assets/Header_Logo.png"
},
"navigation_links": {
"link": [
{ "title": "Home", "href": "/" },
{ "title": "Menu", "href": "/menu" },
{ "title": "About Us", "href": "/about-us" },
{ "title": "Contact", "href": "/contact" }
]
}
}{
"logo": {
"title": "Header Logo.png",
"url": "https://images.contentstack.io/v3/assets/Header_Logo.png",
"$": {
"url": "header.blt8tvsk328dbw258.en-us.logo",
"title": "header.blt8tvsk328dbw258.en-us.logo.title"
}
},
"navigation_links": {
"link": [
{
"title": "Home",
"href": "/",
"$": {
"title": "header.blt8tvsk328dbw258.en-us.navigation_links.link.0.title",
"href": "header.blt8tvsk328dbw258.en-us.navigation_links.link.0.href"
}
},
{
"title": "Menu",
"href": "/menu",
"$": {
"title": "header.blt8tvsk328dbw258.en-us.navigation_links.link.1.title",
"href": "header.blt8tvsk328dbw258.en-us.navigation_links.link.1.href"
}
},
{
"title": "About Us",
"href": "/about-us",
"$": {
"title": "header.blt8tvsk328dbw258.en-us.navigation_links.link.2.title",
"href": "header.blt8tvsk328dbw258.en-us.navigation_links.link.2.href"
}
},
{
"title": "Contact",
"href": "/contact",
"$": {
"title": "header.blt8tvsk328dbw258.en-us.navigation_links.link.3.title",
"href": "header.blt8tvsk328dbw258.en-us.navigation_links.link.3.href"
}
}
]
}
}{content_type_uid}.{entry_uid}.{locale}.{field_uid}<img {...logo.$.url}="" src="{logo?.url}" alt="Logo">For complete Live Preview code with Live Edit tags, please check out the livePreview Branch by executing the below command:
git checkout live-preview