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
8 changes: 8 additions & 0 deletions .changeset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changesets

Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
with multi-package repos, or single-package repos to help you version and publish your code. You can
find the full documentation for it [in our repository](https://github.com/changesets/changesets).

We have a quick list of common questions to get you started engaging with this project in
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md).
11 changes: 11 additions & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://unpkg.com/@changesets/config@3.1.4/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": false,
"fixed": [],
"linked": [],
"access": "restricted",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
}
5 changes: 5 additions & 0 deletions .changeset/lazy-loading-default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'qwik-image': major
---

Images now render `loading="lazy"` by default instead of loading eagerly. Add the new `priority` prop to above-the-fold/LCP images to render `loading="eager"` and `fetchpriority="high"`; explicit `loading`/`fetchpriority` props take precedence.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ useImageProvider({
Here is the loading values and behaviors https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/loading
default: `lazy`

## priority:

Set `priority` on above-the-fold images (especially the [LCP](https://web.dev/articles/optimize-lcp) image) so they load eagerly with a high fetch priority. It renders `loading="eager"` and `fetchpriority="high"` instead of the default `loading="lazy"`. Use it on at most one or two images per page — never lazy-load the LCP image.

```
<Image
layout="constrained"
width="1200"
height="600"
src={...}
alt={...}
priority
/>
```

## layout values:

### `constrained`
Expand Down
60 changes: 60 additions & 0 deletions apps/qwik-demo-app/src/components/image.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,66 @@ test(`should update img when props change`, async () => {
});
});

test(`should lazy load with async decoding by default`, async () => {
const { screen, render } = await createDOM();
await render(
<QwikCityMockProvider>
<TransformerProvider>
<Image width={400} height={400} layout="constrained" src={SRC} />
</TransformerProvider>
</QwikCityMockProvider>
);

const img = screen.querySelector('img');
expect(img?.getAttribute('loading')).toBe('lazy');
expect(img?.getAttribute('decoding')).toBe('async');
expect(img?.hasAttribute('fetchpriority')).toBe(false);
});

test(`should eagerly load with high fetch priority when priority is set`, async () => {
const { screen, render } = await createDOM();
await render(
<QwikCityMockProvider>
<TransformerProvider>
<Image
width={400}
height={400}
layout="constrained"
src={SRC}
priority
/>
</TransformerProvider>
</QwikCityMockProvider>
);

const img = screen.querySelector('img');
expect(img?.getAttribute('loading')).toBe('eager');
expect(img?.getAttribute('fetchpriority')).toBe('high');
expect(img?.hasAttribute('priority')).toBe(false);
});

test(`should keep explicitly set loading and fetchpriority`, async () => {
const { screen, render } = await createDOM();
await render(
<QwikCityMockProvider>
<TransformerProvider>
<Image
width={400}
height={400}
layout="constrained"
src={SRC}
loading="eager"
fetchpriority="low"
/>
</TransformerProvider>
</QwikCityMockProvider>
);

const img = screen.querySelector('img');
expect(img?.getAttribute('loading')).toBe('eager');
expect(img?.getAttribute('fetchpriority')).toBe('low');
});

function validateImg(
img: HTMLImageElement | null,
props: {
Expand Down
1 change: 1 addition & 0 deletions apps/qwik-demo-app/src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default component$(() => {
placeholder="#e6e6e6"
layout="constrained"
src={providers[selectedProvider].src}
priority
/>
</>
);
Expand Down
12 changes: 12 additions & 0 deletions apps/qwik-demo-app/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { fileURLToPath } from 'node:url';
import { qwikVite } from '@builder.io/qwik/optimizer';
import { qwikCity } from '@builder.io/qwik-city/vite';
import { defineConfig } from 'vite';
Expand Down Expand Up @@ -36,5 +37,16 @@ export default defineConfig({
},
environment: 'node',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
alias: {
// qwikVite serves the client variant of this virtual module during
// vitest runs, which crashes in the node environment; the dist module
// detects browser vs server at runtime instead
'@builder.io/qwik/build': fileURLToPath(
new URL(
'../../node_modules/@builder.io/qwik/dist/build/index.mjs',
import.meta.url
)
),
},
},
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"devDependencies": {
"@builder.io/qwik": "1.14.1",
"@builder.io/qwik-city": "1.14.1",
"@changesets/cli": "^2.31.0",
"@commitlint/cli": "17.6.1",
"@commitlint/config-angular": "17.6.1",
"@commitlint/config-conventional": "17.6.1",
Expand Down
10 changes: 9 additions & 1 deletion packages/qwik-image/src/lib/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ export interface ImageProps extends ImageAttributes {
| 'inherit'
| 'initial';
fetchpriority?: 'low' | 'high' | 'auto';
/**
* Marks the image as high-priority (above-the-fold/LCP image): renders
* `loading="eager"` and `fetchpriority="high"` instead of the default
* `loading="lazy"`. Explicit `loading`/`fetchpriority` props take precedence.
*/
priority?: boolean;
}

export const ImageContext = createContextId<ImageState>('ImageContext');
Expand Down Expand Up @@ -215,11 +221,13 @@ export const getBreakpoints = ({
export const Image = component$<ImageProps>((props) => {
const srcSetSig = useSignal<string>();
const state = useContext(ImageContext);
const { resolutions, imageTransformer$, ...imageAttributes } = {
const { resolutions, imageTransformer$, priority, ...imageAttributes } = {
...state,
...props,
};
const imageAttributesWithoutChildren = {
loading: priority ? ('eager' as const) : ('lazy' as const),
fetchpriority: priority ? ('high' as const) : undefined,
...imageAttributes,
children: undefined,
};
Expand Down
Loading
Loading