• API References

@prismicio/svelte - v2

Overview

@prismicio/svelte is the official Prismic package for creating Svelte-based websites.

With this package, you can:

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} />
prop
Description
Default
field
ImageField
An image field to display.
imgixParams
optional
object

An object of imgix URL parameters. See an example.

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 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
optional
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.

Displays a link from a link field or document.

<PrismicLink field={slice.primary.link} />
prop
Description
Default
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
optional
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} />
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.

" "

<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.

components
optional
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} />
prop
Description
Default
field
TableField

A table field to render.

components
optional
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
optional
Component

Rendered if the table field is empty.

<PrismicEmbed>

Displays embedded content from an embed field.

<PrismicEmbed field={slice.primary.embed} />
prop
Description
Default
field
EmbedField

An embed field to render.

<SliceZone>

Renders slices from a slice zone.

<SliceZone slices={page.data.slices} {components} />
prop
Description
Default
slices
SliceZone
A slice zone to render.
components
Record<string, Component<SliceComponentProps>>

A map of slice types to Svelte 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.

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" />
prop
Description
Default
repositoryName
string
The Prismic repository name.
routePrefix
optional
string

The route parameter prefixed during preview sessions.

"preview"

routePrefixName
optional
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 });
parameter
Description
Default
client
Client
A Prismic 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 });
parameter
Description
Default
client
Client
A Prismic client.
request
Request

The request object from a +server.ts file.

cookies
Cookies

The cookies object from a +server.ts file.

defaultURL
optional
string

The default redirect URL if a URL cannot be determined for the previewed document.

/
routePrefix
optional
string

The route parameter prefixed during preview sessions..

"preview"

Upgrade from v1

Follow these steps to upgrade an existing project to @prismicio/svelte v2.