Created
April 13, 2023 14:23
-
-
Save davidpp/7aa8d5289c2e907482c5f32f18b64e2f to your computer and use it in GitHub Desktop.
NextJS image loader for Directus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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