Skip to content

use custom loader prop #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions components/MyImage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Image from "next/image";

const myLoader = ({src, width, quality}) => {
return `https://res.cloudinary.com/rhnmht30/image/upload/w_${width},q_${
quality || 75
}/${src}`;
};
const MyImage = (props) => {
return <Image loader={myLoader} {...props} />;
};

export default MyImage;
16 changes: 15 additions & 1 deletion pages/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Head from "next/head";
import MyImage from "../components/MyImage";
import Image from "next/image";
import styles from "../styles/Home.module.css";

Expand All @@ -22,10 +23,23 @@ export default function Home() {
</p>

<div className={styles.grid}>
{[1, 2, 3, 4].map((n, i) => (
{[1, 2].map((n, i) => (
<Image key={i} src={`/coffee-${n}.jpg`} width={640} height={426} />
))}
{[3, 4].map((n, i) => (
<MyImage
key={i}
src={`/coffee-${n}.jpg`}
width={640}
height={426}
/>
))}
</div>
<p className={styles.description}>
First 2 images are optimised through netlify functions and last 2
images are optimised using cloudinary as the loader. See the
difference in performance by disabling cache and throttling network.
</p>
</main>

<footer className={styles.footer}>
Expand Down