Ported sanity studio to Next js app

This commit is contained in:
Henrik Larsson
2023-08-14 12:06:46 +02:00
parent de85d266bd
commit d32baa7782
67 changed files with 3755 additions and 46 deletions

View File

@@ -4,8 +4,8 @@ import { NextIntlClientProvider } from 'next-intl';
import { Inter } from 'next/font/google';
import { notFound } from 'next/navigation';
import { ReactNode, Suspense } from 'react';
import { supportedLanguages } from '../../i18n-config';
import './globals.css';
import { supportedLanguages } from '../../../i18n-config';
import './../../globals.css';
export const metadata = {
title: {
@@ -48,7 +48,7 @@ export default async function LocaleLayout({ children, params: { locale } }: Loc
let messages;
try {
messages = (await import(`../../messages/${locale}.json`)).default;
messages = (await import(`../../../messages/${locale}.json`)).default;
} catch (error) {
notFound();
}

View File

@@ -0,0 +1,8 @@
'use client';
import { NextStudio } from 'next-sanity/studio';
import config from '@/sanity.config';
export default function AdminPage() {
return <NextStudio config={config} />;
}

View File

@@ -0,0 +1,14 @@
import './../../globals.css';
export const metadata = {
title: `Studio | ${process.env.SITE_NAME}`,
description: 'KM Storefront studio admin interface.'
};
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}

View File

@@ -1,39 +0,0 @@
import { isValidSignature, SIGNATURE_HEADER_NAME } from '@sanity/webhook';
import { revalidatePath } from 'next/cache';
import { headers } from 'next/headers';
import { NextRequest, NextResponse } from 'next/server';
const SANITY_WEBHOOK_SECRET = `${process.env.SANITY_WEBHOOK_SECRET}`;
export async function POST(request: NextRequest) {
// Await the response from our request.
const requestData = await request.json();
// Get headers.
const headersList = headers();
// Get Sanity webhook signature header name.
const signature = `${headersList.get(SIGNATURE_HEADER_NAME)}`;
const isValid = isValidSignature(JSON.stringify(requestData), signature, SANITY_WEBHOOK_SECRET);
// Log out validity of request.
console.log(`Webhook request valid? ${isValid}`);
// If not valid, return.
if (!isValid) {
NextResponse.json({ success: false, message: 'Invalid signature' });
return;
}
const slug = requestData.slug;
const type = requestData.type;
if (type === 'home') {
revalidatePath(`${slug}`)
} else {
revalidatePath(`${slug}`)
}
console.log(`Revalidated path: ${slug}`);
return NextResponse.json({ revalidated: true, now: Date.now() });
}