Skip to content

Commit

Permalink
Add support number quality on Image Component
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts committed Oct 25, 2020
1 parent 9c65c99 commit dab8c14
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
23 changes: 12 additions & 11 deletions packages/next/client/image.tsx
Expand Up @@ -25,7 +25,7 @@ type ImageProps = Omit<
'src' | 'srcSet' | 'ref' | 'width' | 'height' | 'loading'
> & {
src: string
quality?: string
quality?: number | string
priority?: boolean
loading?: LoadingValue
unoptimized?: boolean
Expand Down Expand Up @@ -83,7 +83,7 @@ function getObserver(): IntersectionObserver | undefined {
function computeSrc(
src: string,
unoptimized: boolean,
quality?: string
quality?: number
): string {
if (unoptimized) {
return src
Expand All @@ -94,7 +94,7 @@ function computeSrc(
type CallLoaderProps = {
src: string
width: number
quality?: string
quality?: number
}

function callLoader(loaderProps: CallLoaderProps) {
Expand All @@ -105,7 +105,7 @@ function callLoader(loaderProps: CallLoaderProps) {
type SrcSetData = {
src: string
widths: number[]
quality?: string
quality?: number
}

function generateSrcSet({ src, widths, quality }: SrcSetData): string {
Expand All @@ -121,7 +121,7 @@ type PreloadData = {
widths: number[]
sizes?: string
unoptimized?: boolean
quality?: string
quality?: number
}

function generatePreload({
Expand Down Expand Up @@ -200,13 +200,16 @@ export default function Image({
}
}, [thisEl, lazy])

const parsedQuality =
typeof quality === 'undefined' ? undefined : parseInt(quality as string, 10)

// Generate attribute values
const imgSrc = computeSrc(src, unoptimized, quality)
const imgSrc = computeSrc(src, unoptimized, parsedQuality)
const imgSrcSet = !unoptimized
? generateSrcSet({
src,
widths: configSizes,
quality,
quality: parsedQuality,
})
: undefined

Expand Down Expand Up @@ -301,7 +304,7 @@ export default function Image({
widths: configSizes,
unoptimized,
sizes,
quality,
quality: parsedQuality,
})
: ''}
<img
Expand Down Expand Up @@ -391,7 +394,5 @@ function defaultLoader({ root, src, width, quality }: LoaderProps): string {
}
}

return `${root}?url=${encodeURIComponent(src)}&w=${width}&q=${
quality || '100'
}`
return `${root}?url=${encodeURIComponent(src)}&w=${width}&q=${quality || 100}`
}
12 changes: 12 additions & 0 deletions test/integration/image-component/typescript/pages/valid.tsx
Expand Up @@ -22,6 +22,18 @@ const Page = () => {
src="https://via.placeholder.com/100"
unsized
></Image>
<Image
id="quality-num"
src="https://via.placeholder.com/500"
quality={80}
unsized
></Image>
<Image
id="quality-str"
src="https://via.placeholder.com/500"
quality="80"
unsized
></Image>
<p id="stubtext">This is valid usage of the Image component</p>
</div>
)
Expand Down

0 comments on commit dab8c14

Please sign in to comment.