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
- build the application
- start the server
- visit the provided url
- right click and select view source code
- search for the link
rel=preloadof theimageSrcSet
- see the wrong encoding of the
&it’s&and should be&or%26 - copy the path and concatenate the path with the
localhost:3000 - see the error in the browser:
"w" parameter (width) is required
Current vs. Expected behavior
current behavior is the error in the browser:
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
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)
I wonder if it’s because we’re not URI-encoding the
</picture>
&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:">@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
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/servermethods escape&, into&, it does the same for other characters.Like if you do this in the server:
The resulting HTML would be:
Even OPs
wrongandcorrecturls end with the same&, when inspected through view-source, or outerHTML.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
buglabel 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&) after theimage-loader.tsruns, and then the DOM updates it to&. Just testing this, and doing the reverse (with.outerHTML) returns what is present in the source code.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
&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:">
</picture> on the latest canary (on both Chrome and Safari).
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