Skip to content

Commit

Permalink
chore: setup overlays in next-pages-router and next-app-router
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Nov 16, 2023
1 parent 2c85b51 commit ef08bf6
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { suspend } from 'suspend-react'
import { LiveQueryProvider } from '@sanity/preview-kit'
import VisualEditing from './VisualEditing'

// suspend-react cache is global, so we use a unique key to avoid collisions
const UniqueKey = Symbol('./sanity.client')
Expand All @@ -16,8 +17,11 @@ export default function PreviewProvider({
const { client } = suspend(() => import('./sanity.client'), [UniqueKey])
if (!token) throw new TypeError('Missing token')
return (
<>
<LiveQueryProvider client={client} token={token} logger={console}>
{children}
</LiveQueryProvider>
<VisualEditing />
</>
)
}
54 changes: 54 additions & 0 deletions apps/next-app-router/app/variants/live-store/VisualEditing.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use client'

import { enableOverlays, HistoryAdapterNavigate } from '@sanity/overlays'
import { usePathname, useRouter, useSearchParams } from 'next/navigation'
import { useEffect, useRef, useState } from 'react'

export default function VisualEditing() {
const router = useRouter()
const routerRef = useRef(router)
const [navigate, setNavigate] = useState<HistoryAdapterNavigate | undefined>()

useEffect(() => {
routerRef.current = router
}, [router])
useEffect(() => {
const disable = enableOverlays({
allowStudioOrigin: process.env.NEXT_PUBLIC_STUDIO_URL || 'http://localhost:3333',
history: {
subscribe: (navigate) => {
setNavigate(() => navigate)
return () => setNavigate(undefined)
},
update: (update) => {
switch (update.type) {
case 'push':
return routerRef.current.push(update.url)
case 'pop':
return routerRef.current.back()
case 'replace':
return routerRef.current.replace(update.url)
default:
throw new Error(`Unknown update type: ${update.type}`)
}
},
},
})
return () => disable()
}, [])

const pathname = usePathname()
const searchParams = useSearchParams()
useEffect(() => {
if (navigate) {
navigate({
type: 'push',
url: `${pathname}${searchParams?.size ? `?${searchParams}` : ''}`,
})
}
}, [navigate, pathname, searchParams])



return null
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const client = createClient({
dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || 'production',
apiVersion: process.env.NEXT_PUBLIC_SANITY_API_VERSION || '2022-11-15',
useCdn: false,
studioUrl: 'https://preview-kit-test-studio.sanity.build/',
studioUrl: process.env.NEXT_PUBLIC_STUDIO_URL || 'http://localhost:3333',
logger: console,
encodeSourceMap: true,
perspective: 'published',
Expand Down
1 change: 1 addition & 0 deletions apps/next-app-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"groqd": "0.15.10",
"next": "14.0.2",
"react": "18.2.0",
"@sanity/overlays": "2.0.0",
"react-dom": "18.2.0",
"server-only": "^0.0.1",
"suspend-react": "^0.1.3",
Expand Down
1 change: 1 addition & 0 deletions apps/next-pages-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"bulma": "0.9.4",
"groq": "3.19.3",
"groqd": "0.15.10",
"@sanity/overlays": "2.0.0",
"next": "14.0.2",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LiveQueryProvider } from '@sanity/preview-kit'
import { client } from './sanity.client'
import VisualEditing from './VisualEditing'

export default function PreviewProvider({
children,
Expand All @@ -10,8 +11,11 @@ export default function PreviewProvider({
}) {
if (!token) throw new TypeError('Missing token')
return (
<>
<LiveQueryProvider client={client} token={token} logger={console}>
{children}
</LiveQueryProvider>
<VisualEditing />
</>
)
}
46 changes: 46 additions & 0 deletions apps/next-pages-router/src/variants/live-store/VisualEditing.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { HistoryAdapterNavigate, enableOverlays } from '@sanity/overlays'
import { useRouter } from 'next/router'
import { useEffect, useRef, useState } from 'react'


export default function VisualEditing() {
const router = useRouter()
const routerRef = useRef(router)
const [navigate, setNavigate] = useState<HistoryAdapterNavigate | undefined>()

useEffect(() => {
routerRef.current = router
}, [router])
useEffect(() => {
if (!router.isReady) return
const disable = enableOverlays({
allowStudioOrigin: process.env.NEXT_PUBLIC_STUDIO_URL || 'http://localhost:3333',
history: {
subscribe: (navigate) => {
setNavigate(() => navigate)
return () => setNavigate(undefined)
},
update: (update) => {
switch (update.type) {
case 'push':
return routerRef.current.push(update.url)
case 'pop':
return routerRef.current.back()
case 'replace':
return routerRef.current.replace(update.url)
default:
throw new Error(`Unknown update type: ${update.type}`)
}
},
},
})
return () => disable()
}, [router.isReady])
useEffect(() => {
if (navigate) {
navigate({ type: 'push', url: router.asPath })
}
}, [navigate, router.asPath])

return null
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID || 'pv8y60vp'
const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET || 'production'
const apiVersion = process.env.NEXT_PUBLIC_SANITY_API_VERSION || '2022-11-15'
const useCdn = true
const studioUrl = 'https://preview-kit-test-studio.sanity.build/'
const studioUrl = process.env.NEXT_PUBLIC_STUDIO_URL || 'http://localhost:3333'
export const client = createClient({
projectId,
dataset,
Expand Down
8 changes: 7 additions & 1 deletion pnpm-lock.yaml

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

6 comments on commit ef08bf6

@vercel
Copy link

@vercel vercel bot commented on ef08bf6 Nov 16, 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:

preview-kit-next-pages-router – ./apps/next-pages-router

preview-kit-next-pages-router-git-main.sanity.build
preview-kit-next-pages-router.sanity.build

@vercel
Copy link

@vercel vercel bot commented on ef08bf6 Nov 16, 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:

preview-kit-next-pages-router-live-store – ./apps/next-pages-router

preview-kit-next-pages-router-live-store.sanity.build
preview-kit-next-pages-router-live-store-git-main.sanity.build

@vercel
Copy link

@vercel vercel bot commented on ef08bf6 Nov 16, 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:

preview-kit-next-pages-router-groq-store – ./apps/next-pages-router

preview-kit-next-pages-router-groq-store.sanity.build
preview-kit-next-pages-router-groq-store-git-main.sanity.build

@vercel
Copy link

@vercel vercel bot commented on ef08bf6 Nov 16, 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:

preview-kit-next-app-router – ./apps/next-app-router

preview-kit-next-app-router.sanity.build
preview-kit-next-app-router-git-main.sanity.build

@vercel
Copy link

@vercel vercel bot commented on ef08bf6 Nov 16, 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:

preview-kit-next-app-router-live-store – ./apps/next-app-router

preview-kit-next-app-router-live-store.sanity.build
preview-kit-next-app-router-live-store-git-main.sanity.build

@vercel
Copy link

@vercel vercel bot commented on ef08bf6 Nov 16, 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:

preview-kit-next-app-router-groq-store – ./apps/next-app-router

preview-kit-next-app-router-groq-store-git-main.sanity.build
preview-kit-next-app-router-groq-store.sanity.build

Please sign in to comment.