Netlify CMS & Next.js Series - Custom Previews

Senior Full-Stack Developer with over 20 years of experience building web applications for startups, government, and higher education.
Search for a command to run...

Senior Full-Stack Developer with over 20 years of experience building web applications for startups, government, and higher education.
No comments yet. Be the first to comment.
This will be a series of posts were I setup a simple brochure site in Next.js driven by the Netlify CMS.
You'll sometimes want CMS users to have more control over the text content so they can style it any way they like. To allow this Netlify CMS has the markdown widget but you'll then need to render that content in JSX. Let's change the Hero title to ma...
Writing this up so I have somewhere to quickly grab basic Jest setup. This information is copied from the Jest Documentation Site compressed into just the parts I'm interested in. Create folders mkdir -p <project>/src cd <project> Install and init n...

In this blog post, we'll explore the process of setting up and configuring a Postgres database using Docker and seeding it with test data. This approach can be used for a variety of apps and can be easily adapted for your own needs. We'll cover all t...

Continuing from last time, we will explore the concepts behind Create and Update functionality and provide a step-by-step guide on how to implement it in your SvelteKit application. Let's get started! Server & Client Code In SvelteKit, the server-sid...

Introduction In this post, I will explore how to create a REST API using Spring Boot and Kotlin. We will dive into the fundamentals of Spring Boot and Kotlin, explain how to set up the project, and guide you through the process of building the API st...

Introduction Welcome to the latest post in my series of Koan Battles using Kotlin and TypeScript. In this series, we'll complete a Koan in both languages and then discuss the differences between the two solutions. My goal is to help you gain a deeper...

We can now edit data using Netlify CMS, while its functional I'm sure you'll agree its not exactly pretty!

Lets add some Tailwind CSS to show a Navbar and a Hero section with a custom preview so it looks as if we're editing the actual page when we're in the CMS.
We'll end up with the slightly prettier:

If we take a look at the new design in the CMS we'll see that admin preview is still not great.

Let's change that so the admin preview looks like the actual rendered page.
To style the Admin preview we need to reference the main site CSS from the Admin pages. We can generate a CSS file using the Tailwind command line tool.
Add a script to the package.json file so we can easily generate the CSS and hook it into the Netlify deploy process using the export command.
filename: package.json
"scripts": {
...
"generate-admin-css": "npx tailwindcss build styles/tailwind.css -o public/admin/main.css",
"export": "yarn generate-admin-css && yarn build && next export"
...
},
We can now easily generate this file using the command:
yarn generate-admin-css
Once the CSS file is generated we need to reference it in our Admin page.
filename: pages/admin.tsx
...
import("netlify-cms-app").then((CMS: any) => {
CMS.registerPreviewStyle("/admin/main.css");
...
With the CSS is in place the final step we need a preview component for the Index page:
filename: components/admin/IndexPreview.tsx
import React from "react";
import Index from "../../pages";
export const IndexPreview = ({ entry }) => {
const data = entry.getIn(["data"]).toJS();
if (data) {
return <Index hero={data.hero} />;
} else {
return <div>Loading...</div>;
}
};
Finally add the preview component to the Admin page:
filename: pages/admin.tsx
import React from "react";
import dynamic from "next/dynamic";
import { IndexPreview } from "../components/admin/IndexPreview";
const AdminWithNoSSR = dynamic(
() =>
import("netlify-cms-app").then((CMS: any) => {
CMS.registerPreviewStyle("/admin/main.css");
**CMS.registerPreviewTemplate("index", IndexPreview);**
CMS.init();
}) as any,
{ ssr: false }
);
export default AdminWithNoSSR;
We should finally have a pretty live preview:
