Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
Don't emit duplicate image files (vercel#26843)
Browse files Browse the repository at this point in the history
fixes vercel#26607

This change makes it so the image loader plugin only emits a file while processing an image import for the client. The final generated image URL was already the same in SSR and CSR anyway, so this change doesn't have any functional impact.

I also changed the name of the static page in the image component tests, since it was causing some conflicts with the static assets folder.
  • Loading branch information
atcastle authored Jul 2, 2021
1 parent 36621fb commit 6a1006f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
3 changes: 3 additions & 0 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,9 @@ export default async function getBaseWebpackConfig(
loader: 'next-image-loader',
issuer: { not: regexLikeCss },
dependency: { not: ['url'] },
options: {
isServer,
},
},
]
: []),
Expand Down
5 changes: 4 additions & 1 deletion packages/next/build/webpack/loaders/next-image-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const BLUR_QUALITY = 70
const VALID_BLUR_EXT = ['jpeg', 'png', 'webp']

async function nextImageLoader(content) {
const isServer = loaderUtils.getOptions(this).isServer
const context = this.rootContext
const opts = { context, content }
const interpolatedName = loaderUtils.interpolateName(
Expand Down Expand Up @@ -46,7 +47,9 @@ async function nextImageLoader(content) {
blurDataURL,
})

this.emitFile(interpolatedName, content, null)
if (!isServer) {
this.emitFile(interpolatedName, content, null)
}

return `${'export default '} ${stringifiedData};`
}
Expand Down
8 changes: 8 additions & 0 deletions test/integration/image-component/default/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from 'next-test-utils'
import webdriver from 'next-webdriver'
import { join } from 'path'
import { existsSync } from 'fs'

jest.setTimeout(1000 * 80)

Expand Down Expand Up @@ -555,6 +556,13 @@ function runTests(mode) {
/Image with src (.*)jpg(.*) is smaller than 40x40. Consider removing(.*)/gm
)
})
} else {
//server-only tests
it('should not create an image folder in server/chunks', async () => {
expect(
existsSync(join(appDir, '.next/server/chunks/static/image'))
).toBeFalsy()
})
}

it('should correctly ignore prose styles', async () => {
Expand Down
6 changes: 3 additions & 3 deletions test/integration/image-component/default/test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let app
let browser
let html

const indexPage = new File(join(appDir, 'pages/static.js'))
const indexPage = new File(join(appDir, 'pages/static-img.js'))

const runTests = () => {
it('Should allow an image with a static src to omit height and width', async () => {
Expand Down Expand Up @@ -97,8 +97,8 @@ describe('Static Image Component Tests', () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
html = await renderViaHTTP(appPort, '/static')
browser = await webdriver(appPort, '/static')
html = await renderViaHTTP(appPort, '/static-img')
browser = await webdriver(appPort, '/static-img')
})
afterAll(() => {
killApp(app)
Expand Down

0 comments on commit 6a1006f

Please sign in to comment.