Skip to content

Commit

Permalink
feat: enable CSM powered live preview
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Nov 16, 2023
1 parent 7f7d35c commit 6a079db
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 55 deletions.
13 changes: 13 additions & 0 deletions app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'tailwindcss/tailwind.css'

export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
8 changes: 8 additions & 0 deletions app/studio/[[...index]]/Studio.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use client'

import { NextStudio } from 'next-sanity/studio'
import config from 'sanity.config'

export default function Studio() {
return <NextStudio config={config} />
}
19 changes: 19 additions & 0 deletions app/studio/[[...index]]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* This route is responsible for the built-in authoring environment using Sanity Studio v3.
* All routes under /studio will be handled by this file using Next.js' catch-all routes:
* https://nextjs.org/docs/routing/dynamic-routes#catch-all-routes
*
* You can learn more about the next-sanity package here:
* https://github.com/sanity-io/next-sanity
*/

import Studio from './Studio'

export const dynamic = 'force-static'

export { metadata } from 'next-sanity/studio/metadata'
export { viewport } from 'next-sanity/studio/viewport'

export default function StudioPage() {
return <Studio />
}
5 changes: 5 additions & 0 deletions lib/sanity.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export const previewSecretId: `${string}.${string}` = 'preview.secret'
// Used to generate URLs for previewing your content
export const DRAFT_MODE_ROUTE = '/api/draft'

/**
* Used to configure edit intent links, for Presentation Mode, as well as to configure where the Studio is mounted in the router.
*/
export const studioUrl = '/studio'

function assertValue<T>(v: T | undefined, errorMessage: string): T {
if (v === undefined) {
throw new Error(errorMessage)
Expand Down
11 changes: 10 additions & 1 deletion lib/sanity.client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { apiVersion, dataset, projectId, useCdn } from 'lib/sanity.api'
import {
apiVersion,
dataset,
projectId,
studioUrl,
useCdn,
} from 'lib/sanity.api'
import {
indexQuery,
type Post,
Expand All @@ -17,6 +23,9 @@ export function getClient(preview?: { token: string }): SanityClient {
apiVersion,
useCdn,
perspective: 'published',
encodeSourceMap: preview?.token ? true : false,
studioUrl,
logger: console,
})
if (preview) {
if (!preview.token) {
Expand Down
1 change: 1 addition & 0 deletions next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
/// <reference types="next/navigation-types/compat/navigation" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
74 changes: 42 additions & 32 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"date-fns": "2.30.0",
"intl-segmenter-polyfill": "0.4.4",
"next": "14.0.2",
"next-sanity": "5.5.10",
"next-sanity": "6.0.3",
"next-sanity-image": "6.1.1",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
17 changes: 0 additions & 17 deletions pages/studio/[[...index]].tsx

This file was deleted.

20 changes: 16 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"compilerOptions": {
"baseUrl": ".",
"target": "ES2017",
"lib": ["dom", "dom.iterable", "esnext"],
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
Expand All @@ -19,8 +23,16 @@
{
"name": "next"
}
]
],
"strictNullChecks": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
],
"exclude": [
"node_modules"
]
}

1 comment on commit 6a079db

@vercel
Copy link

@vercel vercel bot commented on 6a079db Nov 16, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.