Skip to content
Merged
7 changes: 5 additions & 2 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2680,9 +2680,12 @@ export default async function build(
workerResult.prerenderedRoutes.length > 0 &&
// in PPR, we create fallback routes with empty params.
// If at least one non-empty param exists, it must've been
// generated by `generateStaticParams`.
// generated by `generateStaticParams`. Static metadata files
// use a placeholder path with empty params instead.
workerResult.prerenderedRoutes.some(
(route) => !isEmptyParams(route.params)
(route) =>
isStaticMetadataFile(route.pathname) ||
!isEmptyParams(route.params)
)
if (isDynamic && !hasGenerateStaticParams) {
if (config.output === 'export') {
Expand Down
35 changes: 3 additions & 32 deletions packages/next/src/client/image-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import React, {
useRef,
useEffect,
useContext,
useMemo,
useState,
forwardRef,
use,
Expand All @@ -20,21 +19,14 @@ import type {
OnLoadingComplete,
PlaceholderValue,
} from '../shared/lib/get-img-props'
import type {
ImageConfigComplete,
ImageLoaderProps,
} from '../shared/lib/image-config'
import { imageConfigDefault } from '../shared/lib/image-config'
import { ImageConfigContext } from '../shared/lib/image-config-context.shared-runtime'
import type { ImageLoaderProps } from '../shared/lib/image-config'
import { RouterContext } from '../shared/lib/router-context.shared-runtime'
import { useImageConfig } from './use-image-config'

// This is replaced by webpack alias
import defaultLoader from 'next/dist/shared/lib/image-loader'
import { useMergedRef } from './use-merged-ref'

// This is replaced by webpack define plugin
const configEnv = process.env.__NEXT_IMAGE_OPTS as any as ImageConfigComplete

if (typeof window === 'undefined') {
;(globalThis as any).__NEXT_IMAGE_IMPORTED = true
}
Expand Down Expand Up @@ -380,28 +372,7 @@ export const Image = forwardRef<HTMLImageElement | null, ImageProps>(
// We're in the app directory if there is no pages router.
const isAppRouter = !pagesRouter

const configContext = useContext(ImageConfigContext)
const config = useMemo(() => {
const c = configEnv || configContext || imageConfigDefault

const allSizes = [...c.deviceSizes, ...c.imageSizes].sort((a, b) => a - b)
const deviceSizes = c.deviceSizes.sort((a, b) => a - b)
const qualities = c.qualities?.sort((a, b) => a - b)
return {
...c,
allSizes,
deviceSizes,
qualities,
// During the SSR, configEnv (__NEXT_IMAGE_OPTS) does not include
// security sensitive configs like `localPatterns`, which is needed
// during the server render to ensure it's validated. Therefore use
// configContext, which holds the config from the server for validation.
localPatterns:
typeof window === 'undefined'
? configContext?.localPatterns
: c.localPatterns,
}
}, [configContext])
const config = useImageConfig()

const { onLoad, onLoadingComplete } = props
const onLoadRef = useRef(onLoad)
Expand Down
43 changes: 12 additions & 31 deletions packages/next/src/client/legacy/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,18 @@ import React, {
useRef,
useEffect,
useCallback,
useContext,
useMemo,
useState,
type JSX,
} from 'react'
import * as ReactDOM from 'react-dom'
import Head from '../../shared/lib/head'
import {
imageConfigDefault,
VALID_LOADERS,
} from '../../shared/lib/image-config'
import { VALID_LOADERS } from '../../shared/lib/image-config'
import type {
ImageConfigComplete,
PreparedImageConfig,
LoaderValue,
} from '../../shared/lib/image-config'
import { useIntersection } from '../use-intersection'
import { ImageConfigContext } from '../../shared/lib/image-config-context.shared-runtime'
import { useImageConfig } from '../use-image-config'
import { warnOnce } from '../../shared/lib/utils/warn-once'
import { normalizePathTrailingSlash } from '../normalize-trailing-slash'
import { findClosestQuality } from '../../shared/lib/find-closest-quality'
Expand All @@ -31,7 +26,6 @@ function normalizeSrc(src: string): string {
}

const supportsFloat = typeof ReactDOM.preload === 'function'
const configEnv = process.env.__NEXT_IMAGE_OPTS as any as ImageConfigComplete
const loadedImageURLs = new Set<string>()
const allImgs = new Map<
string,
Expand All @@ -47,7 +41,7 @@ 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[] }
type ImageConfig = PreparedImageConfig
export type ImageLoader = (resolverProps: ImageLoaderProps) => string

export type ImageLoaderProps = {
Expand Down Expand Up @@ -223,7 +217,13 @@ function defaultLoader({
// We use dynamic require because this should only error in development
const { hasRemoteMatch } =
require('../../shared/lib/match-remote-pattern') as typeof import('../../shared/lib/match-remote-pattern')
if (!hasRemoteMatch(config.domains, config.remotePatterns, parsedSrc)) {
if (
!hasRemoteMatch(
config.domains ?? [],
config.remotePatterns ?? [],
parsedSrc
)
) {
throw new Error(
`Invalid src prop (${src}) on \`next/image\`, hostname "${parsedSrc.hostname}" is not configured under images in your \`next.config.js\`\n` +
`See more info: https://nextjs.org/docs/messages/next-image-unconfigured-host`
Expand Down Expand Up @@ -701,26 +701,7 @@ export default function Image({
blurDataURL,
...all
}: ImageProps) {
const configContext = useContext(ImageConfigContext)
const config: ImageConfig = useMemo(() => {
const c = configEnv || configContext || imageConfigDefault
const allSizes = [...c.deviceSizes, ...c.imageSizes].sort((a, b) => a - b)
const deviceSizes = c.deviceSizes.sort((a, b) => a - b)
const qualities = c.qualities?.sort((a, b) => a - b)
return {
...c,
allSizes,
deviceSizes,
qualities, // During the SSR, configEnv (__NEXT_IMAGE_OPTS) does not include
// security sensitive configs like `localPatterns`, which is needed
// during the server render to ensure it's validated. Therefore use
// configContext, which holds the config from the server for validation.
localPatterns:
typeof window === 'undefined'
? configContext?.localPatterns
: c.localPatterns,
}
}, [configContext])
const config: ImageConfig = useImageConfig()

let rest: Partial<ImageProps> = all
let layout: NonNullable<LayoutValue> = sizes ? 'responsive' : 'intrinsic'
Expand Down
18 changes: 18 additions & 0 deletions packages/next/src/client/use-image-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use client'

import { useContext } from 'react'
import {
prepareImageConfig,
type ImageConfigForRendering,
} from '../shared/lib/image-config'
import { ImageConfigContext } from '../shared/lib/image-config-context.shared-runtime'

// This is replaced by the bundler define plugin.
const configEnv = process.env.__NEXT_IMAGE_OPTS as any as
| ImageConfigForRendering
| undefined

export function useImageConfig() {
const configContext = useContext(ImageConfigContext)
return prepareImageConfig(configEnv, configContext)
}
Loading
Loading