Skip to content

Commit

Permalink
setup prismic preview on article page
Browse files Browse the repository at this point in the history
  • Loading branch information
iamsainikhil committed Dec 6, 2020
1 parent b8ba8fe commit efca6a2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
5 changes: 5 additions & 0 deletions components/Head.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Head = ({
description,
image,
}) => {
const prismicRepo = process.env.NEXT_PUBLIC_PRISMIC_REPO
const twitterHandle = '@iamsainikhil12'
const siteName = 'NextJS Prismic Blog Starter'
const pageTitle = `${title} | ${page} | ${siteName}`
Expand Down Expand Up @@ -64,6 +65,10 @@ const Head = ({
<meta name='msapplication-TileColor' content='#00aba9' />
<meta name='theme-color' content='#333' />
<title>{pageTitle}</title>
<script
async
defer
src={`https://static.cdn.prismic.io/prismic.js?new=true&repo=${prismicRepo}`}></script>
{children}
</NextHead>
</>
Expand Down
6 changes: 6 additions & 0 deletions pages/api/exit-preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default async (_, res) => {
res.clearPreviewData()

res.writeHead(307, {Location: '/'})
res.end()
}
37 changes: 37 additions & 0 deletions pages/api/preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Prismic from 'prismic-javascript'
import {linkResolver} from '../../prismic-configuration'

const apiEndpoint = process.env.PRISMIC_API_URL
const accessToken = process.env.PRISMIC_TOKEN

// Client method to query from the Prismic repo
const Client = (req = null) =>
Prismic.client(apiEndpoint, createClientOptions(req, accessToken))

const createClientOptions = (req = null, prismicAccessToken = null) => {
const reqOption = req ? {req} : {}
const accessTokenOption = prismicAccessToken
? {accessToken: prismicAccessToken}
: {}
return {
...reqOption,
...accessTokenOption,
}
}

const Preview = async (req, res) => {
const {token: ref, documentId} = req.query
const redirectUrl = await Client(req)
.getPreviewResolver(ref, documentId)
.resolve(linkResolver, '/')

if (!redirectUrl) {
return res.status(401).json({message: 'Invalid token'})
}

res.setPreviewData({ref})
res.writeHead(302, {Location: `${redirectUrl}`})
res.end()
}

export default Preview
17 changes: 13 additions & 4 deletions pages/article/[uid].js
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,27 @@ export default function Article({uid, tags, article, author, articles}) {
)
}

export async function getStaticProps({params}) {
export async function getStaticProps({
params,
preview = null,
previewData = {},
}) {
const {uid} = params
const {tags, data: article} = await client.getByUID('article', uid)
const {ref} = previewData
const {tags, data: article} = await client.getByUID(
'article',
uid,
ref ? {ref} : null
)
// get authorID
const authorId = await article?.author?.id
// fetch author data based on authorId
const {data: author} = await client.getByID(authorId)
const {data: author} = await client.getByID(authorId, ref ? {ref} : null)
const {results: articles} = await client.query(
Prismic.Predicates.at('document.type', 'article')
)
return {
props: {uid, tags, article, author, articles},
props: {uid, tags, article, author, articles, preview},
}
}

Expand Down

0 comments on commit efca6a2

Please sign in to comment.