@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 slice zones with custom components.
This page documents all APIs provided by @prismicio/vue
.
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",
});
endpoint
string
A Prismic repository name (recommended) or full endpoint.
clientConfig
object
Options for @prismicio/client
used to create a client. Only one of clientConfig
or client
can be
used.
client
PrismicClient
An explicit @prismicio/client
’s
client for the plugin to use directly instead of creating one. Only one of
clientConfig
or client
can be used.
linkResolver
(field: ContentRelationshipField) => string | null | undefined
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
clientConfig.routes
option.
injectComponents
boolean
Whether to inject Prismic components globally into the Vue app.
true
components
object
Options to configure @prismicio/vue
’s components.
linkRel
string | function
A rel
attribute’s value to apply on links rendered by PrismicLink
.
A function can be provided to use the link’s metadata to determine the
rel
value.
({isExternal}) => isExternal ? "noreferrer" : undefined
linkInternalComponent
Component | string
A component used by PrismicLink
to render internal links.
RouterLink || NuxtLink
linkExternalComponent
Component | string
A component used by PrismicLink
to render external links.
"a" || NuxtLink
imageWidthSrcSetDefaults
number[]
Default widths to use when rendering an image with PrismicImage
’s
widths="defaults"
option.
[640, 828, 1200, 2048, 3840]
imagePixelDensitySrcSetDefaults
number[]
Default pixel densities to use when rendering an image with
PrismicImage
’s pixel-densities="defaults"
option.
[1, 2, 3]
richTextComponents
VueRichTextSerializer
A map of rich text block types to Vue components to use with
PrismicRichText
.
sliceZoneDefaultComponent
Component
Rendered if a component mapping from the SliceZone
’s components
prop cannot be found.
usePrismic
Returns the @prismicio/client
’s client and helpers injected by @prismicio/vue
.
const prismic = usePrismic();
Components
<PrismicImage>
Displays an optimized image from an image field.
<PrismicImage :field="slice.primary.image" />
field
ImageField
alt
""
fallbackAlt
""
Declare an image as decorative if the image field does not have alt text.
widths
number[] | "thumbnails" | "defaults"
Widths used to build a srcset
value for the image field. -
"thumbnails"
uses the image field’s thumbnails. - "defaults"
uses the
plugin’s components.imageWidthSrcSetDefaults
value. Only one of widths
or pixelDensities
can be used.
"defaults"
pixelDensities
number[] | "defaults"
Widths used to build a srcset
value for the image field. - "defaults"
uses the plugin’s components.imagePixelDensitySrcSetDefaults
value. Only
one of widths
or pixelDensities
can be used.
<PrismicLink>
Displays a link from a link field or a document.
<PrismicLink :field="slice.primary.link" />
field
LinkField
A link field to link. Only one of field
or document
can be used.
document
PrismicDocument
A Prismic document to link. Only one of field
or document
can be used.
linkResolver
(field: ContentRelationshipField) => string | null | undefined
A link resolver function used to
determine a URL from the field
or document
prop. If it returns a
value, it takes priority over a route resolver. Prefer
using only a route resolver with the plugin’s
clientConfig.routes
option.
rel
undefined | string | ((metadata) => string | undefined)
A rel
attribute’s value to apply on the link. A function can be provided
to use the link’s metadata to determine the rel
value. Use undefined
to remove the rel
attribute.
"noreferrer"
if the link is external.internalComponent
Component | string
The component used to render internal links.
RouterLink || NuxtLink
externalComponent
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" />
field
RichTextField
fallback
string
Rendered if the rich text field is empty.
separator
string
Separator used between text blocks.
" "
wrapper
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" />
field
RichTextField
fallback
Component
Rendered if the rich text field is empty.
components
VueRichTextSerializer
A map of rich text block types to Vue components. See an example.
Standard HTML elements (e.g. heading1
becomes a <h1>
).
linkResolver
(field: ContentRelationshipField) => string | null | undefined
A link resolver function used to
determine a URL from the field
or document
prop. If it returns a
value, it takes priority over a route resolver. Prefer
using a route resolver with the plugin’s
clientConfig.routes
option instead.
wrapper
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" />
field
EmbedField
wrapper
Component | string
An HTML tag name or a component used to wrap the output.
"div"
<PrismicTable>
Displays a formatted table from a table field.
<PrismicTable :field="slice.primary.my_table_field" />
field
TableField
fallback
Component
Rendered if the table field is empty.
components
VueTableComponents & VueRichTextSerializer
A map of table elements and and rich text block types to Vue components.
The available table elements are table
, thead
, tbody
, tr
, th
,
and td
. The available rich text block types are paragraph
, em
,
strong
, and hyperlink
. See an
example.
<SliceZone>
Renders slices from a slice zone.
<SliceZone :slices="doc.data.slices" :components="components" />
slices
SliceZone
components
object
A map of slice types to Vue components.
slice
Slice
The slice object being displayed.
slices
Slice[]
All slices in the slice zone.
index
number
The index of the slice in the slice zone.
context
unknown
The data passed to SliceZone
’s context
prop.
defaultComponent
Component
Rendered if a slice does not have a component mapping.
context
any
Arbitrary data made available to all slice components.
wrapper
Component | string
An HTML tag name or a component used to wrap the output. The output is not wrapped by default.
Prop helpers
getSliceComponentProps
Returns prop types for a slice component. Components rendered by <SliceZone>
should use this helper.
defineProps(getSliceComponentProps());
getRichTextComponentProps
Returns prop types for a rich text field block component. Components rendered by <PrismicRichText>
should use this helper.
defineProps(getRichTextComponentProps(type));
type
string
The component’s rich text node type.
getTableComponentProps
Returns prop types for a table field component. Components rendered by <PrismicText>
should use this helper.
defineProps(getTableComponentProps.table());
defineProps(getTableComponentProps.thead());
defineProps(getTableComponentProps.tbody());
defineProps(getTableComponentProps.tr());
defineProps(getTableComponentProps.th());
defineProps(getTableComponentProps.td());
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 /* [!code --] */" :components="components /* [!code ++] */" />
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/* [!code --] */" :document="aboutUsDocument/* [!code ++] */" />
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' /* [!code --] */" :rel="({ isExternal }) => isExternal ? 'noreferrer' : undefined /* [!code ++] */" />
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 /* [!code --] */" :serializer="serializer /* [!code ++] */" />
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 >;