next.js: BUG: incorrect url encoding in `` for prioritized images

Link to the code that reproduces this issue

https://github.com/HelaGone/image_testing

To Reproduce

  1. build the application
  2. start the server
  3. visit the provided url
  4. right click and select view source code
  5. search for the link rel=preload of the imageSrcSet
Screenshot 2024-08-16 at 4 35 25 p m
  1. see the wrong encoding of the & it’s & and should be & or %26
  2. copy the path and concatenate the path with the localhost:3000
  3. see the error in the browser: "w" parameter (width) is required Screenshot 2024-08-16 at 4 34 17 p m

Current vs. Expected behavior

current behavior is the error in the browser: Screenshot 2024-08-16 at 4 34 17 p m

Expected behaviour: The image should resolve correctly in the browser.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.5.0: Wed May  1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000
  Available memory (MB): 32768
  Available CPU cores: 10
Binaries:
  Node: 20.9.0
  npm: 10.1.0
  Yarn: 1.22.21
  pnpm: N/A
Relevant Packages:
  next: 15.0.0-canary.118 // Latest available version is detected (15.0.0-canary.118).
  eslint-config-next: N/A
  react: 19.0.0-rc-49496d49-20240814
  react-dom: 19.0.0-rc-49496d49-20240814
  typescript: 5.3.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Image (next/image)

Which stage(s) are affected? (Select all that apply)

Other (Deployed)

Additional context

This is affecting our application in production and reporting Google Search Console errors Screenshot 2024-08-16 at 4 43 41 p m

Here is a production with wrong encoding

The same image with proper encoding:

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 10
  • Comments: 20 (8 by maintainers)

Most upvoted comments

I wonder if it’s because we’re not URI-encoding the & here → https://github.com/vercel/next.js/blob/ac0d72d2607aac1019b90c1ccc4352d378b2c12f/packages/next/src/shared/lib/image-loader.ts#L77, resulting in the difference between what the browser does + path where image exists <picture data-single-emoji=":frog-eyes:" title=":frog-eyes:">:frog-eyes:</picture>

@samcx Can you please check this discussion https://github.com/vercel/next.js/discussions/37340

@HelaGone can you please correct me If I am wrong

I set a new Next Js with version 15.0.3

Added a new image on the index page

Please check the image @samcx it was taken after the build was complete

I guess this is the default behavior of the next build to make an HTML document during SSG As for the & is HTML entity of &

I hope this causes an issue when the Google Search Console accesses an HTML format URL

image

We are also facing this issue, I hope I can find a permanent solution

@Mohamed-Asar @HelaGone My apologies! I see now. Taking another look!

So, as Sam pointed out earlier, this is because react-dom/server methods escape &, into &amp;, it does the same for other characters.

Like if you do this in the server:

<span>&</span

The resulting HTML would be:

<span>&amp;</span

Even OPs wrong and correct urls end with the same &amp;, when inspected through view-source, or outerHTML.

Image

What throws me off here, is why doesn’t Google Search Console, understand this? Is it still an issue in Search Console? What about the others, in which scenarios is this causing issues for you?

@HelaGone this is issue is typed as a bug. GitHub implemented issue types a few months ago, so we don’t need to use a bug label anymore, but rather sub-type the issue as a bug. That’s what Sam did.

Let me recap this issue. I had made some notes elsewhere.

Hi @samcx any update on this issue ?

Edit by maintainer bot: Comment was automatically minimized because it was considered unhelpful. (If you think this was by mistake, let us know). Please only comment if it adds context to the issue. If you want to express that you have the same problem, use the upvote 👍 on the issue description or subscribe to the issue for updates. Thanks!

@HelaGone @Vignesh-M21 Update on the investigation—

Essentially React is encoding the HTML (turning & into &amp;) after the image-loader.ts runs, and then the DOM updates it to &. Just testing this, and doing the reverse (with .outerHTML) returns what is present in the source code.

CleanShot 2024-11-20 at 12 56 27@2x

CleanShot 2024-11-20 at 12 57 48@2x

Here is a production with wrong encoding

The same image with proper encoding:

Just noticed there are the same links.

I think this is working as expected—it’s unclear about Google Search Console (we need more context on the screenshot in the description). I would expect if this is working in Chrome, the Google Search Console should be fine as well.

@samcx you are getting the image url in inspect element, browser will convert &amp; to &, but if you view the source code like @HelaGone mentioned in the screenshot, you can able to reproduce the issue

@HelaGone I’m currently unable to replicate this with the <picture data-single-emoji=":repro:" title=":repro:">:repro:</picture> on the latest canary (on both Chrome and Safari).

CleanShot 2024-11-18 at 10 57 53@2x

We are also facing this issue. Firefox and Chrome seems to be automatically applying some html decoding on the preload url, thus avoiding this issue, but Safari does not, causing a lot of unnecessary 400 errors on our back end.

@HelaGone I’m also facing the same issue