Frequently Asked Questions

Preview Mode with Remix & Hygraph

What is Preview Mode in the context of Remix and Hygraph?

Preview Mode allows content editors to view draft content on their website before publishing it live. With Hygraph and Remix, you can set up a workflow where editors can preview changes in real time, ensuring content accuracy and layout before going public. (Source)

How do I set up a Remix application for use with Hygraph?

You can create a Remix application using the CLI by running npx create-remix@latest. Choose "Just the basics" and TypeScript when prompted. This sets up the foundation for integrating Hygraph and implementing Preview Mode. (Source)

How do I connect my Remix app to Hygraph's GraphQL API?

Install graphql-request and graphql packages, then create a GraphQL client in your app using your Hygraph endpoint. Store the endpoint in an environment variable and use it to initialize the client for API requests. (Source)

What environment variables are required for integrating Hygraph with Remix?

You need to set HYGRAPH_ENDPOINT, HYGRAPH_DEV_TOKEN, HYGRAPH_PROD_TOKEN, and PREVIEW_SECRET in your .env file. These variables store your API endpoint, development and production tokens, and the secret for preview mode authentication. (Source)

How do I display all articles from Hygraph in my Remix app?

Define a GraphQL query to fetch articles, use the loader function in Remix to request data from Hygraph, and render the articles in your component. Ensure you use the correct API token based on preview mode status. (Source)

How do I set up dynamic routes for articles in Remix with Hygraph?

Create a dynamic route file (e.g., $slug.tsx) in your Remix routes folder. Use a GraphQL query to fetch article data based on the URL slug and render the content using the data returned from Hygraph. (Source)

How can I render rich text content from Hygraph in Remix?

Install the @hygraph/rich-text-react-renderer package and use the RichText component to render rich text fields from Hygraph. This handles complex content structures out of the box. (Source)

How does the Preview Mode authentication work in Remix with Hygraph?

Preview Mode uses a secret token and cookies. When a preview link is accessed, the app checks the secret and slug, sets a cookie if valid, and serves draft content. The cookie is checked on each request to determine if preview mode is active. (Source)

How do I implement a banner to indicate Preview Mode is active?

Create a PreviewBanner component and display it conditionally based on the isInPreview variable returned from your loader. This provides visual feedback to users that they are viewing draft content. (Source)

How can I exit Preview Mode in a Remix app using Hygraph?

Implement an API route (e.g., /api/exit-preview) that resets the preview cookie and redirects to the homepage. Use a form with a POST action to trigger this route, ensuring all content is refreshed to the published state. (Source)

Where can I find the complete source code for the Remix Preview Mode example?

The full source code for the Remix Preview Mode example is available on GitHub. You can review all commits and implementation details there. (Source)

How do I configure preview links in Hygraph for use with Remix?

In Hygraph, configure Preview URLs for your schema (e.g., Article) by specifying the application domain, secret, and slug. The preview link should point to your app's preview API route with the appropriate query parameters. (Source)

What is the purpose of the parseCookie utility in the Remix-Hygraph integration?

The parseCookie utility parses cookies from incoming requests, allowing the app to determine if Preview Mode is active by checking the value of the preview cookie. (Source)

Why is a form used to exit Preview Mode instead of a link?

Remix optimizes data loading by only fetching data for changed routes. Using a form with a POST action ensures a full reload, updating all content (including parent routes) to reflect the exit from Preview Mode. (Source)

How can I get help if I have questions about setting up Preview Mode?

You can ask questions in the Hygraph community Slack or refer to the official documentation and GitHub repository for support. (Source)

Is there a demo available for the Remix Preview Mode integration?

Yes, you can try the live demo at https://remix-preview-mode.vercel.app/ and test preview mode functionality directly. (Source)

What are the main steps to enable Preview Mode with Hygraph and Remix?

The main steps include: setting up a Remix app, integrating Hygraph's GraphQL API, configuring environment variables, creating preview and exit-preview API routes, managing cookies for preview state, and updating loaders to serve draft or published content based on preview status. (Source)

How do I secure my Preview Mode implementation?

Use a secret token stored in an environment variable and validate it in your preview API route. Only set the preview cookie if the secret and slug are valid, preventing unauthorized access to draft content. (Source)

Can I customize the duration of the preview cookie in Remix?

