Preview Drafts

Configure previews in your repo

In your repository, navigate to Settings > Previews. Under "Create a New Preview," enter:

  • Site Name: A display name for the preview environment (such as "Development" or "Production").
  • Domain URL: The domain of your site (such as https://your-site.com or http://localhost:3000).
  • Link Resolver: This is a route in your app that Prismic will use to build your website's URLs.

Note that you can add multiple previews in your repository settings. For instance, you can have one preview for your website on localhost and another for your production website.

Before you proceed

First, you'll need a project with SvelteKit and Prismic. If you don't have one yet, start with the Install step.

Add the Prismic Toolbar Script

In Settings > Previews, you will find a script like this:

Copy
<script async defer src="https://static.cdn.prismic.io/prismic.js?new=true&repo=your-repo-name"></script>

The repo=your-repo-name URL parameter should include your repository name.

Include this script in your website header. To ensure that previews and shareable preview links work properly, you must include this script on every page of your website, including 404 pages. In SvelteKit, you can add this script before the closing <head> tag in src/routes/app.html. In plain Svelte, you can add it in App.svelte between <svelte:head> tags.

Preview for a web app without routing (without SvelteKit)

If your app only has one route, previews will work without any configuration in your project.

When you set up previews in your repository, leave the preview route as /preview. In the settings for your hosting provider, add a redirect from /preview to the main page of your app. When you click the preview button in the editor, this should launch a preview by default, with the Prismic toolbar appearing. Click the "x" on the Prismic toolbar to exit the preview.

Preview for a web app with routing (with SvelteKit)

Create a route called /preview.svelte (you can change the route, but you'll need to change the Link Resolver preview setting in your Prismic repository to match), and paste in this code:

Copy
<script>
    import createClient from "$lib/prismicClient"
    import { goto } from "$app/navigation"
    import { onMount } from "svelte"

    onMount(async () => {
      const client = createClient()
      const redirect = await client.resolvePreviewURL({defaultURL: "/"})
      await goto(redirect) // This does a client-side redirect to that URL
    })
</script>

What's happening in /preview?

Prismic doesn't inherently know the route structure for your website. So, to preview a document, you need to build the route for that document and forward the browser to that route.

When you click the "preview" button in the Prismic editor, the editor opens a new window at /preview in your website, and adds some information about the previewed document to the URL. When the preview route component is mounted in the browser, it resolvePreviewURL() automatically extracts the document information from the URL and uses that information to construct a route for the document. Then, goto() redirects the user to the new route.

How does your website display the previewed content? That all happens behind the scenes. When you click "preview" in the editor, Prismic sets a cookie in your browser with a special ref token to access previewed content. When your website queries content from Prismic, the @prismicio/client query methods check for that cookie. If the cookie is present, the helper method includes the ref token in the query, and the API sends back draft content.


Was this article helpful?
Not really
Yes, Thanks

Can't find what you're looking for? Spot an error in the documentation? Get in touch with us on our Community Forum or using the feedback form above.