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

Change Image component lazy=true to loading=lazy #18138

Merged
merged 7 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 7 additions & 8 deletions packages/next/client/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ type ImageData = {

type ImageProps = Omit<
JSX.IntrinsicElements['img'],
'src' | 'srcSet' | 'ref' | 'width' | 'height'
'src' | 'srcSet' | 'ref' | 'width' | 'height' | 'loading'
> & {
src: string
quality?: string
priority?: boolean
lazy?: boolean
loading?: 'lazy' | 'eager'
unoptimized?: boolean
} & (
| { width: number; height: number; unsized?: false }
Expand Down Expand Up @@ -142,7 +142,7 @@ export default function Image({
sizes,
unoptimized = false,
priority = false,
lazy,
loading,
className,
quality,
width,
Expand All @@ -152,17 +152,16 @@ export default function Image({
}: ImageProps) {
const thisEl = useRef<HTMLImageElement>(null)

// Sanity Checks:
// If priority and lazy are present, log an error and use priority only.
if (priority && lazy) {
if (priority && loading === 'lazy') {
if (process.env.NODE_ENV !== 'production') {
throw new Error(
`Image with src "${src}" has both "priority" and "lazy" properties. Only one should be used.`
`Image with src "${src}" has both "priority" and "loading=lazy" properties. Only one should be used.`
)
}
}

if (!priority && typeof lazy === 'undefined') {
let lazy = loading === 'lazy'
if (!priority && typeof loading === 'undefined') {
lazy = true
}

Expand Down
8 changes: 4 additions & 4 deletions test/integration/image-component/basic/pages/client-side.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const ClientSide = () => {
<Image
id="basic-image"
src="foo.jpg"
lazy={false}
lazy="eager"
styfle marked this conversation as resolved.
Show resolved Hide resolved
width={300}
height={400}
quality={60}
Expand All @@ -18,7 +18,7 @@ const ClientSide = () => {
id="attribute-test"
data-demo="demo-value"
src="bar.jpg"
lazy={false}
lazy="eager"
width={300}
height={400}
/>
Expand All @@ -27,15 +27,15 @@ const ClientSide = () => {
data-demo="demo-value"
host="secondary"
src="foo2.jpg"
lazy={false}
lazy="eager"
width={300}
height={400}
/>
<Image
id="unoptimized-image"
unoptimized
src="https://arbitraryurl.com/foo.jpg"
lazy={false}
lazy="eager"
width={300}
height={400}
/>
Expand Down
8 changes: 4 additions & 4 deletions test/integration/image-component/basic/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Page = () => {
<Image
id="basic-image"
src="foo.jpg"
lazy={false}
lazy="eager"
width={300}
height={400}
quality={60}
Expand All @@ -18,7 +18,7 @@ const Page = () => {
id="attribute-test"
data-demo="demo-value"
src="bar.jpg"
lazy={false}
lazy="eager"
width={300}
height={400}
/>
Expand All @@ -27,15 +27,15 @@ const Page = () => {
data-demo="demo-value"
host="secondary"
src="foo2.jpg"
lazy={false}
lazy="eager"
width={300}
height={400}
/>
<Image
id="unoptimized-image"
unoptimized
src="https://arbitraryurl.com/foo.jpg"
lazy={false}
lazy="eager"
width={300}
height={400}
/>
Expand Down