# @degallery/next

Next.js integration for DeGallery. It provides a `next/image` renderer for the
React gallery views and server helpers for loading gallery data during server
rendering.

## Install

```sh
pnpm add @degallery/next @degallery/react @degallery/sdk @tanstack/react-query
```

Use compatible versions of Next.js 15 or 16 and React 19.

## Add the provider

Place `GalleryProvider` in a Client Component and use it from the root layout.
The provider uses `next/image` for gallery images; private and expiring image
URLs bypass the Next.js image optimizer so authenticated delivery continues to
work.

```tsx
// app/gallery-provider.tsx
'use client';

import { GalleryProvider } from '@degallery/next';

export function AppGalleryProvider({ children }: { children: React.ReactNode }) {
  return (
    <GalleryProvider
      apiUrl={process.env.NEXT_PUBLIC_DEGALLERY_API_URL!}
      mediaUrl={process.env.NEXT_PUBLIC_DEGALLERY_MEDIA_URL}
      publishableKey={process.env.NEXT_PUBLIC_DEGALLERY_PUBLISHABLE_KEY}
    >
      {children}
    </GalleryProvider>
  );
}
```

Add the provider to `app/layout.tsx`:

```tsx
import { AppGalleryProvider } from './gallery-provider';

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body><AppGalleryProvider>{children}</AppGalleryProvider></body>
    </html>
  );
}
```

## Server-render a gallery

Use the server-only subpath from a Server Component. The initial gallery page
is fetched on the server and rendered into the HTML, then the client view can
continue loading pages and provide its interactive lightbox.

```tsx
import { ServerGallery } from '@degallery/next/server';

export default async function GalleryPage({
  params,
}: {
  params: Promise<{ slug: string }>;
}) {
  const { slug } = await params;
  return (
    <ServerGallery
      apiUrl={process.env.NEXT_PUBLIC_DEGALLERY_API_URL!}
      mediaUrl={process.env.NEXT_PUBLIC_DEGALLERY_MEDIA_URL}
      galleryIdOrSlug={slug}
      limit={24}
    />
  );
}
```

For private galleries, pass a short-lived read token from a Server Component or
server action. Never expose a private API key to a Client Component. `getGallery`
is also exported for applications that need to render their own markup:

```tsx
import { getGallery } from '@degallery/next/server';

const gallery = await getGallery({ apiUrl, galleryIdOrSlug: 'my-gallery' });
```

The default server fetch uses `cache: 'no-store'` because delivery URLs can be
private or expire. A custom `fetch` implementation may provide application
caching when that is appropriate.

## Next.js image configuration

If your app uses Next.js image optimization for externally hosted public
images, allow the hosts used by your galleries in `next.config`:

```ts
import type { NextConfig } from 'next';

const nextConfig: NextConfig = {
  images: {
    remotePatterns: [
      { protocol: 'https', hostname: 'images.unsplash.com' },
      { protocol: 'https', hostname: 'media.degallery.dev' },
      { protocol: 'https', hostname: 'res.cloudinary.com' },
    ],
  },
};

export default nextConfig;
```

Use the actual media and Cloudinary hostnames configured for your deployment.
The gallery renderer supplies responsive sizing and image dimensions when
available. Set the surrounding layout dimensions with CSS as usual.
