Skip to content

Commit

Permalink
fix(next/image): import error preload is not exported from `react-d…
Browse files Browse the repository at this point in the history
…om` (#54688)

- Fixes #54416
- Related to #47659 

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
  • Loading branch information
styfle and ijjk authored Aug 29, 2023
1 parent 0ef4761 commit 2122e57
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/next/src/client/image-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import React, {
forwardRef,
version,
} from 'react'
import { preload } from 'react-dom'
import ReactDOM from 'react-dom'
import Head from '../shared/lib/head'
import { getImgProps } from '../shared/lib/get-img-props'
import type {
Expand Down Expand Up @@ -320,9 +320,9 @@ function ImagePreload({
...getDynamicProps(imgAttributes.fetchPriority),
}

if (isAppRouter && preload) {
if (isAppRouter && ReactDOM.preload) {
// See https://github.com/facebook/react/pull/26940
preload(
ReactDOM.preload(
imgAttributes.src,
// @ts-expect-error TODO: upgrade to `@types/react-dom@18.3.x`
opts
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react'
import Image from 'next/image'

export const config = { runtime: 'experimental-edge' }

function loader({ src, width, quality }) {
return `${src}?wid=${width}&qual=${quality || 35}`
}
Expand Down
6 changes: 6 additions & 0 deletions test/integration/next-image-new/middleware/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Image from 'next/image'

export async function middleware(request) {
// reference Image so it's not tree shaken / DCE
console.log(`Has image: ${Boolean(Image)}`)
}
15 changes: 15 additions & 0 deletions test/integration/next-image-new/middleware/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Image from 'next/image'

export default function Page() {
return (
<>
<h1>Its Works</h1>
<Image
alt="empty"
src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
width={400}
height={400}
/>
</>
)
}
40 changes: 40 additions & 0 deletions test/integration/next-image-new/middleware/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* eslint-env jest */

import { join } from 'path'
import { check, findPort, killApp, launchApp } from 'next-test-utils'
import webdriver from 'next-webdriver'

const appDir = join(__dirname, '../')
let appPort: number
let app: Awaited<ReturnType<typeof launchApp>>
let output = ''

describe('Image with middleware in edge func', () => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort, {
onStdout: (s) => {
output += s
},
onStderr: (s) => {
output += s
},
})
})
afterAll(async () => {
await killApp(app)
})

it('should not error', async () => {
/**
- wait compiling /middleware (client and server)...
- warn ../../../../packages/next/dist/esm/client/image-component.js
Attempted import error: 'preload' is not exported from 'react-dom' (imported as 'preload').
*/
await webdriver(appPort, '/')
await check(() => output, /compiled client and server successfully/)
expect(output).not.toContain(`'preload' is not exported from 'react-dom'`)
})
})
})

0 comments on commit 2122e57

Please sign in to comment.