Skip to content

Instantly share code, notes, and snippets.

@ssv445
Forked from davidpp/DirectusLoader.tsx
Created February 23, 2025 04:29
Show Gist options
  • Select an option

  • Save ssv445/b785a927d73d00e4452be9875b424e6d to your computer and use it in GitHub Desktop.

Select an option

Save ssv445/b785a927d73d00e4452be9875b424e6d to your computer and use it in GitHub Desktop.
NextJS image loader for Directus
import type { ImageLoaderProps } from 'next/image'
const imgDomain = process.env.NEXT_PUBLIC_IMG_DOMAIN ?? ''
interface DirectusImageProps {
fit?: 'cover' | 'contain' | 'inside' | 'outside'
}
export default function directusImageLoader({
src: imageID,
width,
quality,
fit,
}: ImageLoaderProps & DirectusImageProps) {
const url = new URL(`https://${imgDomain}/assets/${imageID}`)
url.searchParams.set('fit', fit ?? 'contain')
url.searchParams.set('width', width.toString())
url.searchParams.set('quality', quality?.toString() || '75')
return url.href
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment