Skip to content

Commit

Permalink
feat: use sanity-plugin-iframe-pane
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Aug 14, 2023
1 parent cfdc4d9 commit 6c34d44
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 147 deletions.
3 changes: 1 addition & 2 deletions .github/renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"description": "Check the readme: https://github.com/sanity-io/renovate-presets/blob/main/ecosystem/README.md",
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"github>sanity-io/renovate-config",
"github>sanity-io/renovate-config:studio-v3"
"github>sanity-io/renovate-config:starter-template"
]
}
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ You can use this starter to kick-start a clean slate site or learn these technol
| `sanity.cli.ts` | Config file for Sanity CLI |
| `/pages/index.tsx` | Landing page for `/`. |
| `/pages/studio/[[...index]].tsx` | Where Sanity Studio is mounted |
| `/pages/api/preview.ts` | Serverless route for triggering Preview mode |
| `/pages/api/draft.ts` | Serverless route for triggering Draft mode |
| `/sanity/schemas.ts` | Where Sanity Studio gets its content types from |
| `/sanity/env.ts` | Configuration for the Sanity project and dataset |
| `/sanity/schemas.ts` | Where Sanity Studio gets its content types from |
| `/sanity/lib/client.ts` | Sanity client configured based on `env.ts` |
| `/sanity/lib/image.ts` | Sanity image builder - unused in this template, but is needed to render Sanity images |
| `/sanity/plugins/IFramePreviewView.tsx` | See [adding studio preview](docs/studio-preview.md) |
| `tailwind.config.js` | Tailwind config. Only applies to files listed under `content` |

All pages are wrapped in `pages/_document.tsx` and `pages/_app.tsx`.
Expand Down
103 changes: 95 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"sanity": "3.15.1",
"sanity-plugin-iframe-pane": "^2.5.4",
"styled-components": "5.3.11"
},
"devDependencies": {
Expand Down
40 changes: 37 additions & 3 deletions sanity.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
import { visionTool } from '@sanity/vision'
import { defineConfig } from 'sanity'
import { deskTool } from 'sanity/desk'
import {
defineUrlResolver,
Iframe,
IframeOptions,
} from 'sanity-plugin-iframe-pane'
import { previewUrl } from 'sanity-plugin-iframe-pane/preview-url'

// see https://www.sanity.io/docs/api-versioning for how versioning works
import {
Expand All @@ -14,7 +20,15 @@ import {
projectId,
} from '~/lib/sanity.api'
import { schema } from '~/schemas'
import { productionUrl } from '~/utils/productionUrl'

const iframeOptions = {
url: defineUrlResolver({
base: '/api/draft',
requiresSlug:['post'],
}),
urlSecretId: previewSecretId,
reload: { button: true },
} satisfies IframeOptions

export default defineConfig({
basePath: '/studio',
Expand All @@ -25,10 +39,30 @@ export default defineConfig({
//edit schemas in './src/schemas'
schema,
plugins: [
deskTool(),
deskTool({
// `defaultDocumentNode` is responsible for adding a “Preview” tab to the document pane
// You can add any React component to `S.view.component` and it will be rendered in the pane
// and have access to content in the form in real-time.
// It's part of the Studio's “Structure Builder API” and is documented here:
// https://www.sanity.io/docs/structure-builder-reference
defaultDocumentNode: (S, { schemaType }) => {
return S.document().views([
// Default form view
S.view.form(),
// Preview
S.view.component(Iframe).options(iframeOptions).title('Preview'),
])

},
}),
// Add the "Open preview" action
previewUrl({
base: '/api/draft',
requiresSlug: ['post'],
urlSecretId: previewSecretId,
}),
// Vision lets you query your content with GROQ in the studio
// https://www.sanity.io/docs/the-vision-plugin
visionTool({ defaultApiVersion: apiVersion }),
productionUrl({ previewSecretId, types: ['post'], apiVersion }),
],
})
21 changes: 9 additions & 12 deletions src/pages/api/draft.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { NextApiRequest, NextApiResponse } from 'next'
import { isValidSecret } from 'sanity-plugin-iframe-pane/is-valid-secret'

import { previewSecretId, readToken } from '~/lib/sanity.api'
import { getClient } from '~/lib/sanity.client'
import { getPreviewSecret } from '~/utils/previewSecret'

export default async function preview(
req: NextApiRequest,
Expand All @@ -29,23 +29,20 @@ export default async function preview(
token: readToken,
})

// The secret can't be stored in an env variable with a NEXT_PUBLIC_ prefix, as it would make you
// vulnerable to leaking the token to anyone. If you don't have an custom API with authentication
// that can handle checking secrets, you may use https://github.com/sanity-io/sanity-studio-secrets
// to store the secret in your dataset.
const storedSecret = await getPreviewSecret({
client: authClient,
id: previewSecretId,
})


// This is the most common way to check for auth, but we encourage you to use your existing auth
// infra to protect your token and securely transmit it to the client
if (secret !== storedSecret) {
const validSecret = await isValidSecret(
authClient,
previewSecretId,
secret
)
if (!validSecret) {
return res.status(401).send('Invalid secret')
}

if (slug) {
res.setPreviewData({ token: readToken })
res.setDraftMode({ enable: true })
res.writeHead(307, { Location: `/post/${slug}` })
res.end()
return
Expand Down
53 changes: 0 additions & 53 deletions src/utils/previewSecret.ts

This file was deleted.

Loading

1 comment on commit 6c34d44

@vercel
Copy link

@vercel vercel bot commented on 6c34d44 Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

template-nextjs-clean – ./

template-nextjs-clean.sanity.build
template-nextjs-clean-git-main.sanity.build

Please sign in to comment.