Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
b0a1e996
Unverified
Commit
b0a1e996
authored
Dec 23, 2022
by
Steven
Committed by
GitHub
Dec 23, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix `next/image` 404 when `basePath` and `trailingSlash` defined (#44312)
- Fixes #36681
parent
f0aa10b2
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
98 additions
and
4 deletions
+98
-4
config.ts
packages/next/server/config.ts
+4
-4
next.config.js
...next-image-new/both-basepath-trailingslash/next.config.js
+7
-0
index.js
...next-image-new/both-basepath-trailingslash/pages/index.js
+22
-0
test.jpg
...ext-image-new/both-basepath-trailingslash/public/test.jpg
+0
-0
index.test.ts
...-image-new/both-basepath-trailingslash/test/index.test.ts
+65
-0
No files found.
packages/next/server/config.ts
View file @
b0a1e996
...
@@ -447,6 +447,10 @@ function assignDefaults(dir: string, userConfig: { [key: string]: any }) {
...
@@ -447,6 +447,10 @@ function assignDefaults(dir: string, userConfig: { [key: string]: any }) {
)
)
}
}
if
(
images
.
path
===
imageConfigDefault
.
path
&&
result
.
basePath
)
{
images
.
path
=
`
${
result
.
basePath
}${
images
.
path
}
`
}
// Append trailing slash for non-default loaders and when trailingSlash is set
// Append trailing slash for non-default loaders and when trailingSlash is set
if
(
images
.
path
)
{
if
(
images
.
path
)
{
if
(
if
(
...
@@ -458,10 +462,6 @@ function assignDefaults(dir: string, userConfig: { [key: string]: any }) {
...
@@ -458,10 +462,6 @@ function assignDefaults(dir: string, userConfig: { [key: string]: any }) {
}
}
}
}
if
(
images
.
path
===
imageConfigDefault
.
path
&&
result
.
basePath
)
{
images
.
path
=
`
${
result
.
basePath
}${
images
.
path
}
`
}
if
(
images
.
loaderFile
)
{
if
(
images
.
loaderFile
)
{
if
(
images
.
loader
!==
'default'
&&
images
.
loader
!==
'custom'
)
{
if
(
images
.
loader
!==
'default'
&&
images
.
loader
!==
'custom'
)
{
throw
new
Error
(
throw
new
Error
(
...
...
test/integration/next-image-new/both-basepath-trailingslash/next.config.js
0 → 100644
View file @
b0a1e996
module
.
exports
=
{
basePath
:
'/prefix'
,
trailingSlash
:
true
,
images
:
{
deviceSizes
:
[
640
,
828
],
},
}
test/integration/next-image-new/both-basepath-trailingslash/pages/index.js
0 → 100644
View file @
b0a1e996
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
=
"import-img"
alt
=
"import-img"
src
=
{
img
}
priority
/>
<
br
/>
<
Image
id
=
"string-img"
alt
=
"string-img"
src
=
"/prefix/test.jpg"
width
=
{
200
}
height
=
{
200
}
/
>
<
/div
>
)
}
export
default
Page
test/integration/next-image-new/both-basepath-trailingslash/public/test.jpg
0 → 100644
View file @
b0a1e996
6.61 KB
test/integration/next-image-new/both-basepath-trailingslash/test/index.test.ts
0 → 100644
View file @
b0a1e996
/* eslint-env jest */
import
{
fetchViaHTTP
,
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
const
runTests
=
()
=>
{
it
(
'should correctly load image src from import'
,
async
()
=>
{
expect
.
assertions
(
3
)
const
browser
=
await
webdriver
(
appPort
,
'/prefix/'
)
const
img
=
await
browser
.
elementById
(
'import-img'
)
const
src
=
await
img
.
getAttribute
(
'src'
)
expect
(
src
).
toBe
(
'/prefix/_next/image/?url=%2Fprefix%2F_next%2Fstatic%2Fmedia%2Ftest.fab2915d.jpg&w=828&q=75'
)
const
res
=
await
fetchViaHTTP
(
appPort
,
src
)
expect
(
res
.
status
).
toBe
(
200
)
expect
(
res
.
headers
.
get
(
'content-type'
)).
toBe
(
'image/jpeg'
)
})
it
(
'should correctly load image src from string'
,
async
()
=>
{
expect
.
assertions
(
3
)
const
browser
=
await
webdriver
(
appPort
,
'/prefix/'
)
const
img
=
await
browser
.
elementById
(
'string-img'
)
const
src
=
await
img
.
getAttribute
(
'src'
)
expect
(
src
).
toBe
(
'/prefix/_next/image/?url=%2Fprefix%2Ftest.jpg&w=640&q=75'
)
const
res
=
await
fetchViaHTTP
(
appPort
,
src
)
expect
(
res
.
status
).
toBe
(
200
)
expect
(
res
.
headers
.
get
(
'content-type'
)).
toBe
(
'image/jpeg'
)
})
}
describe
(
'Image Component basePath + trailingSlash Tests'
,
()
=>
{
describe
(
'dev mode'
,
()
=>
{
beforeAll
(
async
()
=>
{
appPort
=
await
findPort
()
app
=
await
launchApp
(
appDir
,
appPort
)
})
afterAll
(()
=>
killApp
(
app
))
runTests
()
})
describe
(
'server mode'
,
()
=>
{
beforeAll
(
async
()
=>
{
await
nextBuild
(
appDir
)
appPort
=
await
findPort
()
app
=
await
nextStart
(
appDir
,
appPort
)
})
afterAll
(()
=>
killApp
(
app
))
runTests
()
})
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment