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 image loader file emission path for edge runtime #50683

Merged
merged 3 commits into from Jun 2, 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
2 changes: 1 addition & 1 deletion packages/next/src/build/webpack-config.ts
Expand Up @@ -2124,8 +2124,8 @@ export default async function getBaseWebpackConfig(
not: [new RegExp(WEBPACK_RESOURCE_QUERIES.metadata)],
},
options: {
isServer: isNodeServer || isEdgeServer,
isDev: dev,
compilerType,
basePath: config.basePath,
assetPrefix: config.assetPrefix,
},
Expand Down
@@ -1,9 +1,12 @@
import type { CompilerNameValues } from '../../../../shared/lib/constants'

import path from 'path'
import loaderUtils from 'next/dist/compiled/loader-utils3'
import { getImageSize } from '../../../../server/image-optimizer'
import { getBlurImage } from './blur'

interface Options {
isServer: boolean
compilerType: CompilerNameValues
isDev: boolean
assetPrefix: string
basePath: string
Expand All @@ -13,7 +16,7 @@ function nextImageLoader(this: any, content: Buffer) {
const imageLoaderSpan = this.currentTraceSpan.traceChild('next-image-loader')
return imageLoaderSpan.traceAsyncFn(async () => {
const options: Options = this.getOptions()
const { isServer, isDev, assetPrefix, basePath } = options
const { compilerType, isDev, assetPrefix, basePath } = options
const context = this.rootContext

const opts = { context, content }
Expand Down Expand Up @@ -63,14 +66,18 @@ function nextImageLoader(this: any, content: Buffer) {
})
)

if (isServer) {
if (compilerType === 'client') {
this.emitFile(interpolatedName, content, null)
} else {
this.emitFile(
`../${isDev ? '' : '../'}${interpolatedName}`,
path.join(
'..',
isDev || compilerType === 'edge-server' ? '' : '..',
interpolatedName
),
content,
null
)
} else {
this.emitFile(interpolatedName, content, null)
}

return `export default ${stringifiedData};`
Expand Down
16 changes: 16 additions & 0 deletions test/integration/next-image-new/default/pages/edge.js
@@ -0,0 +1,16 @@
import Image from 'next/image'

import profilePic from '../public/small.jpg'

export const runtime = 'experimental-edge'

function About() {
return (
<>
<h1>edge</h1>
<Image src={profilePic} alt="Picture of the author in edge runtime" />
</>
)
}

export default About
12 changes: 10 additions & 2 deletions test/integration/next-image-new/default/test/index.test.ts
Expand Up @@ -17,7 +17,8 @@ import {
} from 'next-test-utils'
import webdriver from 'next-webdriver'
import { join } from 'path'
import { existsSync } from 'fs'
import fs from 'fs/promises'
import { pathExists } from 'fs-extra'

const appDir = join(__dirname, '../')

Expand Down Expand Up @@ -1051,7 +1052,7 @@ function runTests(mode) {
//server-only tests
it('should not create an image folder in server/chunks', async () => {
expect(
existsSync(join(appDir, '.next/server/chunks/static/media'))
await pathExists(join(appDir, '.next/server/chunks/static/media'))
).toBeFalsy()
})
it('should render as unoptimized with missing src prop', async () => {
Expand Down Expand Up @@ -1300,6 +1301,13 @@ function runTests(mode) {
const computedHeight = await getComputed(browser, id, 'height')
expect(getRatio(computedHeight, computedWidth)).toBeCloseTo(0.75, 1)
})

it('should create images folder in static/media for edge runtime', async () => {
const files = await fs.readdir(join(appDir, '.next/static/media'))
expect(files).toEqual(
expect.arrayContaining([expect.stringMatching(/small\.\w+\.jpg/)])
)
})
}

it('should have blurry placeholder when enabled', async () => {
Expand Down