question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] ImageComponent does not work when combine `trailingSlash` and `basePath`

See original GitHub issue

Verify 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

  1. clone this project: https://github.com/raulfdm/next-basepath-trailing-image-bug
  2. install the dependencies npm install
  3. run the server npm run dev
  4. open the app at http://localhost:3001/docs/
  5. see the image component failing
  6. press the button that adds the base path
  7. the image should be properly displayed

If you run npm build && npm start, the image will still be broken.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
styflecommented, Jun 21, 2022
0reactions
nmalyarskycommented, Nov 22, 2022

That’s actually not the problem with the trailingSlash per se. It is caused by the images property being explicitly declared in your config file. Without that declaration images variable in server/config.ts is in fact the very same object in memory as the imageConfigDefault one. So no matter what changes are made to the path property, condition images.path === imageConfigDefault.path on line 408 always passes.

It stops working in case of custom images config, because in that scenario imageConfigDefault is left intact (i.e. its path has a constant value of /_next/image) and images object mutates (i.e. path changes to /_next/image/ due to trailingSlash option presence).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found