[BUG] ImageComponent does not work when combine `trailingSlash` and `basePath`
See original GitHub issueVerify canary release
- I verified that the issue exists in Next.js canary release
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 21.4.0: Fri Mar 18 00:46:32 PDT 2022; root:xnu-8020.101.4~15/RELEASE_ARM64_T6000
Binaries:
Node: 16.14.2
npm: 7.24.2
Yarn: 1.22.17
pnpm: 6.27.1
Relevant packages:
next: 12.1.7-canary.1
react: 17.0.2
react-dom: 17.0.2
What browser are you using? (if relevant)
Microsoft Edge Version 101.0.1210.32 (Official build) (arm64)
How are you deploying your application? (if relevant)
next start
Describe the Bug
When we combine basePath
with trailingSlash
, the next/image
component does not applies the correct src
and srcSet
.
It always generates /_next/...
instead /<base-path>/_next/...
.
That leads to images not loading properly.
Expected Behavior
Add the basePath
into the generated src
and srcSets
so the image can be loaded properly.
To Reproduce
- clone this project: https://github.com/raulfdm/next-basepath-trailing-image-bug
- install the dependencies
npm install
- run the server
npm run dev
- open the app at
http://localhost:3001/docs/
- see the image component failing
- press the button that adds the base path
- the image should be properly displayed
If you run npm build && npm start
, the image will still be broken.
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top Results From Across the Web
NextJS v10 Image component fails to serve image when there ...
How I am supposed to define the src property while there is a basePath to be able to serve the images? <Image src="/me.jpg"...
Read more >next/image - Next.js
This next/image component uses browser native lazy loading, which may fallback to eager loading for older browsers before Safari 15.4.
Read more >Trailing slash on basePath causing issues. [#2908218] - Drupal
Problem /Motivation Currently the ns.basePath has a trailing slash which is causing double slash on including assets like config.js, ...
Read more >Should You Have a Trailing Slash at the End of URLs? - Ahrefs
A trailing slash is a forward slash (“/”) placed at the end of a URL such as domain.com/ or domain.com/page/. But should you...
Read more >Should I configure my URLs with a leading and/or trailing slash?
Do not use bare strings to record URIs. Most languages have a type for them that handles joining absolute and relative paths automatically, ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Looks like the order of these two if statements needs to be flipped:
https://github.com/vercel/next.js/blob/1f68a5df526cc8085fa83ba2781cfa8ce28189f6/packages/next/server/config.ts#L398-L410
That’s actually not the problem with the
trailingSlash
per se. It is caused by theimages
property being explicitly declared in your config file. Without that declarationimages
variable inserver/config.ts
is in fact the very same object in memory as theimageConfigDefault
one. So no matter what changes are made to thepath
property, conditionimages.path === imageConfigDefault.path
on line 408 always passes.It stops working in case of custom
images
config, because in that scenarioimageConfigDefault
is left intact (i.e. itspath
has a constant value of/_next/image
) andimages
object mutates (i.e.path
changes to/_next/image/
due totrailingSlash
option presence).