Yes, you can set the maxAge attribute when creating the preview cookie to specify how long the preview mode should last. (Source)

What happens if the preview secret or slug is invalid?

If the secret or slug is invalid, the preview API route returns a 401 error and does not set the preview cookie, preventing access to draft content. (Source)

How do I update my Remix loaders to support Preview Mode?

Update your loader functions to check for preview mode by parsing the preview cookie. Use the development or production API token accordingly to fetch draft or published content from Hygraph. (Source)

Where can I find more resources on integrating Hygraph with Remix?

You can find more resources in the Hygraph Documentation, the GitHub repository, and the Hygraph blog. (Source)

Features & Capabilities

What features does Hygraph offer for developers and content teams?

Hygraph provides a GraphQL-native architecture, content federation, user-friendly tools, enterprise-grade security, Smart Edge Cache, localization, asset management, and extensive integration options. These features enable fast, scalable, and flexible content management for modern digital experiences. (Source)

Does Hygraph support integration with other tools and platforms?

Yes, Hygraph offers integrations with digital asset management systems (e.g., Aprimo, AWS S3, Bynder, Cloudinary), headless commerce, PIMs, and more. Developers can also build custom integrations using SDKs and APIs. (Source)

What APIs does Hygraph provide?

Hygraph offers multiple APIs: Content API (read/write), High Performance Content API (low latency, high throughput), MCP Server API (AI assistant integration), Asset Upload API, and Management API. (Source)

How does Hygraph ensure high performance for content delivery?

Hygraph provides high-performance endpoints designed for low latency and high read-throughput. The platform actively measures API performance and offers best practices for optimization. (Source)

What technical documentation is available for Hygraph?

Hygraph offers comprehensive documentation covering API references, schema components, webhooks, AI integrations, and more. Access all resources at Hygraph Documentation.

How do customers rate the ease of use of Hygraph?

Customers frequently praise Hygraph's intuitive user interface, ease of setup, and ability for non-technical users to manage content independently. Some users note a learning curve for complex use cases, but overall feedback is positive. (Source)

What security and compliance certifications does Hygraph have?

Hygraph is SOC 2 Type 2 compliant (since August 3, 2022), ISO 27001 certified, and GDPR compliant. The platform offers enterprise-grade security features such as granular permissions, audit logs, SSO, encryption, and regular backups. (Source)

How does Hygraph handle backups and disaster recovery?

Hygraph performs regular data backups and provides instant one-click backup recovery for enterprise customers, ensuring data safety and business continuity. (Source)

What is Hygraph's approach to localization and asset management?

Hygraph supports multiple locales, advanced asset management, and integrations with leading DAM providers, making it suitable for global teams managing multilingual and multimedia content. (Source)

Pricing & Plans

What does the Hygraph Hobby plan cost and what does it include?

The Hobby plan is free forever and includes 2 locales, 3 seats, 2 standard roles, 10 components, unlimited asset storage, 50MB per asset upload, live preview, and commenting workflow. (Source)

What features are included in the Hygraph Growth plan?

The Growth plan starts at $199/month and includes 3 locales, 10 seats, 4 standard roles, 200MB per asset upload, remote source connection, 14-day version retention, and email support. (Source)

What does the Hygraph Enterprise plan offer?

The Enterprise plan offers custom pricing and includes custom limits on users, roles, entries, locales, API calls, components, version retention for a year, scheduled publishing, dedicated infrastructure, SSO, multitenancy, backup recovery, custom workflows, and dedicated support. (Source)

How can I try Hygraph before committing to a paid plan?

You can sign up for a free forever developer account or use the Hobby plan to explore Hygraph's features without any cost. (Source)

Use Cases & Benefits

Who can benefit from using Hygraph?

Hygraph is ideal for developers, product managers, content creators, marketers, solutions architects, enterprises, agencies, eCommerce platforms, media companies, technology firms, and global brands needing scalable, flexible content management. (Source)

What business impact can customers expect from using Hygraph?

Customers can expect improved operational efficiency, faster speed-to-market, reduced costs, enhanced scalability, and better customer engagement. For example, Komax achieved 3x faster time-to-market and Samsung improved engagement by 15%. (Source)

What industries are represented in Hygraph's case studies?

Industries include SaaS, marketplace, education technology, media, healthcare, consumer goods, automotive, technology, fintech, travel, food and beverage, eCommerce, agencies, gaming, events, government, consumer electronics, engineering, and construction. (Source)

Can you share specific customer success stories with Hygraph?

Yes. Samsung built a scalable API-first app, Komax achieved 3x faster time-to-market, AutoWeb increased monetization by 20%, and Voi scaled multilingual content across 12 countries. See more at Hygraph case studies.

How long does it take to implement Hygraph?

Implementation time varies by project. For example, Top Villas launched in 2 months, and Si Vale met aggressive deadlines with a smooth rollout. Hygraph offers structured onboarding and training resources for fast adoption. (Source)

What pain points does Hygraph solve for businesses?

Hygraph addresses developer dependency, legacy tech stack modernization, content inconsistency, workflow challenges, high operational costs, slow speed-to-market, scalability issues, schema evolution complexity, integration difficulties, performance bottlenecks, and localization challenges. (Source)

How does Hygraph differentiate itself from other CMS platforms?

Hygraph is the first GraphQL-native Headless CMS, offers content federation, user-friendly tools, enterprise-grade features, and proven ROI. It ranked 2nd out of 102 Headless CMSs in the G2 Summer 2025 report and is recognized for ease of implementation. (Source)

What are some use cases for Hygraph's Preview Mode?

Preview Mode is useful for content editors to review draft changes before publishing, for QA teams to validate content, and for developers to test new features or layouts with real content in a safe environment. (Source)

What support resources are available for new Hygraph users?

Hygraph provides onboarding calls, technical and content kickoffs, webinars, live streams, how-to videos, extensive documentation, and a community Slack channel for support. (Source)

Introducing Click to Edit

How to Create a Preview Mode with Remix and Hygraph

In this example, we will show you how to create and initialize a Preview Mode for your content using Remix and Hygraph.
João Pedro Schmitz

Written by João 

Apr 20, 2022
preview-mode-with-remix-graphcms

We’ve been using Next.js for a long time here at Hygraph and one of the features we love the most in Next is preview mode. It allows you to write a draft on your headless CMS and preview it immediately on your website.

In February, we released the new Hygraph Docs Starter, which allows you to create and manage documentation for your business, product, and services. The starter is built with Remix, TypeScript, GraphQL, and styled with TailwindCSS.

We chose Remix for the docs starter, as it is a Full stack web framework that focuses on the web fundamentals with an edge-first approach, without sacrificing a modern UX. We also wanted to try and use it on a real-world project.

After releasing the starter and using it, we added preview mode support for it, so content editors could quickly preview the changes, without needing to publish.

In this guide, you’ll learn how to set up a Remix application, fetch data from a GraphQL API, and configure a preview mode on the app with Hygraph.

Want to try the application before following the tutorial? See the demo. To test preview mode, click here.

#Creating the Remix Application

The easiest way to create a Remix application is by using the CLI.

npx create-remix@latest

The CLI will prompt a few questions, regarding the folder where you want to create your app, the type, and a few other things.

The only elements that are important for us in this tutorial is the type of the app, and if you want to use TypeScript or JavaScript. Make sure to select the option “Just the basics”, and also TypeScript.

? What type of app do you want to create? Just the basics
? TypeScript or JavaScript? TypeScript
? Do you want me to run `npm install`? Yes

#Creating the Hygraph Project

Since the focus here is on Remix and setting up preview mode, we won’t create a big project on Hygraph. The application we’ll build is a news website, with a page to see all the news and a dynamic route to see the news itself.

To create the Hygraph project, you can use the clone button below.

Clone project

#Integrating with the GraphQL API

To simplify the tutorial, we’ll use graphql-request, which is a straightforward GraphQL client created and maintained by our friends from Prisma.

