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

feat: set a better type for the default image loader #41639

Merged
merged 9 commits into from Oct 24, 2022
Merged
13 changes: 4 additions & 9 deletions packages/next/client/image.tsx
Expand Up @@ -13,6 +13,8 @@ import { getImageBlurSvg } from '../shared/lib/image-blur-svg'
import {
ImageConfigComplete,
imageConfigDefault,
ImageLoaderProps,
ImageLoaderPropsWithConfig,
} from '../shared/lib/image-config'
import { ImageConfigContext } from '../shared/lib/image-config-context'
import { warnOnce } from '../shared/lib/utils'
Expand All @@ -33,21 +35,14 @@ if (typeof window === 'undefined') {
const VALID_LOADING_VALUES = ['lazy', 'eager', undefined] as const
type LoadingValue = typeof VALID_LOADING_VALUES[number]
type ImageConfig = ImageConfigComplete & { allSizes: number[] }
export type ImageLoader = (p: ImageLoaderProps) => string

export type ImageLoaderProps = {
amirhhashemi marked this conversation as resolved.
Show resolved Hide resolved
src: string
width: number
quality?: number
}
export { ImageLoaderProps }
export type ImageLoader = (p: ImageLoaderProps) => string

// Do not export - this is an internal type only
// because `next.config.js` is only meant for the
// built-in loaders, not for a custom loader() prop.
type ImageLoaderWithConfig = (p: ImageLoaderPropsWithConfig) => string
type ImageLoaderPropsWithConfig = ImageLoaderProps & {
config: Readonly<ImageConfig>
}

type PlaceholderValue = 'blur' | 'empty'
type OnLoad = React.ReactEventHandler<HTMLImageElement> | undefined
Expand Down
10 changes: 10 additions & 0 deletions packages/next/shared/lib/image-config.ts
Expand Up @@ -8,6 +8,16 @@ export const VALID_LOADERS = [

export type LoaderValue = typeof VALID_LOADERS[number]

export type ImageLoaderProps = {
src: string
width: number
quality?: number
}

export type ImageLoaderPropsWithConfig = ImageLoaderProps & {
config: Readonly<ImageConfig>
}

export type RemotePattern = {
/**
* Must be `http` or `https`.
Expand Down
10 changes: 8 additions & 2 deletions packages/next/shared/lib/image-loader.ts
@@ -1,5 +1,11 @@
// TODO: change "any" to actual type
function defaultLoader({ config, src, width, quality }: any): string {
import type { ImageLoaderPropsWithConfig } from './image-config'

function defaultLoader({
config,
src,
width,
quality,
}: ImageLoaderPropsWithConfig): string {
if (process.env.NODE_ENV !== 'production') {
const missingValues = []

Expand Down
@@ -0,0 +1,11 @@
import Image from 'next/image'
import type { ImageProps, ImageLoader, ImageLoaderProps } from 'next/image'

function myLoader({ src, width, quality }: ImageLoaderProps): string {
return `https://example.com/${src}?w=${width}&q=${quality || 75}`
}
const loader: ImageLoader = myLoader

export function ImageWithLoader(props: Omit<ImageProps, 'loader'>) {
return <Image loader={loader} {...props} />
}
8 changes: 8 additions & 0 deletions test/integration/next-image-new/typescript/pages/valid.tsx
Expand Up @@ -5,6 +5,7 @@ import svg from '../public/test.svg'
import avif from '../public/test.avif'
import { ImageCard } from '../components/image-card'
import { DynamicSrcImage } from '../components/image-dynamic-src'
import { ImageWithLoader } from '../components/image-with-loader'

const Page = () => {
return (
Expand Down Expand Up @@ -95,6 +96,13 @@ const Page = () => {
width={400}
height={400}
/>
<ImageWithLoader
id="image-with-loader"
alt="image-with-loader"
src="test.jpg"
width={300}
height={300}
/>
<p id="stubtext">This is valid usage of the Image component</p>
</div>
)
Expand Down