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.

Assets with a dynamic URL are ignored

See original GitHub issue

Is your feature request related to a problem? Please describe.

When something like <img src=example.png> is used in a Vue template, vite build handles it by copying the PNG file into the dist directory with a hash in the filename etc. However this doesn’t happen when the image filename is dynamic. For example:

<ul>
  <li v-for="item in items">
    <img v-bind:src="`icons/${item.slug}.png`" />
    {{ item.name }}
  </li>
</ul>

The src attribute in the browser’s DOM are exactly the result of template interpolation, which works out with Vite’s own development server but not in “production” since image files are missing.

Describe the solution you’d like

I sort of understand why this doesn’t Just Work, but it’d be nice if it did. Alternatively, is there some other ways to tell Vite about which images exist? The value of item.slug is always in some finite set, although there are more of them that I’d rather hard-code in a template. Or, am I doing something very wrong and shouldn’t use reactive data for this? I’m very new to Vue.

Describe alternatives you’ve considered

Moving these images to the public directory would probably work, but Vite’s README describes this as an escape hatch that is best avoided.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:13
  • Comments:24

github_iconTop GitHub Comments

9reactions
amir20commented, Mar 15, 2021

As mentioned in https://github.com/vitejs/vite/issues/1265#issuecomment-757450141, I tried using glob like const images = import.meta.globEager("/src/images/*.png");. It wasn’t working at first, then I realized that you need to use .default like images["/src/images/image.png"].default.

Just FYI for any one else trying to use globs with static assets.

6reactions
GhimpuLucianEduardcommented, Jul 23, 2021

Based on @amir20 I ended up doing something like this:

export default function useAssets() {
  const svgs = import.meta.globEager('/src/assets/*.svg');
  const pngs = import.meta.globEager('/src/assets/*.png');
  const jpegs = import.meta.globEager('/src/assets/*.jpeg');

  return {
    aboutImage: svgs['/src/assets/aboutImage.svg'].default,
    search: svgs['/src/assets/search.svg'].default,
    info: pngs['/src/assets/info.png'].default,
  }; 
}

Then in any file:

<template>
    <div>
      <img :src="assets.info">
    </div>
</template>
  
<script lang="ts">
import { defineComponent } from '@vue/runtime-core';
import useAssets from '../composable/useAssets';
  
export default defineComponent({
setup() {
  const assets = useAssets();
    return {
      assets,
    };
  },
});
</script>

This way you can keep all your assets in one place and avoid magic strings. It’s a little bit more work to add each asset in the composable return, but in the long run it’s quite nice,

Read more comments on GitHub >

github_iconTop Results From Across the Web

webpack - file-loader does not work with dynamic url in angular
In your particular case - yes, your path is simply ignored as an incorrect one. However, if your change it to ./{{assets}}flower.jpeg (just ......
Read more >
Dynamic URLs vs. static URLs | Google Search Central Blog
Myth: "Dynamic URLs cannot be crawled."​​ Fact: We can crawl dynamic URLs and interpret the different parameters. We might have problems crawling and...
Read more >
Static Asset Handling - Vite
Explicit URL Imports​​ Assets that are not included in the internal list or in assetsInclude , can be explicitly imported as a URL...
Read more >
How do I dynamically define Asset Volumes' Base URLs for ...
I have two sites, Site A and Site B, with their own assets folder. They are separate sites that will not share assets...
Read more >
Canonical URLs vs dynamic URLs: what's the difference?
A dynamic URL is a URL produced by a site at the moment when a user submits a search query into a search...
Read more >

github_iconTop Related Medium Post

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