The official Next.js documentation states that basePath is needed to be explicitly coded in the Images[Src] attribute.
So, if you setup your basePath in the next.config.js, like this:
/** @type {import('next').NextConfig} */
const nextConfig = {
basePath: '/absproxy/3000',
assetPrefix: '/absproxy/3000',
reactStrictMode: true,
swcMinify: true,
trailingSlash: true,
async rewrites() {
return [
{
source: '/images/:path*',
destination: '/absproxy/3000/images/:path*', // The :path parameter isn't used here so will be automatically passed in the query
},
]
},
images: {
path: '/absproxy/3000/_next/image',
},
}
module.exports = nextConfig
Then I have my flower.jpg located in
{my_project_path}/public/images/flower.jpg
Here we have 3 ways to apply the basePath in your Image component:
Hard-coding
import Head from 'next/head';
import styles from '../styles/Home.module.css';
import Link from 'next/link';
import Image from 'next/image';
export default function Home() {
return (
<div className={styles.container}>
<Head>
<title>Create Next App</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main>
<Image
src='/absproxy/3000/images/flower.jpg'// Route of the image file
height={144} // Desired size with correct aspect ratio
width={144} // Desired size with correct aspect ratio
alt="Your Name"
/>
</main>
</div>
)
}
Import the image file as variable ({flower}):
import flower from '../public/images/flower.jpg'
then use as the Image[src] attribute:
import Head from 'next/head';
import styles from '../styles/Home.module.css';
import Link from 'next/link';
import Image from 'next/image';
import flower from '../public/images/flower.jpg'
export default function Home() {
return (
<div className={styles.container}>
<Head>
<title>Create Next App</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main>
<Image
src={flower}// Route of the image file
height={144} // Desired size with correct aspect ratio
width={144} // Desired size with correct aspect ratio
alt="Your Name"
/>
</main>
</div>
)
}
- Import the
basePath
variable from next.config.js, and then concatenate the relative path of the static image file, and then apply in the src attribute of the image component.
src={${basePath}/images/flower.jpg
}
import Head from 'next/head';
import styles from '../styles/Home.module.css';
import Link from 'next/link';
import Image from 'next/image';
import { basePath } from '../next.config';
export default function Home() {
return (
<div className={styles.container}>
<Head>
<title>Create Next App</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main>
<Image
src={`${basePath}/images/flower.jpg`}// Route of the image file
height={144} // Desired size with correct aspect ratio
width={144} // Desired size with correct aspect ratio
alt="Your Name"
/>
</main>
</div>
)
}
public/
folder? – Trieciouspublic/
– Quaquaversal