vitejs / vitejs/vite

Fetch an imported asset URL responds with wrong content in dev mode

Open
#13,636 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

pending triage
Dominant language
TypeScript
Stars
82.9k
Forks
8.8k
Avg merge
1d 23h
Merged PRs (30d)
61

Description

Describe the bug
  1. Create an asset named text with content I'm a text file.
  2. Add the asset to assetsInclude in vite.config.js: assetsInclude: ["text"],
  3. Import the asset: import BinUrl from './text';
  4. Use fetch to retrieve its content: fetch(BinUrl).then(response => response.text()).then(text => document.querySelector('#app').innerHTML = text);

In dev mode, the result is export default "/text"

In preview mode, the result is I'm a text file. (correct)

Reproduction

https://stackblitz.com/edit/vitejs-vite-whfdz5?file=main.js

Steps to reproduce
  1. npm install && npm run dev
System Info
System:
    OS: macOS 12.4
    CPU: (4) x64 Intel(R) Core(TM) i7-7567U CPU @ 3.50GHz
    Memory: 31.76 MB / 24.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.16.0 - /usr/local/opt/node@18/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.5.1 - /usr/local/opt/node@18/bin/npm
  Browsers:
    Chrome: 114.0.5735.133
    Safari: 15.5
  npmPackages:
    vite: ^4.3.9 => 4.3.9
Used Package Manager

npm

Logs

No response

Validations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked StackBlitz reproduction, then inspect vite.config.js and main.js while comparing npm run dev with preview mode. Reproduce the fetch and make the dev-mode response return the asset contents, matching the preview result instead of export default "/text".

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
build-system, devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.