Skip to content

Commit

Permalink
chore: test server only solution
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Jan 18, 2024
1 parent 56d2902 commit 330ee45
Show file tree
Hide file tree
Showing 20 changed files with 247 additions and 68 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,4 @@ package-lock.json
.idea/
.vercel
.turbo
.vscode
19 changes: 3 additions & 16 deletions apps/next-server-only/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const require = createRequire(import.meta.url)

/** @type {import('next').NextConfig} */
const nextConfig = {
// reactStrictMode: true,
/*
reactStrictMode: true,
// /*
logging: {
fetches: {
fullUrl: true,
Expand All @@ -30,6 +30,7 @@ const nextConfig = {

experimental: {
taint: true,
windowHistorySupport: true,
},

webpack(config) {
Expand All @@ -45,20 +46,6 @@ const nextConfig = {
}
return config
},

async headers() {
return [
{
source: '/:path*',
headers: [
{
key: 'Content-Security-Policy',
value: `frame-ancestors 'self' https://*.sanity.build http://localhost:3333`,
},
],
},
]
},
}

export default withBundleAnalyzer({
Expand Down
2 changes: 2 additions & 0 deletions apps/next-server-only/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@sanity/presentation": "workspace:*",
"@sanity/preview-kit-compat": "workspace:*",
"@sanity/preview-url-secret": "workspace:*",
"@sanity/react-loader": "workspace:*",
"@sanity/vision": "^3.24.1",
"@sanity/visual-editing-helpers": "workspace:*",
"@tailwindcss/typography": "^0.5.10",
Expand All @@ -28,6 +29,7 @@
"autoprefixer": "^10.4.16",
"classnames": "^2.5.1",
"date-fns": "^2.30.0",
"fast-deep-equal": "3.1.3",
"next": "14.0.4",
"next-sanity": "^7.0.10",
"react": "^18.2.0",
Expand Down
8 changes: 6 additions & 2 deletions apps/next-server-only/src/app/(blog)/AuthorAvatar.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { sanityFetch } from '@/lib/fetch'
import { urlForImage } from '@/lib/image'
import { loadQuery } from '@/lib/loadQuery'
import Image from 'next/image'
import Balancer from 'react-wrap-balancer'

const query = /* groq */ `*[_type == "author" && _id == $id][0]`

export async function AuthorAvatar(params: { id: string }) {
const data = await sanityFetch<any>({
const [data,RevalidatePreviewQuery] = await loadQuery<any>({
query,
params,
tags: [`author:${params.id}`],
})
const { name = 'Anonymous', image } = data
return (
<div className="flex items-center">
<><div className="flex items-center">
<div className="relative mr-4 h-12 w-12">
<Image
src={
Expand All @@ -31,6 +32,9 @@ export async function AuthorAvatar(params: { id: string }) {
<Balancer>{name}</Balancer>
</div>
</div>
{/* When Draft Mode is enabled this component lets the Sanity Presentation Tool revalidate queries as content changes */}
<RevalidatePreviewQuery />
</>
)
}

Expand Down
6 changes: 4 additions & 2 deletions apps/next-server-only/src/app/(blog)/MoreStories.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Link from 'next/link'
import { Suspense } from 'react'

import { sanityFetch } from '@/lib/fetch'
import { postFields } from '@/lib/queries'
import { AuthorAvatar, AuthorAvatarFallback } from './AuthorAvatar'
import CoverImage from './CoverImage'
import Date from './PostDate'
import { loadQuery } from '@/lib/loadQuery'

const query = /* groq */ `*[_type == "post" && _id != $skip] | order(date desc, _updatedAt desc) [0...$limit] {
${postFields}
Expand All @@ -15,7 +15,7 @@ export default async function MoreStories(params: {
skip: string
limit: number
}) {
const data = await sanityFetch<any>({ query, params, tags: ['post'] })
const [data,RevalidatePreviewQuery] = await loadQuery<any>({ query, params, tags: ['post'] })

return (
<>
Expand Down Expand Up @@ -63,6 +63,8 @@ export default async function MoreStories(params: {
)
})}
</div>
{/* When Draft Mode is enabled this component lets the Sanity Presentation Tool revalidate queries as content changes */}
<RevalidatePreviewQuery />
</>
)
}
11 changes: 7 additions & 4 deletions apps/next-server-only/src/app/(blog)/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { client } from '@/lib/client'
import { urlForImage } from '@/lib/image'
import { sanityFetch } from '@/lib/fetch'
import { postFields } from '@/lib/queries'
import { loadQuery } from '@/lib/loadQuery'

type Props = {
params: { slug: string }
Expand All @@ -25,10 +26,10 @@ export async function generateMetadata(
{ params }: Props,
parent: ResolvingMetadata,
): Promise<Metadata> {
const [post, authorName] = await Promise.all([
sanityFetch<any>({ query, params, tags: [`post:${params.slug}`] }),
const [[post], [authorName]] = await Promise.all([
loadQuery<any>({ query, params, tags: [`post:${params.slug}`] }),
// @TODO necessary as there's problems with type inference when `author-{name,image}` is used
sanityFetch<string | null>({
loadQuery<string | null>({
query: /* groq */ `*[_type == "post" && slug.current == $slug][0].author->name`,
params,
tags: [`post:${params.slug}`, 'author'],
Expand All @@ -55,7 +56,7 @@ export async function generateMetadata(

export default async function BlogPostPage({ params }: Props) {
const { slug } = params
const data = await sanityFetch<any>({
const [data, RevalidatePreviewQuery] = await loadQuery<any>({
query,
params,
tags: [`post:${params.slug}`],
Expand Down Expand Up @@ -105,6 +106,8 @@ export default async function BlogPostPage({ params }: Props) {
<MoreStories skip={_id} limit={2} />
</Suspense>
</aside>
{/* When Draft Mode is enabled this component lets the Sanity Presentation Tool revalidate queries as content changes */}
<RevalidatePreviewQuery />
</>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@ import BlogHeader from './BlogHeader'
import { EXAMPLE_NAME } from '@/lib/constants'
import PreviewBanner from './PreviewBanner'
import { draftMode } from 'next/headers'
import dynamic from 'next/dynamic'

export default function Template({ children }: { children: React.ReactNode }) {
const Overlays = dynamic(() => import('@/components/VisualEditing/Overlays'))
const PresentationToolBridge = dynamic(
() => import('@/components/VisualEditing/PresentationToolBridge'),
)

export default function BlogLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<>
{draftMode().isEnabled && <PreviewBanner />}
Expand All @@ -19,6 +29,8 @@ export default function Template({ children }: { children: React.ReactNode }) {
</Link>
</footer>
</div>
{draftMode().isEnabled && <Overlays />}
{draftMode().isEnabled && <PresentationToolBridge />}
</>
)
}
6 changes: 4 additions & 2 deletions apps/next-server-only/src/app/(blog)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import CoverImage from './CoverImage'
import MoreStories from './MoreStories'
import Date from './PostDate'
import { postFields } from '@/lib/queries'
import { sanityFetch } from '@/lib/fetch'
import { loadQuery } from '@/lib/loadQuery'

export default async function BlogIndexPage() {
const data = await sanityFetch<any>({
const [data, RevalidatePreviewQuery] = await loadQuery<any>({
query: /* groq */ `
*[_type == "post"] | order(publishedAt desc, _updatedAt desc) [0] {
${postFields}
Expand Down Expand Up @@ -67,6 +67,8 @@ export default async function BlogIndexPage() {
</Suspense>
</aside>
)}
{/* When Draft Mode is enabled this component lets the Sanity Presentation Tool revalidate queries as content changes */}
<RevalidatePreviewQuery />
</>
)
}

This file was deleted.

3 changes: 0 additions & 3 deletions apps/next-server-only/src/app/@visualEditing/default.tsx

This file was deleted.

10 changes: 2 additions & 8 deletions apps/next-server-only/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'tailwindcss/tailwind.css'

import type { Metadata } from 'next'

import { Inter } from 'next/font/google'

import { CMS_NAME } from '@/lib/constants'

const inter = Inter({
Expand All @@ -18,18 +18,12 @@ export const metadata = {

export default function RootLayout({
children,
visualEditing,
}: {
children: React.ReactNode
visualEditing: React.ReactNode
}) {
console.log('RootLayout', { children, visualEditing })
return (
<html lang="en" className={inter.variable}>
<body className="bg-white text-black">
{children}
{visualEditing}
</body>
<body className="bg-white text-black">{children}</body>
</html>
)
}
10 changes: 0 additions & 10 deletions apps/next-server-only/src/app/studio/layout.tsx

This file was deleted.

Loading

0 comments on commit 330ee45

Please sign in to comment.