On the Docs Starter, we decided to use [GraphQL Code Generator](https://www.graphql-code-generatoHow r.com/), which is also a powerful but simple library, created by The Guild.

Let’s get started by installing the packages.

npm add graphql-request graphql

Now, let’s create a folder named lib inside app. In the lib folder, create a file named hygraph.server.ts. This file will export the GraphQL client, with the Hygraph endpoint.

// app/lib/hygraph.server.ts
import { GraphQLClient } from "graphql-request";
const endpoint = process.env.hygraph_ENDPOINT as string;
export const hygraph = new GraphQLClient(endpoint);

#Environment Variables with Remix

On the file we created, we’ll use a HYGRAPH_ENDPOINT environment variable, which is the endpoint of our GraphQL API.

The remix dev server provides built-in support for environment variables during development, but you’ll need to configure them on your server. For more information, I recommend checking the Remix documentation.

To add the environment variable, create a .env file on the root.

touch .env

And edit the file:

HYGRAPH_ENDPOINT=

You can find the Hygraph endpoint on Project Settings -> API Access -> Content API

Hygraph API Tokens.png

If you already started your server, you’ll need to restart it.

#Displaying all the Articles

Before focusing on preview mode, let’s update the index page, so it can show all the articles. The first thing we need to do is define a "loader" function that will be called on the server before rendering to provide data.

// app/routes/index.tsx
import type { LoaderFunction } from "@remix-run/node";
export const loader: LoaderFunction = async () => {
};
// …

Inside the loader function, we need to use the GraphQL client instance to send a request. We also need to define our query that will fetch all the articles.

// app/routes/index.tsx
import type { LoaderFunction } from '@remix-run/node';
import { json } from '@remix-run/node';
import { gql } from 'graphql-request';
import { hygraph } from '~/lib/hygraph.server';
const allArticlesQuery = gql`
{
articles {
id
title
url
createdAt
}
}
`;
export const loader: LoaderFunction = async () => {
const data = await hygraph.request(allArticlesQuery);
return json({
articles: data.articles,
});
};
// …

The API will return an object with all the articles. Since all loaders must return a Response object, we can use the json function to convert the object into a JSON response. We will now use this data on our page.

// …
type Article = {
id: string;
title: string;
url: string;
createdAt: string;
};
type LoaderData = {
articles: Article[];
};
export default function Index() {
const { articles } = useLoaderData<LoaderData>();
return (
<div style={{ fontFamily: 'system-ui, sans-serif', lineHeight: '1.4' }}>
<h1>Welcome to Remix</h1>
<ul>
{articles.map((article) => (
<li key={article.id}>
<a href={`/${article.url}`}>{article.title}</a>
</li>
))}
</ul>
</div>
);
}

If you start your server using npm run dev and go to [http://localhost:3000](http://localhost:3000), you’ll see: an error! 😔

Application Error.png

That error occurs because we haven’t defined the permissions for our API. On the ‘API Access’ page on Hygraph, go to the ‘Permanent Auth Tokens’ section, and create a new token for development. The default stage will be Draft.

Hygraph Create Token.png

On the token page, in the content permissions section, you also need to initialize the default permissions.

Content API Controls.png

We will also need a production token. The process to create it is the same, but the default stage must be Published.

After creating both tokens, we need to add two new environment variables to the .env file.

# .env
HYGRAPH_ENDPOINT=<api-url>
HYGRAPH_DEV_TOKEN=
HYGRAPH_PROD_TOKEN=

The last step is updating the index.tsx loader function.

// app/routes/index.tsx
// …
export const loader: LoaderFunction = async () => {
const preview = true;
const API_TOKEN = preview
? process.env.HYGRAPH_DEV_TOKEN
: process.env.HYGRAPH_PROD_TOKEN;
const data = await hygraph.request(
allArticlesQuery,
{},
{
authorization: `Bearer ${API_TOKEN}`,
},
);
return json({
articles: data.articles,
});
};
// …

You may notice that we’re defining a preview variable as ‘true’, which controls which token we’re using. Later, we’ll implement the logic to check if we’re in preview or not, but for now, we’ll keep it this way.

Now, if you refresh the page, you’ll see all the articles. 🎉

Welcome to Remix Index.png

#Dynamic Route for the Article Page

Now that the ‘index page’ is ready, we can start working on the ‘article page’. First, we need to define a dynamic route, because we don’t want to create new files for every new article. On Remix, routes that begin with a $ character indicate that the route is dynamic. So, let’s create a file named $slug.tsx in the routes folder. Below, you can find the complete code for the page.

// app/routes/$slug.tsx
import type { LoaderFunction } from '@remix-run/node';
import { json } from '@remix-run/node';
import { useLoaderData } from '@remix-run/react';
import { gql } from 'graphql-request';
import { hygraph } from '~/lib/hygraph.server';
const getArticleQuery = gql`
query Article($url: String!) {
article(where: { url: $url }) {
title
url
content {
... on ArticleContentRichText {
json
}
}
}
}
`;
export const loader: LoaderFunction = async ({ params }) => {
const { slug } = params;
const preview = true;
const API_TOKEN = preview
? process.env.HYGRAPH_DEV_TOKEN
: process.env.HYGRAPH_PROD_TOKEN;
const data = await hygraph.request(
getArticleQuery,
{
url: slug,
},
{
authorization: `Bearer ${API_TOKEN}`,
},
);
return json(data);
};
type Article = {
title: string;
content: any;
};
type LoaderData = {
article: Article;
};
export default function Index() {
const { article } = useLoaderData<LoaderData>();
return (
<div style={{ fontFamily: 'system-ui, sans-serif', lineHeight: '1.4' }}>
<h1>{article.title}</h1>
</div>
);
}

The logic for this page is almost the same as the index route, but with a few differences.

  • On our GraphQL query, we define a $url variable, so we can filter the article based on it;

  • On the loader, to get the $slug value, we can use the params parameter, which is available in the loader function.

Rendering Rich Text content

The only thing missing on the ‘article page’ is rendering the Rich Text content. Luckily, that’s an easy task. Hygraph has a Rich Text Renderer library for React, which handles everything out of the box.

First, let’s install the package.

npm add @hygraph/rich-text-react-renderer

We also need to install the types.

npm add @hygraph/rich-text-types -D

Inside the article page, we need to update the Article type, and use the RichText component which was exported from the @hygraph/rich-text-react-renderer package.

// …
import { RichText } from '@hygraph/rich-text-react-renderer';
import type { ElementNode } from '@hygraph/rich-text-types';
// …
type Article = {
title: string;
content: {
json: {
children: ElementNode[];
};
};
};
// …
export default function Index() {
const { article } = useLoaderData<LoaderData>();
return (
<div style={{ fontFamily: 'system-ui, sans-serif', lineHeight: '1.4' }}>
<h1>{article.title}</h1>
<RichText content={article.content.json} />
</div>
);
}

If you open an article page, you’ll be able to see the content.

Article title Hygraph.png0

Are you lost? See the complete code until this part of the tutorial.

#Setting up Preview Mode

With the project ready, we can now start the preview mode logic. On both pages, we’re controlling the token based on a variable, which is set as true. However, what we really need to do is control preview mode based on a cookie. Lucily, that’s super easy to do with Remix. Before getting started, let’s understand how it will work.

With Hygraph, we can define Preview URLs for every schema. For our project, we only want to do that on the Article schema. The configuration for preview links looks like this:

remix preview urls hygraph

Let’s break it into parts. First, you’ll see that we have two preview links configurations, one for production, which we call “Preview”, and the other one for Development. The only difference between these two are the URL, but the structure will always be similar to this:

https://<your-url>/api/preview?secret=<secret>&slug=<slug>
  • <your-url> should be your application domain.

  • <secret> should be replaced with a secret token. Only your app and Hygraph will know this secret.

  • <slug> should be the path for the page that you want to preview. On Hygraph, you can use all the schema fields.

This route api/preview, will be responsible for checking if the secret matches with the one you have on your app, and it also needs to check if that slug exists in the CMS, otherwise the request should fail.

If the secret is valid and there’s a slug, we’ll set a cookie. Any requests containing this cookie on the article or index page will be considered preview mode. The token used on the API request changes, which allows us to see draft content on the app.

To get started, create a folder under routes named api and a file preview.ts inside it. This route is the one we also configured on the CMS.

// app/routes/api/preview.ts
import { gql } from 'graphql-request';
import { json, redirect } from '@remix-run/node';
import type { LoaderFunction } from '@remix-run/node';
import { hygraph } from '~/lib/hygraph.server';
const getArticleQuery = gql`
query Article($url: String!) {
article(where: { url: $url }) {
url
}
}
`;
export const loader: LoaderFunction = async ({ request }) => {
const requestUrl = new URL(request?.url);
const secret = requestUrl?.searchParams?.get('secret');
const slug = requestUrl?.searchParams?.get('slug');
// This secret should only be known to this API route and the CMS
if (secret !== process.env.PREVIEW_SECRET || !slug) {
return json({ message: 'Invalid token' }, { status: 401 });
}
// Check if the provided `slug` exists
const data = await hygraph.request(
getArticleQuery,
{
url: slug,
},
{
authorization: `Bearer ${process.env.HYGRAPH_DEV_TOKEN}`,
},
);
// If the slug doesn't exist prevent preview from being enabled
if (!data.article) {
return json({ message: 'Invalid slug' }, { status: 401 });
}
// Enable preview by setting a cookie
// …
};

In this file, we’re fetching the search params, checking if the secret matches an environment variable, and looking for the article on the CMS. If it exists, we need to set a cookie.

Remember to add the PREVIEW_SECRET on the .env file. You also need to restart your server.

Working with Cookies

To get started, create a file preview-mode.server.ts under a folder named utils. The first thing we need to do within this file is create a cookie and with Remix, that’s very easy.

// app/utils/preview-mode.server.ts
import { createCookie } from '@remix-run/node';
export const previewModeCookie = createCookie('stage', {
path: '/',
sameSite: process.env.NODE_ENV !== 'development' ? 'none' : 'lax',
secure: process.env.NODE_ENV !== 'development',
httpOnly: true,
secrets: [process.env.PREVIEW_SECRET as string],
});

The createCookie function exported from @remix-run/node package receives the cookie name as the first argument, and the cookie attributes as the second argument. You can find the complete list of cookie attributes on MDN.

As a plus, you could also define a maxAge attribute in seconds, which specifies the duration for the preview cookie to last for.

Continuing, create another file on the utils folder named parse-cookie.server.ts. This file will have only one responsibility: parsing a cookie received as an argument.

// app/utils/parse-cookie.server.ts
import type { Cookie } from '@remix-run/node';
export const parseCookie = async (request: Request, cookie: Cookie) => {
const cookieHeader = request.headers.get('Cookie');
const parsedCookie = (await cookie.parse(cookieHeader)) || {};
return parsedCookie;
};
For more information on how to work with cookies in Remix, check the docs.

Last but not least, on the api/preview.ts file, let’s add the logic for setting the cookie and redirecting to the article URL.

// app/routes/api/preview.ts
// …
export const loader: LoaderFunction = async ({ request }) => {
// …
// Enable preview by setting a cookie
const cookie = await parseCookie(request, previewModeCookie);
cookie.stage = 'draft';
return redirect(`/${data.article.url}`, {
headers: {
'Set-Cookie': await previewModeCookie.serialize(cookie),
},
});
};

Showing a Banner on Preview Mode

If you are in preview mode in the current state of the application, you won’t get any visual feedback on the application saying that you’re seeing draft content. That not ideal from a UX perspective. We also need to check on the loader function, on both pages (article and index), if we’re really in preview mode. Right now, we’re controlling that based on a variable, which is set as true.

First, let’s create a function to check if we are in preview mode. Go to the preview-mode.server.ts file, and add the following function:

// app/utils/preview-mode.server.ts
import { parseCookie } from '~/utils/parse-cookie.server';
// …
export async function isPreviewMode(request: Request) {
const cookie = await parseCookie(request, previewModeCookie);
return cookie?.stage === 'draft';
}

With the isPreviewMode function in place, we can now use it on the article and index page. The first thing we need to do is import the function, and use it on the loader. We are also going to return a isInPreview variable from the loader, so we can use it on the page to show a banner, which we will create later.

Let’s start by changing the article page.

// app/routes/$slug.tsx
// …
import { isPreviewMode } from '~/utils/preview-mode.server';
import { PreviewBanner } from '~/components/preview-banner';
// …
export const loader: LoaderFunction = async ({ params, request }) => {
const { slug } = params;
const preview = await isPreviewMode(request);
const API_TOKEN = preview
? process.env.HYGRAPH_DEV_TOKEN
: process.env.HYGRAPH_PROD_TOKEN;
const data = await hygraph.request(
getArticleQuery,
{
url: slug,
},
{
authorization: `Bearer ${API_TOKEN}`,
},
);
return json({
...data,
isInPreview: preview,
});
};
// …
type LoaderData = {
article: Article;
isInPreview: boolean;
};
export default function Index() {
const { article, isInPreview } = useLoaderData<LoaderData>();
return (
<div style={{ fontFamily: 'system-ui, sans-serif', lineHeight: '1.4' }}>
{isInPreview && <PreviewBanner />}
<h1>{article.title}</h1>
<RichText content={article.content.json} />
</div>
);
}

And now, the index page.

// app/routes/index.tsx
// …
import { isPreviewMode } from '~/utils/preview-mode.server';
import { PreviewBanner } from '~/components/preview-banner';
// …
export const loader: LoaderFunction = async ({ request }) => {
const preview = await isPreviewMode(request);
const API_TOKEN = preview
? process.env.HYGRAPH_DEV_TOKEN
: process.env.HYGRAPH_PROD_TOKEN;
const data = await hygraph.request(
allArticlesQuery,
{},
{
authorization: `Bearer ${API_TOKEN}`,
},
);
return json({
...data,
isInPreview: preview,
});
};
// …
type LoaderData = {
articles: Article[];
isInPreview: boolean;
};
export default function Index() {
const { articles, isInPreview } = useLoaderData<LoaderData>();
return (
<div style={{ fontFamily: 'system-ui, sans-serif', lineHeight: '1.4' }}>
{isInPreview && <PreviewBanner />}
<h1>Welcome to Remix</h1>
<ul>
{articles.map((article) => (
<li key={article.id}>
<a href={`/${article.url}`}>{article.title}</a>
</li>
))}
</ul>
</div>
);
}

With the pages updated, let’s create the PreviewBanner component.

// app/components/preview-banner.tsx
export function PreviewBanner() {
return (
<p style={{ fontWeight: 'bold' }}>
You&apos;re in <strong>preview mode</strong> (Content served from DRAFT)
&mdash;&nbsp;
<form action="/api/exit-preview" method="post">
<button type="submit">
Exit Preview Mode <span aria-hidden="true">&rarr;</span>
</button>
</form>
</p>
);
}

If you try the preview mode, you should see the banner on the page.

Preview Mode UX Hygraph

But you might be wondering, why are we using a <form> to exit preview mode, and not a link to an API route, like on Next.js?

Remix has a different approach when compared to Next.js, especially when we are talking about layouts and routes. When we have a parent route on our application, like a layout route, with a header where all the items are requested from the CMS, Remix will only load the data for the parts of the page that are changing on navigation.

If you click a link in the header Remix knows that the parent route will remain on the page but the child route's data will change because the url param for the document will change. With this insight, Remix will not refetch the parent route's data. To force a full reload, we can use an action, and that’s exactly what we are doing here.

This application doesn’t have any parent routes. However, if we take the docs starter for example, all the sidebar entries are coming from the CMS. When you enter preview mode, the items from the navigation will also update and show draft changes. If we use a link to exit, the parent route won’t change, and the navigation will still show the draft content until you reload. If you want to dive deeper, I recommend taking a look into this guide.

Exit Preview Mode

The last thing we need to do on our app is implementing the exit preview API route. This is very simple since the only thing it needs to do is change the cookie value and redirect to the homepage.

import { redirect } from '@remix-run/node';
import type { ActionFunction, LoaderFunction } from '@remix-run/node';
import { parseCookie } from '~/utils/parse-cookie.server';
import { previewModeCookie } from '~/utils/preview-mode.server';
export const loader: LoaderFunction = async () => {
return redirect('/');
};
export const action: ActionFunction = async ({ request }) => {
const cookie = await parseCookie(request, previewModeCookie);
cookie.stage = 'published';
return redirect(`/`, {
headers: {
'Set-Cookie': await previewModeCookie.serialize(cookie),
},
});
};

On the route, we also need to make sure to have a loader function, which redirects to the homepage, in case someone tries to access the route directly.

See all the changes we did on this preview mode section here.

I hope this post has helped you getting started with Remix and Hygraph, and setting up preview mode. The source code is available on GitHub. If you have any questions, make sure to post them to our community on Slack!

Blog Author

João Pedro Schmitz

João Pedro Schmitz

Front-End Engineer

Front-End Engineer in love with React. Learning every day.

Share with others

Sign up for our newsletter!

Be the first to know about releases and industry news and insights.