import { navigate, PageProps, useStaticQuery, graphql } from 'gatsby';
import { usePrismicPreview } from 'gatsby-source-prismic';
import React, { FC, useEffect } from 'react';
// Note that the `location` prop is taken and provided to the `usePrismicPreview` hook.
const PreviewPage: FC = ({ location }) => {
const { allSitePage } = useStaticQuery(
graphql`
{
allSitePage {
nodes {
path
}
}
}
`,
);
const allPaths = allSitePage.nodes.map(node => node.path);
const { isPreview, previewData, path } = usePrismicPreview({
// The repositoryName value from your `gatsby-config.js`.
repositoryName: 'XXXXXXXX',
});
// This useEffect runs when values from usePrismicPreview update. When
// preview data is available, this will save the data globally and redirect to
// the previewed document's page.
useEffect(() => {
// If this is not a preview, skip.
// null = Not yet determined if previewing.
// true = Preview is available.
// false = Preview is not available.
if (isPreview === false) return;
// Save the preview data to somewhere globally accessible. This could be
// something like a global Redux store or React context.
//
// We'll just put it on window.
console.log(previewData);
window.__PRISMIC_PREVIEW_DATA__ = previewData;
if (allPaths.includes(path)) {
navigate(path);
} else {
navigate('/unpublishedPreview');
}
}, [isPreview, previewData, path]);
// Tell the user if this is not a preview.
if (isPreview === false) return <div>Not a preview!</div>;
return <div>Loading preview...</div>;
};
export default PreviewPage;
Thanks for the response. I checked the files and all is implemented like the guide is instructing.
In my preview.tsx I'm trying to log previewData and Path and they are both undefined. Shouldn't this contain the previewdata from prismic? isPreview resolves to true
Hey Jorg, I'm not sure what could be wrong. Everything looks just fine in your config, and if you have setup previews in your repo. If you want you can send me your project via dm and I can take a look so we can try and debug this together
I'm having the same issue setting up previews, except I'm not using Gatsby Cloud. I believe this is a Prismic issue as the plugin looks for token and documentId query strings in the url, but what is in the url is previewId, document and version. As you can see in this link (gatsby-source-prismic/usePrismicPreview.ts at c86470cf30a51dfd8607106d15e93f2521b87d80 · angeloashmore/gatsby-source-prismic · GitHub), the function in lines 124-133 is looking for parameters that aren't coming back from Prismic. The useEffect in 138-144 sets isPreview to false as a result.