Preview Drafts

On this page, you will gain a theoretical understanding of how previews work under the hood with Prismic, and you'll configure Prismic previews in a project using Vanilla JavaScript.

Before your proceed

We strongly recommend building your app with a JavaScript framework, like Express, React, or Vue. If you choose to use vanilla JavaScript, you'll first need a project configured with Prismic. If you don't have one yet, start with the Setup step.

Set up your project

If you've completed the setup step, you know how a Vanilla JavaScript project works in both the browser and Node.js. In this guide, we'll use the browser setup, since we'll need something displayed in the browser to preview.

Ensure your client has a Route Resolver configured

Make sure to add a Route Resolver routes object to your client to define the URL structure of your project. This helps the preview to know which route to go.

Create a preview route

Create a file at the root of your project called preview.html and paste in this code:

Copy
<!DOCTYPE html>
<html>
  <head>
    <title>Preview route</title>
    <script type="module">
      import { client } from '../path-to-your-client.js'

      const init = async () => {
        const defaultURL = '/'

        const url = await client.resolvePreviewURL({
          defaultURL,
        })
        window.location.replace(url)
      }
      init()
    </script>
  </head>
  <body>
    <h1>Loading Preview...</h1>
  </body>
</html>

Inside, you will use the resolvePreviewURL() method, which will get the preview token and document ID from the preview URL and generate a URL for the document to be previewed. Then, the script redirects the browser to this URL with JavaScript's replace() method.

This /preview route will function as a middleware to redirect to the correct path when you launch a preview session from the editor.

Add the Prismic Toolbar script

Find your script in your repository's Settings > Previews and include the script to the head of every page of your website, including 404 pages:

Copy
<!DOCTYPE html>
<html>
  <head>
    <title>Preview route</title>
    <!-- Here is your Prismic Toolbar script -->
+   <script async defer src="https://static.cdn.prismic.io/prismic.js?new=true&repo=your-repo-name"></script>

    <script type="module">
      import { client } from '../client.js'

      const init = async () => {
        const defaultURL = '/'

        const url = await client.resolvePreviewURL({
          defaultURL,
        })
        window.location.replace(url)
      }
      init()
    </script>
  </head>
  <body>
    <h1>Loading Preview...</h1>
  </body>
</html>

Replace your-repo-name in the toolbar script so that it works with your repository

ℹ️ How does your website access the draft content?

Draft content is not publicly accessible on your API endpoint, so how does it appear in your preview? When you click the "preview" button in the Prismic editor, Prismic sets a cookie in your browser for the preview session. All of the query methods in @prismicio/client check for that cookie when they query content from Prismic.

If the cookie is present, the query methods will include it in the API query, and you will receive draft content for that preview session. (By default, a preview session previews the draft content in your repository. But if you preview a Release, you will see the content from that Release instead.) The Prismic Toolbar includes an "X" button to quit the preview session, finish the preview session, and delete the preview cookie. You can also do this manually in your browser dev tools.

Enable previews

Enable previews in your repository. Use /preview for the Link Resolver in the preview settings. For more information on the preview settings, read Set up Previews.

Preview your draft documents

Now go to one document in your repository, make changes, savem and click the preview button. You'll should see the draft version of your document.

Please keep in mind that with a setup like this you can only preview documents that have a corresponding HTML file.


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.