Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiled a wrong asset path(‘/base/’ prefix) in the development environment #3239

Closed
4 tasks done
imafee opened this issue Nov 23, 2023 · 3 comments · Fixed by #3258
Closed
4 tasks done

Compiled a wrong asset path(‘/base/’ prefix) in the development environment #3239

imafee opened this issue Nov 23, 2023 · 3 comments · Fixed by #3258
Labels
bug Something isn't working build Related to the build system upstream Related to the dependencies

Comments

@imafee
Copy link

imafee commented Nov 23, 2023

Describe the bug

When I:

  • installed 'vitepress v1.0.0-rc.25' and executed 'npm run docs: dev', any image displayed normally.
  • installed 'vitepress v1.0.0-rc.26' and executed 'npm run docs: dev', any image displayed a 404 error. After manually adding the prefix {vitepressconfig. base} before the path of the image in the domElementPanel(devtool), it displayed normally.

The problem of compilating wrong assets path does not have any issues in production environment (when you 'npm run docs: build' and 'npm run docs: preview'), only occurs in the development environment.

Reproduction

Init project

npm i -D vitepress@vitepress v1.0.0-rc.26
npx vitepress init
/* choosed this options
Where should VitePress initialize the config? "./docs"
Theme: Default Theme (Out of the box, good-looking docs)
*/

Create directory structure

docs/
   .vitepress/
      config.mts
   assets/
      image/
        cat.png
   index.md

Add base property in .vitepress/config.mts

import { defineConfig } from "vitepress";
export default defineConfig({
base:'/base/'
})

Add a image in index.md
![cat](/assets/image/cat.png)

Start development environment
npm run docs:dev

Then, in the network-panel(chrome dev-tool) you will find that the image request is 404.
open the dom-panel and modify the image path from '/assets/image/cat. png' to '/base/assets/image/cat. png', it will be normal.

However, if you install vitepress@vitepress V1.0.0-rc.25, everything is normal. It seems that there has been a breaking-change in the asset path compilation, but I did not see it in the CHANGLOG.

Expected behavior

In the development environment

/base/assets/image/cat.png`   // it's wrong,please don't compile to this
/assets/image/cat.png   // it's correct!

System Info

System:
    OS: macOS 13.6.1
    CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
    Memory: 1.97 GB / 16.00 GB
    Shell: 5.9 - /bin/zsh
Binaries:
    Node: 20.9.0 - ~/.nvm/versions/node/v20.9.0/bin/node
    npm: 10.1.0 - ~/.nvm/versions/node/v20.9.0/bin/npm
Browsers:
    Chrome: 119.0.6045.159
    Edge: 119.0.2151.72
    Safari: 17.1
npmPackages:
    vitepress: ^1.0.0-rc.29 => 1.0.0-rc.29`

Additional context

I really want to fix this issue, but I can't located yet😢 ...

Validations

@imafee imafee added the bug: pending triage Maybe a bug, waiting for confirmation label Nov 23, 2023
@imafee imafee changed the title Using the wrong asset path(‘/base/’ prefix) in the development environment Compile wrong asset path(‘/base/’ prefix) in the development environment Nov 23, 2023
@imafee imafee changed the title Compile wrong asset path(‘/base/’ prefix) in the development environment Compiled a wrong asset path(‘/base/’ prefix) in the development environment Nov 23, 2023
@brc-dd
Copy link
Member

brc-dd commented Nov 23, 2023

/base/assets/image/cat.png // it's wrong, please don't compile to this

That is correct actually. Even in rc.25 after build you'll get paths like /base/assets/cat.hash.png.

In rc.26 we switched to vite 5, which disallows serving files outside base (vitejs/vite#5657). This is also correct behavior IMO.

The inconsistency in dev and build should be fixed though. In the meantime, please use relative paths -
![cat](./assets/image/cat.png)

@brc-dd brc-dd added bug Something isn't working build Related to the build system and removed bug: pending triage Maybe a bug, waiting for confirmation labels Nov 23, 2023
@imafee
Copy link
Author

imafee commented Nov 23, 2023

I may not have explained the main point of problem clearly 😢

Whether in rc.25 or rc.26, the image resource path will be compiled as the correct(same) result:

  • code '/sets/image/cat. png'
  • compiled as'/sets/image/cat. png 'in the development environment,
  • compiled as'/base/sets/cat. hash. png' in the product environment.
  • I don't put the assets outside the docs folder(/docs/assets/image/cat.png)

But, In the development environment, they behave differently:

  • rc.25 can display normally,
  • rc.26 reports a 404 error.

@brc-dd
Copy link
Member

brc-dd commented Nov 23, 2023

Yeah, I understood your problem.

The issue is vite 4 (rc 25) served docs/assets/image/cat.png at both /assets/image/cat.png and /base/assets/image/cat.png. But vite 5 (rc 26) only serves it at /base/assets/image/cat.png. See the linked PR in my above comment. The build (production) behavior is not affected by that.

@brc-dd brc-dd added the upstream Related to the dependencies label Nov 24, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working build Related to the build system upstream Related to the dependencies
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants