commerce/components/preview/preview-provider.tsx
2023-08-21 11:24:25 +02:00

26 lines
689 B
TypeScript

'use client';
import dynamic from 'next/dynamic';
import { suspend } from 'suspend-react';
const LiveQueryProvider = dynamic(() => import('next-sanity/preview'));
// suspend-react cache is global, so we use a unique key to avoid collisions
const UniqueKey = Symbol('@/lib/sanity/sanity.client');
export default function PreviewProvider({
children,
token
}: {
children: React.ReactNode;
token?: string;
}) {
const { client } = suspend(() => import('@/lib/sanity/sanity.client'), [UniqueKey]);
if (!token) throw new TypeError('Missing token');
return (
<LiveQueryProvider client={client} token={token} logger={console}>
{children}
</LiveQueryProvider>
);
}