Skip to content

Commit

Permalink
Refactor experimental config name to better reflect intent
Browse files Browse the repository at this point in the history
  • Loading branch information
Joonpark13 committed Apr 16, 2021
1 parent c74a188 commit ffa2db7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ export default async function getBaseWebpackConfig(
domains: config.images.domains,
}
: {}),
placeholder: config.experimental.imagePlaceholder,
enableBlurryPlaceholder: config.experimental.enableBlurryPlaceholder,
}),
'process.env.__NEXT_ROUTER_BASEPATH': JSON.stringify(config.basePath),
'process.env.__NEXT_HAS_REWRITES': JSON.stringify(hasRewrites),
Expand Down
26 changes: 17 additions & 9 deletions packages/next/client/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
imageConfigDefault,
LoaderValue,
VALID_LOADERS,
PlaceholderValue,
} from '../next-server/server/image-config'
import { useIntersection } from './use-intersection'

Expand Down Expand Up @@ -46,6 +45,11 @@ const VALID_LAYOUT_VALUES = [
] as const
type LayoutValue = typeof VALID_LAYOUT_VALUES[number]

enum PlaceholderValue {
BLURRY = 'blurry',
EMPTY = 'empty',
}

type ImgElementStyle = NonNullable<JSX.IntrinsicElements['img']['style']>

export type ImageProps = Omit<
Expand Down Expand Up @@ -79,7 +83,7 @@ export type ImageProps = Omit<
placeholder?: Exclude<PlaceholderValue, PlaceholderValue.BLURRY>
blurDataURL: never
}
| { placeholder: PlaceholderValue; blurDataURL: string }
| { placeholder: PlaceholderValue.BLURRY; blurDataURL: string }
)

const {
Expand All @@ -88,7 +92,7 @@ const {
loader: configLoader,
path: configPath,
domains: configDomains,
placeholder: configPlaceholder,
enableBlurryPlaceholder: configEnableBlurryPlaceholder,
} =
((process.env.__NEXT_IMAGE_OPTS as any) as ImageConfig) || imageConfigDefault
// sort smallest to largest
Expand Down Expand Up @@ -223,10 +227,10 @@ function removePlaceholder(
placeholder: PlaceholderValue
) {
const hasBlurryPlaceholder =
configPlaceholder === PlaceholderValue.BLURRY &&
placeholder === PlaceholderValue.BLURRY
configEnableBlurryPlaceholder && placeholder === PlaceholderValue.BLURRY
if (hasBlurryPlaceholder && element) {
if (element.complete) {
// If the real image fails to load, this will still remove the placeholder. This is the desired behavior for now, and will be revisited when error handling is worked on for the image component itself.
element.style.backgroundImage = 'none'
} else {
element.onload = () => {
Expand All @@ -236,6 +240,10 @@ function removePlaceholder(
}
}

const defaultPlaceholder = configEnableBlurryPlaceholder
? PlaceholderValue.BLURRY
: PlaceholderValue.EMPTY

export default function Image({
src,
sizes,
Expand All @@ -249,7 +257,7 @@ export default function Image({
objectFit,
objectPosition,
loader = defaultImageLoader,
placeholder = configPlaceholder,
placeholder = defaultPlaceholder,
blurDataURL,
...all
}: ImageProps) {
Expand Down Expand Up @@ -320,16 +328,16 @@ export default function Image({
const heightInt = getInt(height)
const qualityInt = getInt(quality)

const MIN_IMG_SIZE_FOR_PLACEHOLDER = 100
const MIN_IMG_SIZE_FOR_PLACEHOLDER = 5000
const tooSmallForBlurryPlaceholder =
widthInt && heightInt
? widthInt * heightInt < MIN_IMG_SIZE_FOR_PLACEHOLDER
: false
const shouldShowBlurryPlaceholder =
configPlaceholder === PlaceholderValue.BLURRY &&
configEnableBlurryPlaceholder &&
placeholder === PlaceholderValue.BLURRY &&
priority &&
(layout === 'fill' || !tooSmallForBlurryPlaceholder)
!tooSmallForBlurryPlaceholder

let wrapperStyle: JSX.IntrinsicElements['div']['style'] | undefined
let sizerStyle: JSX.IntrinsicElements['div']['style'] | undefined
Expand Down
6 changes: 3 additions & 3 deletions packages/next/next-server/server/config-shared.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os from 'os'
import { Header, Redirect, Rewrite } from '../../lib/load-custom-routes'
import { imageConfigDefault, PlaceholderValue } from './image-config'
import { imageConfigDefault } from './image-config'

export type DomainLocales = Array<{
http?: true
Expand Down Expand Up @@ -60,7 +60,7 @@ export type NextConfig = { [key: string]: any } & {
skipValidation?: boolean
}
turboMode: boolean
imagePlaceholder: PlaceholderValue
enableBlurryPlaceholder: boolean
}
}

Expand Down Expand Up @@ -116,7 +116,7 @@ export const defaultConfig: NextConfig = {
externalDir: false,
serialWebpackBuild: false,
turboMode: false,
imagePlaceholder: PlaceholderValue.EMPTY,
enableBlurryPlaceholder: false,
},
future: {
strictPostcssConfiguration: false,
Expand Down
9 changes: 2 additions & 7 deletions packages/next/next-server/server/image-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@ export const VALID_LOADERS = [

export type LoaderValue = typeof VALID_LOADERS[number]

export enum PlaceholderValue {
EMPTY = 'empty',
BLURRY = 'blurry',
}

export type ImageConfig = {
deviceSizes: number[]
imageSizes: number[]
loader: LoaderValue
path: string
domains?: string[]
placeholder: PlaceholderValue
enableBlurryPlaceholder: boolean
}

export const imageConfigDefault: ImageConfig = {
Expand All @@ -27,5 +22,5 @@ export const imageConfigDefault: ImageConfig = {
path: '/_next/image',
loader: 'default',
domains: [],
placeholder: PlaceholderValue.EMPTY,
enableBlurryPlaceholder: false,
}
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ describe('Image Component Tests', () => {
module.exports = {
target: 'serverless',
experimental: {
imagePlaceholder: 'blurry',
enableBlurryPlaceholder: true,
},
}
`
Expand Down

0 comments on commit ffa2db7

Please sign in to comment.