Frameworks

Use Nuxt with Prismic

Last updated

Overview

Prismic has a first-party Nuxt integration that supports all of Prismic’s features:

Here’s what a Prismic page looks like in Nuxt:

pages/[uid].vue
<script setup lang="ts">
import { components } from "~/slices";

// 1. Fetch a page from Prismic
const prismic = usePrismic();
const route = useRoute();
const { data: page } = await useAsyncData(route.params.uid as string, () =>
  prismic.client.getByUID("page", route.params.uid as string),
);
</script>

<template>
  <main>
    <!-- 2. Display the page's slices -->
    <SliceZone :slices="page?.data.slices ?? []" :components="components" />
  </main>
</template>

Nuxt websites use @nuxtjs/prismic, @prismicio/client, and @prismicio/vue.

Set up a Nuxt website

Prismic can be added to new or existing Nuxt websites. Set up your project using the Type Builder, a tool for building by hand, or the Prismic CLI, a tool for AI agents.

Create pages

Content writers create pages from page types, like a homepage, blog post, or landing page. Model page types by hand in the Type Builder, or with the Prismic CLI for AI agents. TypeScript types are generated automatically.

Learn how to create page types

Write page components

Each page type needs a Nuxt page component. With file-system-based routing, you create a page file at each page’s path.

The example below shows a page component for a Page page type.

pages/[uid].vue

Define routes

The Prismic CLI keeps routes in prismic.config.json in sync with your page types, whether you model in the Type Builder or with CLI commands.

Page routes are inferred from each page type’s API ID:

  • A page type named Homepage maps to /.
  • A page type named Page maps to /:uid.
  • Any other page type, like Blog Post, maps to /<api-id>/:uid (e.g. /blog-post/:uid).
prismic.config.json
{
  "repositoryName": "example-prismic-repo",
  "routes": [
    { "type": "homepage", "path": "/" },
    { "type": "page", "path": "/:uid" },
    { "type": "blog_post", "path": "/blog/:uid" }
  ]
}

Edit prismic.config.json directly to customize routes. Make sure your routes match your Nuxt file-system routes. Here are common examples:

Prismic routeNuxt file-system route
/pages/index.vue
/:uidpages/[uid].vue
/blog/:uidpages/blog/[uid].vue
/:grandparent/:parent/:uidpages/[...path].vue
Learn more about routes

Create slices

Content writers build pages from reusable sections called slices, like a block of text, a hero, or a call to action. Model slices by hand in the Type Builder, or with the Prismic CLI for AI agents. TypeScript types are generated automatically.

Learn how to create slices

Write Vue components

The Prismic CLI generates a starter component in ~/slices/<SliceName>/index.vue. Edit the component to add your implementation.

The following example Call to Action component displays a rich text field and a link.

~/slices/CallToAction/index.vue
<script setup lang="ts">
import type { Content } from "@prismicio/client";

defineProps(getSliceComponentProps<Content.CallToActionSlice>());
</script>

<template>
  <section class="flex flex-col gap-4 p-8">
    <PrismicRichText :field="slice.primary.text" />
    <PrismicLink :field="slice.primary.link" class="button" />
  </section>
</template>

Learn how to display content

Fetch content

Use @prismicio/client and its methods to fetch page content.

The Prismic client

The Prismic CLI keeps your prismic.config.json in sync with the Type Builder. @nuxtjs/prismic reads from it via the clientConfig option in nuxt.config.ts, which centralizes your client configuration.

nuxt.config.ts
import prismicConfig from "./prismic.config.json";

export default defineNuxtConfig({
  modules: ["@nuxtjs/prismic"],
  prismic: {
    endpoint: prismicConfig.repositoryName,
    clientConfig: {
      routes: prismicConfig.routes,
    },
  },
});

Fetch content in pages and slices

Call the usePrismic composable to access the client. Use the client to fetch content. The following example fetches content for a /[uid] dynamic route.

~/pages/[uid].vue
<script setup lang="ts">
import { components } from "~/slices";

const prismic = usePrismic();
const route = useRoute();
const { data: page } = await useAsyncData(route.params.uid as string, () =>
  prismic.client.getByUID("page", route.params.uid as string),
);
</script>

<template>
  <main>
    <SliceZone :slices="page?.data.slices ?? []" :components="components" />
  </main>
</template>

You can fetch content in slices the same way. The following example fetches a Settings page.

~/slices/ContactForm/index.vue
<script setup lang="ts">
import { components } from "~/slices";

const prismic = usePrismic();
const { data: page } = await useAsyncData("settings", () =>
  prismic.client.getSingle("settings"),
);

// ...
</script>

Learn more about fetching content

Secure with an access token

Published content is public by default. You can require a private access token to secure the API.

Learn more about content visibility

  • Generate an access token

    npx prismic token create

    Save the new access token as an environment variable in a .env file.

    .env
    NUXT_PUBLIC_PRISMIC_ACCESS_TOKEN=my-access-token

    Access tokens can also be managed in your repository at SettingsAPI & Security.

  • Pass the access token to your client

    Provide the token via the accessToken option in clientConfig:

    nuxt.config.ts
    export default defineNuxtConfig({
      modules: ["@nuxtjs/prismic"],
      prismic: {
        endpoint: prismicConfig.repositoryName,
        clientConfig: {
          accessToken: process.env.NUXT_PUBLIC_PRISMIC_ACCESS_TOKEN, 
          routes: prismicConfig.routes,
        },
      },
    });
  • Change your repository’s API access

    Set the API access to private. Content API requests will immediately require an access token.

    npx prismic repo set-api-access private

    API visibility can also be managed in your repository at SettingsAPI & Security.

Display content

Display content using @prismicio/vue. Here are the most commonly used components:

All Prismic components are automatically available in your Nuxt app through auto-imports.

Learn how to display content from a field

Live previews in the Page Builder

The Page Builder shows live-updating thumbnails for each slice as content writers edit.

A screenshot of a page with live previews in the Page Builder.

A page with live previews in the Page Builder.

Live previews are powered by the slice simulator, a special route that renders individual slices. The Prismic CLI creates the simulator page at pages/slice-simulator.vue when you run prismic init.

Tell Prismic where your simulator is running so the Page Builder can load it:

npx prismic preview set-simulator http://localhost:3000

The simulator URL can also be set from any document. Click the ”” button next to the Publish/Unpublish button in the top-right corner and select Live preview settings.

Once your website is deployed, update the simulator URL to your production domain.

Preview draft content

Full-website previews let content writers see draft content on your website before publishing. The @nuxtjs/prismic module registers the /preview route automatically once it’s added to nuxt.config.ts.

Add a development preview URL so you can preview drafts locally:

npx prismic preview add http://localhost:3000/preview --name Development

Once deployed, add your production URL as a second preview. Previews can also be managed at SettingsPreviews.

Deploy

Deploy your Nuxt website with your hosting provider:

Handle content changes

If you deploy your website with nuxt generate, it needs to rebuild when content changes in Prismic. Follow the instructions in our webhooks documentation for your hosting provider.

Register the rebuild endpoint as a webhook so Prismic calls it when content is published or unpublished:

npx prismic webhook create https://example.com/your-rebuild-endpoint \
  --trigger documentsPublished \
  --trigger documentsUnpublished

Webhooks can also be managed at SettingsWebhooks.

SEO

Internationalization

Prismic supports multi-lingual websites. See Locales for details on locale management.

Was this page helpful?