Skip to content

Commit c9984d8

Browse files
committed
Add support number quality on Image Component
1 parent 379f4c6 commit c9984d8

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

packages/next/client/image.tsx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type ImageProps = Omit<
2626
'src' | 'srcSet' | 'ref' | 'width' | 'height' | 'loading'
2727
> & {
2828
src: string
29-
quality?: string
29+
quality?: number | string
3030
priority?: boolean
3131
loading?: LoadingValue
3232
unoptimized?: boolean
@@ -104,8 +104,8 @@ function getDeviceSizes(width: number | undefined): number[] {
104104
function computeSrc(
105105
src: string,
106106
unoptimized: boolean,
107-
width: number | undefined,
108-
quality?: string
107+
width?: number,
108+
quality?: number
109109
): string {
110110
if (unoptimized) {
111111
return src
@@ -118,7 +118,7 @@ function computeSrc(
118118
type CallLoaderProps = {
119119
src: string
120120
width: number
121-
quality?: string
121+
quality?: number
122122
}
123123

124124
function callLoader(loaderProps: CallLoaderProps) {
@@ -129,8 +129,8 @@ function callLoader(loaderProps: CallLoaderProps) {
129129
type SrcSetData = {
130130
src: string
131131
unoptimized: boolean
132-
width: number | undefined
133-
quality: string | undefined
132+
width?: number
133+
quality?: number
134134
}
135135

136136
function generateSrcSet({
@@ -155,7 +155,7 @@ type PreloadData = {
155155
unoptimized: boolean
156156
width: number | undefined
157157
sizes?: string
158-
quality?: string
158+
quality?: number
159159
}
160160

161161
function generatePreload({
@@ -251,8 +251,10 @@ export default function Image({
251251
}
252252
}, [thisEl, lazy])
253253

254-
let widthInt = getInt(width)
255-
let heightInt = getInt(height)
254+
const widthInt = getInt(width)
255+
const heightInt = getInt(height)
256+
const qualityInt = getInt(quality)
257+
256258
let divStyle: React.CSSProperties | undefined
257259
let imgStyle: React.CSSProperties | undefined
258260
let wrapperStyle: React.CSSProperties | undefined
@@ -305,12 +307,12 @@ export default function Image({
305307
}
306308

307309
// Generate attribute values
308-
const imgSrc = computeSrc(src, unoptimized, widthInt, quality)
310+
const imgSrc = computeSrc(src, unoptimized, widthInt, qualityInt)
309311
const imgSrcSet = generateSrcSet({
310312
src,
311313
width: widthInt,
312314
unoptimized,
313-
quality,
315+
quality: qualityInt,
314316
})
315317

316318
let imgAttributes:
@@ -352,7 +354,7 @@ export default function Image({
352354
width: widthInt,
353355
unoptimized,
354356
sizes,
355-
quality,
357+
quality: qualityInt,
356358
})
357359
: ''}
358360
<img
@@ -442,7 +444,5 @@ function defaultLoader({ root, src, width, quality }: LoaderProps): string {
442444
}
443445
}
444446

445-
return `${root}?url=${encodeURIComponent(src)}&w=${width}&q=${
446-
quality || '100'
447-
}`
447+
return `${root}?url=${encodeURIComponent(src)}&w=${width}&q=${quality || 100}`
448448
}

test/integration/image-component/typescript/pages/valid.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ const Page = () => {
2222
src="https://via.placeholder.com/100"
2323
unsized
2424
></Image>
25+
<Image
26+
id="quality-num"
27+
src="https://via.placeholder.com/500"
28+
quality={80}
29+
unsized
30+
></Image>
31+
<Image
32+
id="quality-str"
33+
src="https://via.placeholder.com/500"
34+
quality="80"
35+
unsized
36+
></Image>
2537
<p id="stubtext">This is valid usage of the Image component</p>
2638
</div>
2739
)

0 commit comments

Comments
 (0)