Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lazy-source-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/image": patch
---

A lazy `SolidImage` no longer renders its `<source>` tags until it is eager or near the viewport, so the browser no longer downloads an offscreen variant before the image scrolls into view.
28 changes: 28 additions & 0 deletions src/__tests__/browser/solid-image.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,34 @@ describe("SolidImage in the browser", () => {
expect(sources[0]!.srcset).toBe(`${PIXEL} 400w,${PIXEL} 800w`);
});

it("does not fetch the preferred variant before the image scrolls into view", async () => {
const stamp = Date.now();
const preferred = new URL(`/lazy-variant-${stamp}.webp`, location.href).href;
const fallback = new URL(`/lazy-variant-${stamp}.png`, location.href).href;
const { host, scrollIntoView } = mount(() => (
<SolidImage
src={{ source: PIXEL, width: 1600, height: 900, options: {} }}
alt="pixel"
transformer={{
transform: () => [
{ path: preferred, width: 400, type: "image/webp" },
{ path: fallback, width: 400, type: "image/png" },
],
}}
/>
));

await expect.poll(() => host.querySelector("noscript")).toBe(null);
await new Promise(resolve => setTimeout(resolve, 100));

expect(host.querySelector("source")).toBe(null);
expect(performance.getEntriesByName(preferred)).toHaveLength(0);

scrollIntoView();

await expect.poll(() => performance.getEntriesByName(preferred)).toHaveLength(1);
});

it("drops the inline placeholder once the image has loaded", async () => {
const { host, scrollIntoView } = mount(() => (
<SolidImage
Expand Down
23 changes: 23 additions & 0 deletions src/__tests__/components.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,26 @@ describe("SolidImage SSR", () => {
expect(outsideNoscript).not.toContain("hero.png");
});

it("renders no <source> for a lazy image on the server", () => {
const html = renderToString(() => (
<SolidImage
src={{ source: "/hero.png", width: 1600, height: 900, options: {} }}
alt="hero"
transformer={{
transform: () => [
{ path: "/hero-400.webp", width: 400, type: "image/webp" },
{ path: "/hero-400.jpg", width: 400, type: "image/jpeg" },
],
}}
fallback={() => <div>loading</div>}
/>
));

const outsideNoscript = html.replace(/<noscript[^>]*>.*?<\/noscript>/gs, "");
expect(outsideNoscript).not.toContain("<source");
expect(outsideNoscript).not.toContain("hero-400");
});

it("renders without a fallback", () => {
const html = renderToString(() => (
<SolidImage
Expand All @@ -150,6 +170,7 @@ describe("SolidImage SSR", () => {
<SolidImage
src={{ source: "/hero.png", width: 1600, height: 900, options: {} }}
alt="hero"
eager
sizes="(max-width: 600px) 100vw, 50vw"
transformer={{
transform: () => [
Expand All @@ -169,6 +190,7 @@ describe("SolidImage SSR", () => {
<SolidImage
src={{ source: "/hero.png", width: 1600, height: 900, options: {} }}
alt="hero"
eager
transformer={{
transform: () => [{ path: "/hero-400.webp", width: 400, type: "image/webp" }],
}}
Expand Down Expand Up @@ -239,6 +261,7 @@ describe("SolidImage SSR", () => {
<SolidImage
src={{ source: "/hero.png", width: 1600, height: 900, options: {} }}
alt="hero"
eager
transformer={{
transform: () => [
{ path: "/hero-400.webp", width: 400, type: "image/webp" },
Expand Down
5 changes: 4 additions & 1 deletion src/core/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,10 @@ export function SolidImage<T>(props: SolidImageProps<T>): JSX.Element {
<div ref={laze.ref} data-solid-image="container">
<div data-solid-image="aspect-ratio" style={boxStyle()}>
<picture data-solid-image="picture">
<SolidImageSources groups={groups()} sizes={props.sizes} />
{/* A picture's source is fetched on sight, whatever the img's src. */}
<Show when={visible()}>
<SolidImageSources groups={groups()} sizes={props.sizes} />
</Show>
<ClientOnly
fallback={
// An eager image is rendered in full, so the browser finds it
Expand Down
Loading