@prismicio/vue - v5
Overview
@prismicio/vue
is the official Prismic package for creating Vue.js-based websites.
With this package, you can:
- Query Prismic content with a dedicated client.
- Display Prismic images, links, and rich text.
- Display slice zones with custom components.
Install
@prismicio/vue
is automatically installed when bootstrapping a Nuxt project with @slicemachine/init
.
npx @slicemachine/init@latest
It can also be manually installed in any Vue.js project:
npm install --save @prismicio/vue
Register the Vue plugin
@prismicio/vue
provides a Vue.js plugin that injects @prismicio/client
and Vue components into your project.
Configure your plugin with
createPrismic
createPrismic
configures a Vue.js plugin with your Prismic repository.prismic.tsimport { createPrismic } from "@prismicio/vue"; const prismic = createPrismic({ endpoint: "example-prismic-repo", }); export default prismic;
Register the plugin in your Vue app
The plugin needs to be registered in your Vue app.
main.tsimport { createApp } from "vue"; import App from "./App.vue"; import prismic from "./prismic"; createApp(App).use(prismic).mount("#app");
Access Prismic client and helpers
The plugin injects @prismicio/client
’s client and helpers into your Vue app. They can be accessed with usePrismic
in your component setup and $prismic
in your template sections.
<script setup lang="ts">
import { usePrismic } from "@prismicio/vue";
const {
options,
client, filter, cookie,
// All @prismicio/client helpers are available.
asText, asHTML,
} = usePrismic();
</script>
<template>
<div>
{{ $prismic.options }}
{{ $prismic.client }}
{{ $prismic.filter }}
{{ $prismic.cookie }}
<!-- All @prismicio/client helpers are available. -->
{{ $prismic.asText }}
{{ $prismic.asHTML }}
</div>
</template>
API
createPrismic
Creates a Vue.js plugin with your Prismic endpoint and additional options.
createPrismic({
endpoint: "example-prismic-repo"
})
Property | Description | Default |
---|---|---|
endpoint | string A Prismic repository name (recommended) or full endpoint. | |
clientConfig optional | object Options for | |
client optional | PrismicClient An explicit | |
linkResolver optional | function A link resolver function used to determine a URL from a link field or document. If it returns a value, it takes priority over a route resolver. Prefer using only a route resolver with the plugin’s | |
injectComponents optional | boolean Whether to inject Prismic components globally into the Vue app. | true |
components optional | object Options to configure |
components
options
Argument | Description | Default |
---|---|---|
linkRel optional | string | function A | ({ isExternal }) => isExternal ? "noreferrer" : undefined |
linkInternalComponent optional | Component | string A component used by | <RouterLink> || <NuxtLink> |
linkExternalComponent optional | Component | string A component used by | "a" || <NuxtLink> |
imageWidthSrcSetDefaults optional | number[] Default widths to use when rendering an image with | [640, 828, 1200, 2048, 3840] |
imagePixelDensitySrcSetDefaults optional | number[] Default pixel densities to use when rendering an image with | [1, 2, 3] |
richTextComponents optional | VueRichTextSerializer A map of rich text block types to Vue components to use with | |
sliceZoneDefaultComponent optional | Component Rendered if a component mapping from the | TODOSliceComponent |
usePrismic
Returns the @prismicio/client
’s client and helpers injected by @prismicio/vue
.
usePrismic()
<PrismicImage>
Displays an optimized image from an image field.
<PrismicImage :field="slice.primary.image" />
Prop | Description | Default |
---|---|---|
field | ImageField An image field to display. | |
imgixParams optional | object An object of imgix URL parameters. | |
alt optional | "" Declare an image as decorative. | |
fallbackAlt optional | "" Declare an image as decorative if the image field does not have alt text. | |
widths optional | number[] | "thumbnails" | "defaults" Widths used to build a
Only one of | "defaults" |
pixelDensities optional | number[] | "defaults" Widths used to build a
Only one of |
<PrismicLink>
Displays a link from a link field or a document.
<PrismicLink :field="slice.primary.link" />
Prop | Description | Default |
---|---|---|
field | LinkField A link field to link. Only one of | |
document | PrismicDocument A Prismic document to link. Only one of | |
linkResolver optional | function A link resolver function used to determine a URL from the | |
rel optional | string | function A | ({ isExternal }) => isExternal ? "noreferrer" : undefined |
internalComponent optional | Component | string The component used to render internal links. | <RouterLink> || <NuxtLink> |
externalComponent optional | Component | string The component used to render external links. | "a" || <NuxtLink> |
<PrismicText>
Displays plain text from a rich text field.
<PrismicText :field="slice.primary.text" />
Prop | Description | Default |
---|---|---|
field | RichTextField A rich text field to render. | |
fallback optional | string Rendered if the rich text field is empty. | |
separator optional | string Separator used between text blocks. | " " |
wrapper optional | Component | string An HTML tag name or a component used to wrap the output. The output is not wrapped by default. |
<PrismicRichText>
Displays formatted text from a rich text field.
<PrismicRichText :field="slice.primary.text" />
Prop | Description | Default |
---|---|---|
field | RichTextField A rich text field to render. | |
fallback optional | Component Rendered if the rich text field is empty. | |
components optional | VueRichTextSerializer A map of rich text block types to Vue components. | |
linkResolver optional | function A link resolver function used to determine a URL from the | |
wrapper optional | Component | string An HTML tag name or a component used to wrap the output. The output is not wrapped by default. |
<PrismicEmbed>
Displays HTML from an embed field.
<PrismicEmbed :field="slice.primary.embed" />
Prop | Description | Default |
---|---|---|
field | EmbedField An embed field to render. | |
wrapper optional | Component | string An HTML tag name or a component used to wrap the output. | "div" |
<SliceZone>
Renders slices from a slice zone.
<SliceZone :slices="doc.data.slices" :components="components" />
Prop | Description | Default |
---|---|---|
slices | SliceZone A slice zone to render. | |
components | object A map of slice types to Vue components. | |
defaultComponent optional | Component Rendered if a slice does not have a component mapping. | |
context optional | any Arbitrary data made available to all slice components. | |
wrapper optional | Component | string An HTML tag name or a component used to wrap the output. The output is not wrapped by default. |
Upgrading from v4
Follow these steps to upgrade an existing project to @prismicio/vue
v5.
Install
@prismicio/vue
v5@prismicio/vue
v5 is automatically installed when using@nuxtjs/prismic
v4.npm install --save @prismicio/vue@^5
Provide your own
fetch
polyfillisomorphic-unfetch
is no longer provided as a defaultfetch
implementation. If you still need afetch
polyfill, installisomorphic-unfetch
:npm install --save isomorphic-unfetch
Then, import and configure it with the plugin:
prismic.tsimport { createPrismic } from "@prismicio/vue"; import fetch from "isomorphic-unfetch"; const prismic = createPrismic({ endpoint: "your-repository-name", clientConfig: { fetch, }, }); export default prismic;
Replace
htmlSerializer
plugin optionUse the
richTextSerializer
option instead.prismic.tsimport { createPrismic } from "@prismicio/vue"; const prismic = createPrismic({ endpoint: "your-repository-name", htmlSerializer: serializer richTextSerializer: serializer }); export default prismic;
Replace
components.imageComponent
plugin optionUse your own image component instead with
@prismicio/client
’s helpers.<MyImageComponent v-if="$prismic.isFilled(slice.primary.image)" :src="$prismic.asImageSrc(slice.primary.image)" />
Replace
components.linkBlankTargetRelAttribute
plugin optionUse the new
components.linkRel
option.prismic.tsimport { createPrismic } from "@prismicio/vue"; const prismic = createPrismic({ endpoint: "your-repository-name", components: { linkBlankTargetRelAttribute: "noreferrer" linkRel: ({ isExternal }) => isExternal ? "noreferrer" : undefined } });
Replace
<SliceZone>
’sresolver
propUse the new
components
prop instead.<SliceZone :slices="doc.data.slices" :resolver="resolver" :components="components" />
Replace
<PrismicImage>
’simageComponent
propUse your own image component instead with
@prismicio/client
’s helpers.<MyImageComponent v-if="$prismic.isFilled(slice.primary.image)" :src="$prismic.asImageSrc(slice.primary.image)" />
Replace
<PrismicLink>
’sfield
prop when linking documentsUse the new
document
prop instead.<PrismicLink :field="aboutUsDocument" :document="aboutUsDocument" />
Update
<PrismicLink>
’srel
attribute behaviorAll external links now default to
noreferrer
. Previously only links with atarget="_blank"
attribute had a default value.You can resume the previous behavior globally using the new
components.linkRel
option.prismic.tsimport { createPrismic } from "@prismicio/vue"; const prismic = createPrismic({ endpoint: "your-repository-name", components: { linkRel: ({ isExternal }) => isExternal ? "noreferrer" : undefined } });
The function can also be passed directly to
<PrismicLink>
’srel
prop.<PrismicLink :field="slice.primary.link" :rel="({ isExternal }) => isExternal ? 'noreferrer' : undefined" />
Update
<PrismicLink>
’starget
attribute behaviorThe
target
prop is removed. Use the editor to control whether links should be opened in a new tab.Replace
<PrismicLink>
’sblankTargetRelAttribute
propUse the new
rel
prop.<PrismicLink :field="slice.primary.image" :blankTargetRelAttribute="'noreferrer'" :rel="({ isExternal }) => isExternal ? 'noreferrer' : undefined" />
Wrap
<PrismicText>
output<PrismicText>
output is no longer wrapped by default. Use thewrapper
prop to wrap the output explicitly.<PrismicText :field="slice.primary.text" wrapper="p" />
Replace
<PrismicRichText>
’shtmlSerializer
propMigrate to the new component-based serializer, or use the (now deprecated)
serializer
prop during your migration instead.<PrismicRichText :field="slice.primary.text" :htmlSerializer="serializer" :serializer="serializer" />
Wrap
<PrismicRichText>
output<PrismicRichText>
output is no longer wrapped by default. Use thewrapper
prop to wrap the output explicitly.<PrismicRichText :field="slice.primary.text" wrapper="div" />
Replace
usePrismicImage()
Use
@prismicio/client
’s helpers directly.import { usePrismicImage } from "@prismicio/vue"; const image = usePrismicImage(slice.primary.image); import { computed } from "vue"; import { usePrismic } from "@prismicio/vue"; const prismic = usePrismic(); const image = computed(() => { return prismic.asImageSrcSet(slice.primary.image); });
Replace
usePrismicLink()
Use
@prismicio/client
’s helpers directly.import { usePrismicLink } from "@prismicio/vue"; const link = usePrismicLink(slice.primary.link); import { computed } from "vue"; import { usePrismic } from "@prismicio/vue"; const prismic = usePrismic(); const link = computed(() => { return prismic.asLinkAttrs(slice.primary.link); });
Replace
usePrismicText()
Use
@prismicio/client
’s helpers directly.import { usePrismicText } from "@prismicio/vue"; const text = usePrismicText(slice.primary.text); import { computed } from "vue"; import { usePrismic } from "@prismicio/vue"; const prismic = usePrismic(); const text = computed(() => { return prismic.asText(slice.primary.text); });
Replace
usePrismicRichText()
Use
@prismicio/client
’s helpers directly.import { usePrismicRichText } from "@prismicio/vue"; const html = usePrismicRichText(slice.primary.text); import { computed } from "vue"; import { usePrismic } from "@prismicio/vue"; const prismic = usePrismic(); const html = computed(() => { return prismic.asHTML(slice.primary.text); });
Replace all query composables
Use a query library with a Prismic client, such as TanStack Query.
import { usePrismicDocumentByUID } from "@prismicio/vue"; const { data } = usePrismicDocumentByUID("page", "home"); import { usePrismic } from "@prismicio/vue"; import { useQuery } from "@tanstack/vue-query"; const prismic = usePrismic(); const { data } = useQuery({ queryKey: ["home"], queryFn: () => prismic.client.getByUID("page", "home"), });
Refer to the following table for each hook’s replacement client method.
Hook Query method usePrismicDocuments()
client.get()
useFirstPrismicDocument()
client.getFirst()
useAllPrismicDocumentsDangerously()
client.dangerouslyGetAll()
usePrismicDocumentByID()
client.getByID()
usePrismicDocumentsByIDs()
client.getByIDs()
useAllPrismicDocumentsByIDs()
client.getAllByIDs()
usePrismicDocumentByUID()
client.getByUID()
usePrismicDocumentsByUIDs()
client.getByUIDs()
useAllPrismicDocumentsByUIDs()
client.getAllByUIDs()
useSinglePrismicDocument()
client.getSingle()
usePrismicDocumentsByType()
client.getByType()
useAllPrismicDocumentsByType()
client.getAllByType()
usePrismicDocumentsByTag()
client.getByTag()
useAllPrismicDocumentsByTag()
client.getAllByTag()
usePrismicDocumentsBySomeTags()
client.getBySomeTags()
useAllPrismicDocumentsBySomeTags()
client.getAllBySomeTags()
usePrismicDocumentsByEveryTag()
client.getByEveryTag()
useAllPrismicDocumentsByEveryTag()
client.getAllByEveryTag()
Replace
DefineComponentSliceComponentProps
typeUse the return type of
getSliceComponentProps
instead.import { getSliceComponentProps } from "@prismicio/vue"; type DefineComponentSliceComponentProps = ReturnType<typeof getSliceComponentProps>;