Unverified Commit 4e74db2d authored by Yubin's avatar Yubin Committed by GitHub

Add `lazyBoundary` prop to Image component (#27258)

This PR adds `lazyBoundary` prop on Image Component.
This feature is to load the images earlier.
I'm not good at English. So, I couldn't explain enough in the documentation what `lazyBoundary` is.

Feature request: https://github.com/vercel/next.js/discussions/24552

## Feature

- [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [x] Make sure the linting passes
parent 1d347ba5
......@@ -16,6 +16,7 @@ description: Enable Image Optimization with the built-in Image component.
| Version | Changes |
| --------- | ------------------------------------------------------------------------------------------------- |
| `v11.1.0` | `lazyBoundary` prop added. |
| `v11.0.0` | `src` prop support for static import.<br/>`placeholder` prop added.<br/>`blurDataURL` prop added. |
| `v10.0.5` | `loader` prop added. |
| `v10.0.1` | `layout` prop added. |
......@@ -234,6 +235,12 @@ Try it out:
You can also [generate a solid color Data URL](https://png-pixel.com) to match the image.
### lazyBoundary
A string (with similar syntax to the margin property) that acts as the bounding box used to detect the intersection of the viewport with the image and trigger lazy [loading](#loading). Defaults to `"200px"`.
[Learn more](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/rootMargin)
### unoptimized
When true, the source image will be served as-is instead of changing quality,
......
......@@ -97,6 +97,7 @@ export type ImageProps = Omit<
quality?: number | string
priority?: boolean
loading?: LoadingValue
lazyBoundary?: string
placeholder?: PlaceholderValue
blurDataURL?: string
unoptimized?: boolean
......@@ -285,6 +286,7 @@ export default function Image({
unoptimized = false,
priority = false,
loading,
lazyBoundary = '200px',
className,
quality,
width,
......@@ -428,7 +430,7 @@ export default function Image({
}
const [setRef, isIntersected] = useIntersection<HTMLImageElement>({
rootMargin: '200px',
rootMargin: lazyBoundary,
disabled: !isLazy,
})
const isVisible = !isLazy || isIntersected
......
......@@ -46,6 +46,14 @@ const Lazy = () => {
height={400}
width={1900}
></Image>
<div style={{ height: '2000px' }}></div>
<Image
id="lazy-boundary"
src="lazy6.jpg"
height={400}
width={800}
lazyBoundary="0px 0px 500px 0px"
></Image>
<Link href="/missing-observer">
<a id="observerlink">observer</a>
</Link>
......
......@@ -205,6 +205,32 @@ function lazyLoadingTests() {
await browser.elementById('eager-loading').getAttribute('srcset')
).toBeTruthy()
})
it('should load the sixth image, which has lazyBoundary property after scrolling down', async () => {
expect(await browser.elementById('lazy-boundary').getAttribute('src')).toBe(
emptyImage
)
expect(
await browser.elementById('lazy-boundary').getAttribute('srcset')
).toBeFalsy()
let viewportHeight = await browser.eval(`window.innerHeight`)
let topOfBottomImage = await browser.eval(
`document.getElementById('lazy-boundary').parentElement.offsetTop`
)
let buffer = 500
await browser.eval(
`window.scrollTo(0, ${topOfBottomImage - (viewportHeight + buffer)})`
)
await waitFor(200)
expect(await browser.elementById('lazy-boundary').getAttribute('src')).toBe(
'https://example.com/myaccount/lazy6.jpg?auto=format&fit=max&w=1600'
)
expect(
await browser.elementById('lazy-boundary').getAttribute('srcset')
).toBe(
'https://example.com/myaccount/lazy6.jpg?auto=format&fit=max&w=1024 1x, https://example.com/myaccount/lazy6.jpg?auto=format&fit=max&w=1600 2x'
)
})
}
async function hasPreloadLinkMatchingUrl(url) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment