@prismicio/svelte - v2
Overview
@prismicio/svelte
is the official Prismic package for creating Svelte-based websites.
With this package, you can:
Display slice zones with custom components.
This page documents all APIs provided by @prismicio/svelte
.
Install
@prismicio/svelte
is installed when bootstrapping a SvelteKit project with @slicemachine/init
.
npx @slicemachine/init@latest
It can also be manually installed in any Svelte project:
npm install --save @prismicio/svelte @prismicio/client
@prismicio/client
is a peer dependency and must be installed.
API
All components can be imported as named imports.
The following example imports PrismicImage
:
import { PrismicImage } from "@prismicio/svelte";
<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[640, 750, 828, 1080, 1200, 1920, 2048, 3840]
.
Only one of widths
or pixelDensities
can be used.
"defaults"
pixelDensities
number[] | "defaults"
Pixel densities used to build a srcset
value for the image field.
"defaults"
uses[1, 2, 3]
.
Only one of widths
or pixelDensities
can be used.
<PrismicLink>
Displays a link from a link field or document.
<PrismicLink field={slice.primary.link} />
field
LinkField
A link field to link. Only one of field
, document
, or href
can be
used.
document
PrismicDocument
A Prismic document to link. Only one of field
, document
, or href
can
be used.
href
string
A URL to link. Only one of field
, document
, or href
can be used.
rel
undefined | string | ((metadata) => string | undefined)
A ref
attribute. A function can be passed to determine the value based
on the link. Use undefined
to remove the rel
attribute.
"noreferrer"
if the link is external.
<PrismicText>
Displays plain text from a rich text field.
<PrismicText field={slice.primary.text} />
field
RichTextField
A rich text field to render.
fallback
string
Rendered if the rich text field is empty.
separator
string
Separator used between text blocks.
" "
<PrismicRichText>
Displays formatted text from a rich text field.
<PrismicRichText field={slice.primary.text} />
field
RichTextField
A rich text field to render.
components
object
A map of rich text block types to Svelte components. See an example.
Standard HTML elements (e.g. heading1
becomes a <h1>
).
<PrismicTable>
Displays a table from a table field.
<PrismicTable field={slice.primary.table} />
field
TableField
A table field to render.
components
object
A map of table elements and rich text block types to Svelte 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.
Standard HTML elements (e.g. table
becomes a <table>
).
fallback
Component
Rendered if the table field is empty.
<PrismicEmbed>
Displays embedded content from an embed field.
<PrismicEmbed field={slice.primary.embed} />
field
EmbedField
An embed field to render.
<SliceZone>
Renders slices from a slice zone.
<SliceZone slices={page.data.slices} {components} />
slices
SliceZone
components
Record<string, Component<SliceComponentProps>>
A map of slice types to Svelte 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.
SvelteKit API
@prismicio/svelte
provides components and functions for SvelteKit.
All SvelteKit components and functions can be imported as named imports from @prismicio/svelte/kit
.
The following example imports PrismicPreview
:
import { PrismicPreview } from "@prismicio/svelte/kit";
<PrismicPreview>
Adds support for content previews by including the Prismic toolbar and event listeners. Webpages are automatically refreshed when a new content draft is saved.
<PrismicPreview repositoryName="example-prismic-repo" />
repositoryName
string
routePrefix
string
The route parameter prefixed during preview sessions.
"preview"
routePrefixName
string
The name of the preview-specific route parameter prefixed during preview sessions.
"preview"
enableAutoPreviews()
Configures a Prismic client to automatically fetch draft content during a preview session.
enableAutoPreviews({ client, cookies });
client
Client
cookies
Cookies
The cookies
object provided to a route’s load()
function. This
parameter is required to load previews.
redirectToPreviewURL()
Redirects a content writer to a previewed document’s URL. This function should only be called in a +server
file.
redirectToPreviewURL({ client, request, cookies });
client
Client
defaultURL
string
The default redirect URL if a URL cannot be determined for the previewed document.
/
routePrefix
string
The route parameter prefixed during preview sessions..
"preview"
Upgrade from v1
Follow these steps to upgrade an existing project to @prismicio/svelte
v2.
Upgrade to Svelte 5
@prismicio/svelte
v2 requires Svelte 5. If you are using Svelte 4 or earlier, follow the official Svelte 5 migration guide.Install
@prismicio/svelte
v2npm install @prismicio/svelte@^2
Replace
SvelteRichTextSerializer
withRichTextComponents
The
SvelteRichTextSerializer
TypeScript type is deprecated to phase out the “serializer” term.The type has been renamed to
RichTextComponents
.import type { SvelteRichTextSerializer, RichTextComponents, } from "@prismicio/svelte"; const serializer: SvelteRichTextSerializer = const serializer: RichTextComponents = { paragraph: Paragraph };