Install Prismic

This article explains how to install and configure Prismic in a Next.js project. By the end of this page, you will have Prismic utilities installed in your app.

Quickstart from the dashboard

To start with a preconfigured project, go to prismic.io/dashboard and select Next.js.

Dashboard

Start from scratch

This guide assumes basic knowledge of React and Next.js. If you are new to development with either, see the official introductions for React and Next.js.

Create a Next.js project

Before proceeding, you will need a Next.js project. You can quickly create a brand new project using the following create-next-app command. (Read the official documentation to learn more):

npx create-next-app@latest

This command will prompt an option to name your project. After that you’ll be able to open your newly created Next.js app.

Once it’s finished, you’ll have a brand new Next.js project where you can install Slice Machine.

Run the setup command

In the root of your Next.js project, run the following command:

npx @slicemachine/init@latest

Define routes

The init script creates a file called prismicio.js at the root of your project. This file contains configurations for your project.

The most important configuration is the routes array, which is your route resolver. Update the route resolver to match the routing structure of your Next.js app. To learn more about how to add configurations, see the documentation for the route resolver and @prismicio/client.

The prismicio.js file is prefilled with an example routes array:

prismicio.js
const routes = [
  {
    type: "homepage",
    path: "/",
  },
  {
    type: "page",
    path: "/:uid",
  },
];

In prismicio.js, customize the routes array to match the routing of your project. For each page type, add an object that describes the route for that page. Learn more about how the route resolver works.

Add <PrismicPreview>

To enable previewing, add <PrismicPreview> to the root of your app:

src/app/layout.tsx
import { PrismicPreview } from "@prismicio/next";
import { repositoryName } from "@/prismicio";

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>
        {children}
        <PrismicPreview repositoryName={repositoryName} />
      </body>
    </html>
  );
}

Configure previews in your Prismic repository

Finish setting up previews by configuring your Prismic repository:

  • In your Prismic repository, go to Settings > Previews
  • Ignore the step about including the Prismic Toolbar (<PrismicPreview> adds the toolbar for you)
  • Click Create Preview
  • You’ll be prompted to input a site name, a domain for your application, and an optional preview route. Fill out the fields with the following information:
    Domain: http://localhost:3000
    Preview Route: /api/preview

You now have Prismic utilities available throughout your project, and your project is set up to handle previews.

Now you can start creating slices and custom types to model your project.