51
loading...
This website collects cookies to deliver better user experience
const cloudinaryBaseUrl = `https://res.cloudinary.com/${process.env.CLOUDINARY_CLOUD_NAME}/image/upload/`;
module.exports = {
images: {
loader: "cloudinary",
path: cloudinaryBaseUrl,
},
};
function Home({ exampleImage }) {
return (
<div className={styles.container}>
<main className={styles.main}>
<h1 className={styles.title}>Blurred image placeholder</h1>
<h2 className={styles.subtitle}>with Next.js image and cloudinary</h2>
<div className={styles.imagewrapper}>
<Image
src={exampleImage.src}
alt="Example"
width="1920"
height="1280"
layout="responsive"
quality="75"
sizes="60vw"
/>
</div>
</main>
</div>
);
}
export async function getBase64ImageUrl(imageId: string): Promise<string | undefined> {
const response = await fetch(`${process.env.CLOUDINARY_BASE_URL}w_100/e_blur:1000,q_auto,f_webp${imageId}`);
const buffer = await response.arrayBuffer();
const data = Buffer.from(buffer).toString('base64');
return `data:image/webp;base64,${data}`;
}
export async function getStaticProps() {
const imageSrc = process.env.CLOUDINARY_EXAMPLE_IMAGE_SRC;
if (!imageSrc) {
throw new Error('Missing CLOUDINARY_EXAMPLE_IMAGE_SRC env variable');
}
const blurDataUrl = await getBase64ImageUrl(imageSrc);
return {
props: {
exampleImage: {
src: imageSrc,
blurDataUrl: blurDataUrl,
},
},
};
}
function Home({ exampleImage }: InferGetStaticPropsType<typeof getStaticProps>) {
return (
<div className={styles.container}>
<main className={styles.main}>
<h1 className={styles.title}>Blurred image placeholder</h1>
<h2 className={styles.subtitle}>with Next.js image and cloudinary</h2>
<div className={styles.imagewrapper}>
<Image
src={exampleImage.src}
alt="Example"
width="1920"
height="1280"
layout="responsive"
quality="75"
sizes="60vw"
placeholder="blur"
blurDataURL={exampleImage.blurDataUrl}
/>
</div>
</main>
</div>
);
}