next/image does not work with assetPrefix

See original GitHub issue

What 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:closed
  • Created 3 years ago
  • Reactions:23
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
TimoWilhelmcommented, Jun 9, 2021

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.

1reaction
ijjkcommented, Sep 27, 2021

@avaleriani it sounds like yours might be related to https://github.com/vercel/next.js/issues/29289 which was a bug where the assetPrefix wasn’t being added to the src for static image imports. This is fixed in the latest canary.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CDN Support with Asset Prefix - next.config.js
To set up a CDN, you can set up an asset prefix and configure your CDN's origin to resolve to the domain that...
Read more >
Github pages deploy Next.js assetPrefix not working
When using next-image we should set the assetPrefix inside it. const withImages = require('next-images') const withPlugins ...
Read more >
CDN Support with Asset Prefix - 周陆军博客
To set up a CDN, you can set up an asset prefix and configure your CDN's origin to resolve to the domain that...
Read more >
With NextJS, I want to programmatically access assetPrefix ...
[Solved]-With NextJS, I want to programmatically access assetPrefix from ... Using Tabs - Tab with Link from next JS (tab indicator not working) ......
Read more >
next.config.js: CDN Support with Asset Prefix
You do not need to manually setup an Asset Prefix. Note: Next.js 9.5+ added support for a customizable Base Path, which is better...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found