-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
fix: check srcset when hydrating to prevent needless requests #8868
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing! Thank you so much for working on this!!
I'm afraid it's not quite working as it's not taking into account the possible width or pixel density descriptor: https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/srcset#value
What do you mean by that exactly? In which case would it result in a false negative or false positive? You're right that if the widths are different it's technically wrong and we should account for the "url is the same but width descriptor is different" case - is that what you mean? |
It seems my explanation was incorrect. I noticed that we weren't checking for the descriptors and jumped to the wrong conclusion and mis-interpretted the debugger output.
It wasn't, but we probably should account for that as you say. What's really happening It seems that the existing Here's a screenshot showing that the temporary element Here's a screenshot showing |
I don't understand - the idea is that assigning to the |
Ah, you might be right. I was thinking it was the reverse (convert them to relative) and maybe Chrome changed or something. So then I guess
Yes. It looks as below (also visible on https://c3.ventures/)
|
Ok now I'm confused aswell - how is the second parameter becoming a full URL but the first is not? |
reading img.src returns the resolved URL, the second parameter has full URLs on the client-side because of how vite and vitest output code for SSR IIRC, it's just string URLs on the server output but on the client output it's emitted as |
Yeah I just noticed that, too - either that's Vite or it's vite-imagetools, but regardless of who it is, that's why it happens. So we need to account for the things being the other way around, too. |
Ok @benmccann I think this is ready for another test on your site - according to my tests on svelte.dev this should work as expected now. I also added some explanation and a test. |
I wonder if perhaps we should see whether it'd be possible to fix Vite to output the same in SSR and on the client. That would remove the need for this PR I think reducing complication in the codebase and perform better |
For reference, here's the generated code:
I believe In SvelteKit, if I change:
To:
Then I get as output:
|
beforeAll(() => { | ||
const host = 'https://svelte.dev'; | ||
let _href = ''; | ||
// @ts-ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we've been using @ts-ignore
as a TODO and @ts-expect-error
if it's something that we intend to leave like that permanently. it could probably use a comment next to it either way
it's a shame you'd have to do this though. Vitest doesn't show it in their example: https://vitest.dev/config/#environment. I wonder if this is something worth bringing up with them or something that we can fix with a global TS declaration
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why I needed this, I don't see the error anymore - removed it
I just tested this PR and it's fixed the issue for me. It seems to be running about 5-6x faster now! I sent sveltejs/kit#10287 to also improve things on the SvelteKit side |
Co-authored-by: Ben McCann <[email protected]>
fixes #8838
@benmccann could you test if this fixes the issue?
Before submitting the PR, please make sure you do the following
feat:
,fix:
,chore:
, ordocs:
.Tests and linting
pnpm test
and lint the project withpnpm lint