32
loading...
This website collects cookies to deliver better user experience
next/script
: Automatically prioritise loading of third-party scripts to improve performance.next/image
: Reduce layout shift and create a smoother visual experience with automatic size detection and support for blur-up placeholders.
<Image
alt={`image info`}
src={`/static/image.png`}
width={5000}
height={3313}
/>
npm install react@latest react-dom@latest
yarn
:yarn add react@latest react-dom@latest
npm install next@latest
yarn
:yarn add next@latest
import Image from 'next/image'
import author from '../public/me.png'
export default function Home() {
return (
// When importing the image as the source, you
// don't need to define `width` and `height`.
<Image src={author} alt="Picture of the author" />
)
}
import Image from 'next/image'
import author from '../public/me.png'
export default function Home() {
return (
// When importing the image as the source, you
// don't need to define `width` and `height`.
<Image src={author} alt="Picture of the author" placeholder="blur"/>
)
}
blurDataURL
, which is provided by your back-end. For example, you can generate a blurha.sh on the server.<Image
src="https://nextjs.org/static/images/learn.png"
blurDataURL="data:image/jpeg;base64,/9j/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAIAAoDASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAb/xAAhEAACAQMDBQAAAAAAAAAAAAABAgMABAUGIWEREiMxUf/EABUBAQEAAAAAAAAAAAAAAAAAAAMF/8QAGhEAAgIDAAAAAAAAAAAAAAAAAAECEgMRkf/aAAwDAQACEQMRAD8AltJagyeH0AthI5xdrLcNM91BF5pX2HaH9bcfaSXWGaRmknyJckliyjqTzSlT54b6bk+h0R//2Q=="
alt="Picture of the author"
placeholder="blur"
/>
next/image
component, It is extremely simple. MDXComponents
fileMDX
file, so I worked around it by creating a react component and importing the image in there and and calling that component in the MDX
file like the code below.// MDXComponets.js
import Image from 'next/image';
const NextImg = (props) => {
const { src, alt } = props;
return <Image src={require(`../public/static/images/${src}`)} alt={alt} placeholder="blur" />;
};
const MDXComponents = {
NextImg,
};
export default MDXComponents;
NOTE - placeholder={"blur"}
wont work with gif
files, so I used a disablePlaceholder
prop to control whether to show the placeholder
MDXComponents
file, create one in your components folder and call it in your MDXProvider
in _app.js
. MDXComponents
file is used to inject your custom components to your markdown.// My _app.js file
import 'styles/global.scss';
import { ThemeProvider } from 'next-themes';
import { MDXProvider } from '@mdx-js/react';
import MDXComponents from 'components/MDXComponents';
function MyApp({ Component, pageProps }) {
return (
<ThemeProvider attribute="class">
<MDXProvider components={MDXComponents}>
<Component {...pageProps} />
</MDXProvider>
</ThemeProvider>
);
}
export default MyApp;
Nextimg
component in my MDX file// One of my article .mdx file
--------
title: Tips For Using Async/Await - Write Better JavaScript!
tags: javaScript
--------
<NextImg
alt={`Tips-for-using-async-await-write-better-java-script`}
src={'tips-for-using-async-await-write-better-java-script/banner.png'}
// To disable placeholder feature use disablePlaceholder={true}
/>
Pro Tip : In-case you are not able to see this try a hard reload by ctrl
+r
or try throttling the website in the networks panel