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

Got wrong url when importing some svg files #18034

Open
7 tasks done
hungtcs opened this issue Sep 5, 2024 · 5 comments
Open
7 tasks done

Got wrong url when importing some svg files #18034

hungtcs opened this issue Sep 5, 2024 · 5 comments

Comments

@hungtcs
Copy link

hungtcs commented Sep 5, 2024

Describe the bug

This problem only occurs in prod mode.

There are two svg files in reproduction that are imported in the same way, but get different results

import url_01_url from "./assets/01.svg?url";
import url_02_url from "./assets/02.svg?url";
01: /assets/01-CAOkh7Dq.svg
02: data:image/svg+xml,%3csvg%20xmlns='http://www.

Reproduction

https://github.com/hungtcs/vite-svg-assets-reproduction/blob/master/src/main.tsx

Steps to reproduce

git clone [email protected]:hungtcs/vite-svg-assets-reproduction.git
cd vite-svg-assets-reproduction
pnpm run build
pnpm run preview

System Info

System:
  OS: macOS 14.6.1
  CPU: (8) arm64 Apple M3
  Memory: 67.17 MB / 24.00 GB
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 22.6.0 - ~/.local/state/fnm_multishells/17635_1725528034762/bin/node
  npm: 10.8.2 - ~/.local/state/fnm_multishells/17635_1725528034762/bin/npm
  pnpm: 9.7.0 - ~/.local/state/fnm_multishells/17635_1725528034762/bin/pnpm
  bun: 1.1.25 - /opt/homebrew/bin/bun
Browsers:
  Chrome: 128.0.6613.119
  Safari: 17.6
npmPackages:
  @vitejs/plugin-react: ^4.3.1 => 4.3.1 
  vite: ^5.4.1 => 5.4.3

Used Package Manager

pnpm

Logs

No response

Validations

@PengBoUESTC
Copy link
Contributor

There is a config about img convert assetsInlineLimit
https://vitejs.dev/config/build-options.html#build-assetsinlinelimit

@hungtcs
Copy link
Author

hungtcs commented Sep 6, 2024

There is a config about img convert assetsInlineLimit https://vitejs.dev/config/build-options.html#build-assetsinlinelimit

Thanks @PengBoUESTC It is indeed related to this configuration, setting to 0 solves the problem.


But what puzzles me is that I found 3 parameters for importing files in client.d.ts, and there is no description of the ?inline parameter in the documentation, and using it has no effect.

For some files, I want to explicitly import them using inline or url, instead of automatically processing it by size.

The other side is that this behavior is not uniform in dev mode and prod mode, which can lead to issues that are difficult to troubleshoot in dev mode and are only discovered after going prod.

declare module '*?raw' {
  const src: string
  export default src
}

declare module '*?url' {
  const src: string
  export default src
}

declare module '*?inline' {
  const src: string
  export default src
}

@PengBoUESTC
Copy link
Contributor

There is a config about img convert assetsInlineLimit https://vitejs.dev/config/build-options.html#build-assetsinlinelimit

Thanks @PengBoUESTC It is indeed related to this configuration, setting to 0 solves the problem.

But what puzzles me is that I found 3 parameters for importing files in client.d.ts, and there is no description of the ?inline parameter in the documentation, and using it has no effect.

For some files, I want to explicitly import them using inline or url, instead of automatically processing it by size.

The other side is that this behavior is not uniform in dev mode and prod mode, which can lead to issues that are difficult to troubleshoot in dev mode and are only discovered after going prod.

declare module '*?raw' {
  const src: string
  export default src
}

declare module '*?url' {
  const src: string
  export default src
}

declare module '*?inline' {
  const src: string
  export default src
}

first of all : there is a default assets file test about filetoURL

assetsInclude(file: string) {
      return DEFAULT_ASSETS_RE.test(file) || assetsFilter(file)
    }

so that if u didn't use url query, the svg will be compiled by vite:asset plugin too,

and then , I notice that vite handle the file in different way

export async function fileToUrl(
  id: string,
  config: ResolvedConfig,
  ctx: PluginContext,
): Promise<string> {
  if (config.command === 'serve') {
    return fileToDevUrl(id, config)
  } else {
    return fileToBuiltUrl(id, config, ctx)
  }
}

last, I think that Base64 url is a kind of url, the difference is file compiled or not.

@hungtcs
Copy link
Author

hungtcs commented Sep 6, 2024

@PengBoUESTC I totally agree with you that Data URLs are also a type of URLs. Now there are only two final questions:

  1. Is it possible to explicitly specify whether the type of import is a dataurl or a regular url? (there are always some people who have some strange needs).
  2. Is it possible to get consistent behavior in dev and prod modes?

So far assetsInlineLimit solves my problem, and the above is what I think could be taken a step further.

@PengBoUESTC
Copy link
Contributor

t

1: maybe a assetsInlineLimit function config is enough

    assetsInlineLimit: (id) => {
      return id.includes('01')
    }

2: not sure about this +_+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants