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

fix(next/image): import error preload is not exported from react-dom #54688

Merged
merged 4 commits into from Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/next/src/client/image-component.tsx
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
@@ -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
@@ -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
@@ -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
@@ -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'`)
})
})
})