This document describes the general setup and architecture of Prismic's integration with Gatsby. This should provide a high-level overview of the flow of data and how the integration's different plugins work together. This may help new people joining the project to help out.
The Gatsby + Prismic integration is currently composed of two plugins.
- gatsby-source-prismic: Sources data from a Prismic content repository and makes it available to a Gatsby site.
- gatsby-plugin-prismic-previews: An optional enhancement that allows website content editors to preview content changes before publishing.
Note that gatsby-plugin-prismic-previews
performs previews completely
client-side and is separate from Gatsby Cloud. gatsby-source-prismic
provides
support for Gatsby Cloud and its preview service.
This plugin serves the following purposes:
- Sources content from a Prismic repository
- Provides Gatsby Cloud webhook-based builds integration
- Prepares data for
gatsby-plugin-prismic-previews
The following actions occur during Gatsby's bootstrap phase.
- Validate plugin options in the
pluginOptionsSchema
Gatsby Node API. - (Optional) Fetch Custom Type schemas and inject into plugin options.
- Create GraphQL types using schemas in the
createSchemaCustomization
Gatsby Node API. - Save Custom Type metadata to node store (used in
gatsby-plugin-prismic-previews
). - Fetch content from Prismic repository.
- Normalize document data.
- Create nodes for each document in the
sourceNodes
Gatsby Node API viacreateNodes
.
In Prismic, a user will setup two webhooks in their content repository:
- Builds: This webhook will trigger a full site rebuild.
- Gatsby Cloud Previews: This webhook will trigger a content delta update event.
The following webhook actions are for the second type of webhook (i.e. specific to Gatsby Cloud Previews).
This webhook may fire anytime a user performs one of the following editor actions, depending on the user's webhook preferences:
- Publishes a document
- Unpublishes a document
- Creates a release (a collection of changes)
- Edits a release
- Deletes a release
- Creates a tag (label for a document)
- Deletes a tag
If those actions are enabled for the webhook, the following occurs:
- Validate the webhook in the
sourceNodes
Gatsby Node API. - Fetch documents referenced in the webhook from the Prismic repository.
- Compute the necessary delta operations needed to incorporate the webhook event data (create/update/delete nodes).
- Execute delta operations.
The Gatsby node store is now equivalent to a fresh bootstrap. A full Prismic repository content fetch was not needed.
This webhook fires anytime a user clicks a "Trigger It" button for a webhook. It allows a user to test if webhooks are being received within the Gatsby site without performing any content action.
The following occurs:
- Log a success message.
No content actions are taken during this webhook as the webhook contains no delta information.
This optional plugin serves the following purposes:
- Fetches draft content from a Prismic repository client-side
- Emulates Gatsby's GraphQL API shape for Prismic content
- Recursively replaces static page data with preview data
Prismic Previews provide the following unique features:
- Unlimited concurrent preview sessions targeting different content branches
- Automatic content refreshing while editing content
- Shareable preview links
The following actions occur during Gatsby's bootstrap phase.
- Validate plugin options in the
pluginOptionsSchema
Gatsby Node API. - Save Custom Type metadata saved in
gatsby-source-prismic
's bootstrap phase to a JSON file in/public
.
The metadata JSON file will be referenced during client-side preview sessions.
The following actions occur during the client entry phase.
- Add plugin options to global state in the
onClientEntry
Gatsby Node API.
The plugin options will be referenced during client-side preview sessions.
When an editor clicks a preview button in the Prismic interface, a new window is opened that redirects to a specific page within the Gatsby site. This page is called a "preview resolver" and directs editors to the correct page within the site.
The preview resolver page performs the following actions:
- Determine if a preview session is active by checking for the presence of a Prismic-specific cookie. This cookie is set after a user clicks the preview button within the Prismic editor. Continue if present.
- Fetch the previewed document.
- Compute the document's URL using the app's Link Resolver function.
- Redirect to the document's URL.
On the document's page:
- Fetch all Prismic repository content using the preview ref, which includes all draft content.
- Normalize document data.
- Save data to a React Context store.
- Traverse static page query data and replace with preview content where matches are found.
Matching static content with preview content is determined by comparing a
Gatsby-specific _previewable
field on the static data. At the time of writing,
the _previewable
field contains the document's unique Prismic document ID, but
this could change in the future.
Client-side data is shaped nearly identically to build-time data. Because
preview data is merged into the static data, all build-time-only data, such as
siteMetadata
objects, are kept intact.
When a document is previewed but does not yet have a page within the app (e.g. an unpublished document), the following flow is used.
- After the user is redirected to the previewed document's URL, the user will land on the 404 page.
- Fetch all Prismic repository content using the preview ref, which includes all draft content.
- Normalize document data.
- Save data to a React Context store.
- Resolve the page template to render and the data to provide it using user-provided functions. The functions are given all nodes whose URLs resolve to the current URL.
- Render the resolved component with the computed
data
prop.