Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addendum to Nextjs draft guide that uses branch name for preview data #1633

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions content/guides/tinacms/contextual-drafts/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,68 @@ export default App
Now when an editor logs in they will enter preview mode and be able to contextual edit draft documents.

You can see the [final result here](https://github.com/tinacms/tina-barebones-starter-preview-mode) and if you want to learn more about preview mode [see the Next.js docs](https://nextjs.org/docs/advanced-features/preview-mode).

## Working with editorial workflows

If you're using [editorial workflows](https://tina.io/docs/drafts/editorial-workflow/), you'll likely want to ensure that the preview data is fetching content from the branch
you're editing. To enable this, subscribe to the `branch:change` event via the `cmsCallback` function in the config:

```ts
// tina/config.ts
import { defineConfig } from 'tinacms'

export default defineConfig({
// ...
cmsCallback: (cms) => {
cms.events.subscribe('branch:change', async ({ branchName }) => {
console.log(`branch change detected. setting branch to ${branchName}`)
return fetch(`/api/preview/change-branch?branchName=${branchName}`)
})
return cms
},
})
```

Add another api endpoint at `/api/preview/change-branch`, which only updates the branch data if we're
in preview mode:

```ts
// api/preview/change-branch
export default function handler(req, res) {
if (req.preview && req.query?.branchName) {
res.setPreviewData({ branch: req.query.branchName })
return res.status(200).json({ message: 'Success' })
}
return res.status(403).json({ message: 'Unauthorized' })
}
```

Update our request to include the branch (if provided) in `getStaticProps`:

```ts
export const getStaticProps = async ({
params,
preview = false,
previewData = {},
}) => {
const { data, query, variables } = await client.queries.post(
{
relativePath: params.slug + '.md',
},
{
branch: preview && previewData?.branch,
}
)

return {
// the post is not found if its a draft and the preview is false
notFound: data?.post?.draft && !preview,
props: {
preview,
data,
query,
variables,
},
}
}
```