-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
setup prismic preview on article page
- Loading branch information
1 parent
b8ba8fe
commit efca6a2
Showing
4 changed files
with
61 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters