Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: incorrect url encoding in <link rel="preload"> for prioritized images #68998

Open
HelaGone opened this issue Aug 16, 2024 · 17 comments
Open
Labels
Image (next/image) Related to Next.js Image Optimization. linear: next Confirmed issue that is tracked by the Next.js team.

Comments

@HelaGone
Copy link

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 &amp; 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:

@HelaGone HelaGone added the bug Issue was opened via the bug report template. label Aug 16, 2024
@github-actions github-actions bot added the Image (next/image) Related to Next.js Image Optimization. label Aug 16, 2024
@fersan96

This comment has been minimized.

@HelaGone
Copy link
Author

hello Vercel / Next team. Do you have any update or eta to review this bug? Is there something we can do to solve this issue without losing the image optimization?

@github-actions github-actions bot added the linear: next Confirmed issue that is tracked by the Next.js team. label Sep 9, 2024
@Mohamed-Asar
Copy link

@HelaGone I'm also facing the same issue

@ica-nis
Copy link

ica-nis commented Oct 10, 2024

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.

@samcx
Copy link
Member

samcx commented Nov 18, 2024

@HelaGone I'm currently unable to replicate this with the :repro: on the latest canary (on both Chrome and Safari).

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

@samcx
Copy link
Member

samcx commented Nov 18, 2024

@HelaGone Sorry I made a typo, I meant to say I was unable to replicate

@HelaGone
Copy link
Author

@samcx so do you think this will be solved in the latest releases?

@samcx
Copy link
Member

samcx commented Nov 18, 2024

@HelaGone Possibly. Can you try verifying that it's fixed for you on the latest stable or canary?

@Mohamed-Asar
Copy link

Mohamed-Asar commented Nov 20, 2024

@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

@samcx
Copy link
Member

samcx commented Nov 20, 2024

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

@Vignesh-M21
Copy link

@samcx Can you please check this discussion #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

@samcx
Copy link
Member

samcx commented Nov 20, 2024

I wonder if it's because we're not URI-encoding the & here →

return `${config.path}?url=${encodeURIComponent(src)}&w=${width}&q=${
, resulting in the difference between what the browser does + path where image exists :frog-eyes:

@samcx
Copy link
Member

samcx commented Nov 20, 2024

@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.

@Vignesh-M21
Copy link

I missed your message @samcx

Yeah, I guess so.

I look at the most recent two-month data on 404 Image URLs on our website.

I confirm it does not happen on all the images. It thought randomly on most of the pages

Would it be possible to relate to load time if it is high?

@ica-nis
Copy link

ica-nis commented Nov 25, 2024

@samcx As I wrote in a previous post, both Firefox and Chrome seems to be handling this by doing some HTML decoding on the preload url. Safari on the other hand seems not to be able to handle it, sending the raw '&' instead of '&', resulting in a 400 response from Next.

@HelaGone
Copy link
Author

HelaGone commented Dec 5, 2024

Hi @samcx The bad encoding in Chrome is resulting in a lot of 404 errors in Google Search Console. We're not using the priority attribute anymore because of this. I'm confused about how Chrome is handling the encoding as expected but It's throwing an error: "w" parameter (width) is required that confuses me. The issue here is that that encoding of the & > &amp; is not properly decoded neither in chrome nor GSC, causing the increase in 404 errors on the GSC report on those image urls with the bad encoding. Let me know if you need more evidence of the issue.

@Mohamed-Asar
Copy link

Mohamed-Asar commented Dec 21, 2024

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!

@samcx samcx removed the bug Issue was opened via the bug report template. label Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Image (next/image) Related to Next.js Image Optimization. linear: next Confirmed issue that is tracked by the Next.js team.
Projects
None yet
Development

No branches or pull requests

6 participants