preview bar

This commit is contained in:
Joel Varty
2021-06-23 10:21:22 -04:00
parent 4929032e5f
commit 00922a8c7d
11 changed files with 396 additions and 139 deletions

9
pages/api/exitPreview.js Normal file
View File

@@ -0,0 +1,9 @@
export default async (req, res) => {
// Clears the preview mode cookies.
// This function accepts no arguments.
res.clearPreviewData()
// Redirect to the slug
res.writeHead(307, { Location: req.query.slug })
res.end()
}

View File

@@ -0,0 +1,12 @@
import { generatePreviewKey } from "@agility/nextjs/node"
export default async (req, res) => {
//TODO: Only generate the preview link if you are already in Preview!
//how can I check if i'm in preview mode already?
const previewKey = generatePreviewKey();
//Return a valid preview key
res.end(previewKey)
}

35
pages/api/preview.js Normal file
View File

@@ -0,0 +1,35 @@
import { validatePreview, getDynamicPageURL } from '@agility/nextjs/node'
// A simple example for testing it manually from your browser.
// If this is located at pages/api/preview.js, then
// open /api/preview from your browser.
export default async (req, res) => {
//validate our preview key, also validate the requested page to preview exists
const validationResp = await validatePreview({
agilityPreviewKey: req.query.agilitypreviewkey,
slug: req.query.slug
});
if (validationResp.error) {
return res.status(401).end(`${validationResp.message}`)
}
let previewUrl = req.query.slug;
//TODO: these kinds of dynamic links should work by default (even outside of preview)
if(req.query.ContentID) {
const dynamicPath = await getDynamicPageURL({contentID: req.query.ContentID, preview: true, slug: req.query.slug});
if(dynamicPath) {
previewUrl = dynamicPath;
}
}
//enable preview mode
res.setPreviewData({})
// Redirect to the slug
res.writeHead(307, { Location: previewUrl })
res.end()
}