diff --git a/packages/next/client/image.tsx b/packages/next/client/image.tsx index 184c1a84af3530a..69ad812509c68a7 100644 --- a/packages/next/client/image.tsx +++ b/packages/next/client/image.tsx @@ -29,8 +29,8 @@ type ImageProps = Omit< loading?: LoadingValue unoptimized?: boolean } & ( - | { width: number; height: number; unsized?: false } - | { width?: number; height?: number; unsized: true } + | { width: number | string; height: number | string; unsized?: false } + | { width?: number | string; height?: number | string; unsized: true } ) const imageData: ImageData = process.env.__NEXT_IMAGE_OPTS as any @@ -229,16 +229,18 @@ export default function Image({ className = className ? className + ' __lazy' : '__lazy' } - // No need to add preloads on the client side--by the time the application is hydrated, - // it's too late for preloads - const shouldPreload = priority && typeof window === 'undefined' - let divStyle: React.CSSProperties | undefined let imgStyle: React.CSSProperties | undefined let wrapperStyle: React.CSSProperties | undefined - if (typeof height === 'number' && typeof width === 'number' && !unsized) { - // - const quotient = height / width + if ( + typeof height !== 'undefined' && + typeof width !== 'undefined' && + !unsized + ) { + // + // + const quotient = + parseInt(height as string, 10) / parseInt(width as string, 10) const ratio = isNaN(quotient) ? 1 : quotient * 100 wrapperStyle = { maxWidth: '100%', @@ -278,6 +280,10 @@ export default function Image({ } } + // No need to add preloads on the client side--by the time the application is hydrated, + // it's too late for preloads + const shouldPreload = priority && typeof window === 'undefined' + return (
diff --git a/test/integration/image-component/default/pages/index.js b/test/integration/image-component/default/pages/index.js index b1f7ef53e142ed8..bdbd9edc72b7dc3 100644 --- a/test/integration/image-component/default/pages/index.js +++ b/test/integration/image-component/default/pages/index.js @@ -5,7 +5,7 @@ const Page = () => { return (

Hello World

- +

This is the index page

diff --git a/test/integration/image-component/default/test/index.test.js b/test/integration/image-component/default/test/index.test.js index f698189e3b4f558..f6deaf57b064339 100644 --- a/test/integration/image-component/default/test/index.test.js +++ b/test/integration/image-component/default/test/index.test.js @@ -29,6 +29,7 @@ function runTests() { const result = await browser.eval( `document.getElementById('basic-image').naturalWidth` ) + if (result === 0) { throw new Error('Incorrectly loaded image') } @@ -36,10 +37,15 @@ function runTests() { return 'result-correct' }, /result-correct/) + await browser.eval( + 'document.getElementById("unsized-image").scrollIntoView()' + ) + await check(async () => { const result = await browser.eval( `document.getElementById('unsized-image').naturalWidth` ) + if (result === 0) { throw new Error('Incorrectly loaded image') } diff --git a/test/integration/image-component/typescript/pages/valid.tsx b/test/integration/image-component/typescript/pages/valid.tsx index 7236e64209dd741..e2d1934d8ef2e23 100644 --- a/test/integration/image-component/typescript/pages/valid.tsx +++ b/test/integration/image-component/typescript/pages/valid.tsx @@ -6,11 +6,17 @@ const Page = () => {

Hello World

+