next/image does not work with assetPrefix
See original GitHub issueWhat version of Next.js are you using?
10.0.5
What version of Node.js are you using?
14.5.0
What browser are you using?
Chrome 87
What operating system are you using?
macOS
How are you deploying your application?
local production build
Describe the Bug
I dug through the docs and found only 1 CDN related config is assetPrefix, but assetPrefix only redirect js and css files to the CDN server. From what I saw in the next/image RFC, I think it’s reasonable to let next/image redirect local images to the CDN server.
After setting assetPrefix in the config file:
// next.config.js
module.exports = {
assetPrefix: BUILD_ENV === 'local' ? '' : 'https://cdn.example.com'
}
and reference public image in <Image>
<Image src={'/assets/home/service-icon/labor.png'} alt='' />
results in a request to https://www.example.com/_next/image?url=%2Fassets%2Fhome%2Fservice-icon%2Flabor.png&w=828&q=75, not https://cdn.example.com/_next/image?url=%2Fassets%2Fhome%2Fservice-icon%2Flabor.png&w=828&q=75 as expected.
Expected Behavior
<Image> with local image path also applies the assetPrefix.
To Reproduce
Use both <Image> and assetPrefix.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:23
- Comments:12 (3 by maintainers)
Top Related StackOverflow Question
I’m also facing this issue. I was expecting the image component to use the same asset prefix as the other static assets. One workaround would be to use a custom loader but that doesn’t seem to play well with ISR in my case.
@avaleriani it sounds like yours might be related to https://github.com/vercel/next.js/issues/29289 which was a bug where the
assetPrefixwasn’t being added to thesrcfor static image imports. This is fixed in the latest canary.