I have a custom type in Prismic called article that has over 100 documents. My application is a Gatsby project where within my gatsby-node.js
file I make a request for all article documents from Prismic then have a for loop that creates the relevant page for each article based on a template file. I'm trying to get Prismic previews working but so far no luck. Here's all the information:
Prismic
Project Structure
gatsby-node.js
const path = require('path');
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const newsPages = await graphql(`
{
allPrismicArticle {
nodes {
uid
}
}
}
`);
newsPages.data.allPrismicArticle.nodes.forEach((node) => {
createPage({
path: `/news/${node.uid}`,
component: path.resolve('src/templates/article.jsx'),
context: {
uid: node.uid,
},
});
});
}
gatsby-config.js
const { linkResolver } = require('./src/utils/LinkResolver');
const articleSchema = require('./src/schemas/article.json');
module.exports = {
plugins: [
{
resolve: 'gatsby-plugin-prismic-previews',
options: {
repositoryName: process.env.GATSBY_PRISMIC_REPO_NAME,
accessToken: process.env.PRISMIC_API_KEY
},
},
{
resolve: 'gatsby-source-prismic',
options: {
repositoryName: process.env.GATSBY_PRISMIC_REPO_NAME,
accessToken: process.env.PRISMIC_API_KEY,
linkResolver,
schemas: {
article: articleSchema,
},
},
},
],
};
LinkResolver.js
exports.linkResolver = (doc) => {
if (doc.type === 'article') {
return `/news/${doc.uid}`;
}
// Backup for all other types
return '/'
}
404.jsx
import * as React from 'react';
import { withPrismicUnpublishedPreview, componentResolverFromMap } from 'gatsby-plugin-prismic-previews'
import { linkResolver } from '../utils/LinkResolver'
import { Layout } from '../components';
import HomeTemplate from './index'
import NewsTemplate from '../templates/article'
const NotFoundPage = () => (
<Layout>
<h1>Empty</h1>
</Layout>
);
export default withPrismicUnpublishedPreview(
NotFoundPage,
[
{
repositoryName: process.env.GATSBY_PRISMIC_REPO_NAME,
linkResolver,
componentResolver: componentResolverFromMap({
homepage: HomeTemplate,
article: NewsTemplate,
}),
},
],
);
preview.jsx
import * as React from 'react'
import { withPrismicPreviewResolver } from 'gatsby-plugin-prismic-previews'
import { linkResolver } from '../utils/LinkResolver'
const PreviewPage = () => {
return (
<div>
<h1>Loading preview…</h1>
</div>
)
}
export default withPrismicPreviewResolver(PreviewPage, [
{
repositoryName: process.env.GATSBY_PRISMIC_REPO_NAME,
linkResolver,
},
]);
article.jsx
import React from 'react';
import { graphql } from 'gatsby';
import { withPrismicPreview } from 'gatsby-plugin-prismic-previews';
import Layout from '../components/Layout';
import { linkResolver } from '../utils/LinkResolver';
const Article = ({ data }) => {
return (
<Layout>
<p>{data.body.raw}</p>
</Layout>
);
};
export const articleQuery = graphql`
query ArticleQuery($uid: String!) {
prismicArticle(uid: {eq: $uid}) {
_previewable
data {
body {
raw
}
}
}
}
`;
export default withPrismicPreview(Article, [
{
repositoryName: process.env.GATSBY_PRISMIC_REPO_NAME,
linkResolver,
},
]);
And yet when I create a new article document in Prismic, save (without publishing it) and hit preview, I get taken to this Gatsby 404 page:
I see the Prismic preview toolbar, I see the preview page loading text before being rerouted to the right endpoint but still 404. What am I doing wrong?