Unverified Commit 9b22406e authored by Gustavo Edinger's avatar Gustavo Edinger Committed by GitHub

fix: Prevent image redirection when trailingSlash is set (#29640)

* fix: Prevent image redirection when trailingSlash is set

* test: Trailing slash test for next/image

* lint-fix
Co-authored-by: 's avatarGustavo Edinger <gbe@nect.com>
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
Co-authored-by: 's avatarSteven <steven@ceriously.com>
parent 6455bae3
...@@ -287,11 +287,12 @@ function assignDefaults(userConfig: { [key: string]: any }) { ...@@ -287,11 +287,12 @@ function assignDefaults(userConfig: { [key: string]: any }) {
) )
} }
// Append trailing slash for non-default loaders // Append trailing slash for non-default loaders and when trailingSlash is set
if (images.path) { if (images.path) {
if ( if (
images.loader !== 'default' && (images.loader !== 'default' &&
images.path[images.path.length - 1] !== '/' images.path[images.path.length - 1] !== '/') ||
result.trailingSlash
) { ) {
images.path += '/' images.path += '/'
} }
......
import React from 'react'
import Image from 'next/image'
import img from '../public/test.jpg'
const Page = () => {
return (
<div>
<p>Trailing Slash</p>
<Image id="test1" src={img} />
</div>
)
}
export default Page
/* eslint-env jest */
import {
findPort,
killApp,
launchApp,
nextBuild,
nextStart,
} from 'next-test-utils'
import webdriver from 'next-webdriver'
import { join } from 'path'
const appDir = join(__dirname, '../')
let appPort
let app
describe('Image Component Trailing Slash Tests', () => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
it('should include trailing slash when trailingSlash is set on config file during next dev', async () => {
expect.assertions(1)
let browser
try {
browser = await webdriver(appPort, '/')
const id = 'test1'
const srcImage = await browser.eval(
`document.getElementById('${id}').src`
)
expect(srcImage).toMatch(
/\/_next\/image\/\?url=%2F_next%2Fstatic%2Fimage%2Fpublic%2Ftest(.+).jpg&w=828&q=75/
)
} finally {
if (browser) {
await browser.close()
}
}
})
})
describe('server mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
it('should include trailing slash when trailingSlash is set on config file during next start', async () => {
expect.assertions(1)
let browser
try {
browser = await webdriver(appPort, '/')
const id = 'test1'
const srcImage = await browser.eval(
`document.getElementById('${id}').src`
)
expect(srcImage).toMatch(
/\/_next\/image\/\?url=%2F_next%2Fstatic%2Fimage%2Fpublic%2Ftest(.+).jpg&w=828&q=75/
)
} finally {
if (browser) {
await browser.close()
}
}
})
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment