Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Nuxt images with a dynamic :src show broken or 404

ErrorDynamic image works with a fixed path but breaks when :src uses a variable

Short answer

Nuxt and Vite rewrite only static asset paths at build time. A :src built from a variable, such as ~/assets/img/${name}.png, is opaque to them, so it is never bundled. Put the files in public and use a root path, or import them with a literal path, a variable import or import.meta.glob.

  • Applies to Nuxt 4, Nuxt 3, Vite 7, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the browser network tab and read the failing image URL. A URL that still contains ~/assets means the path was never rewritten.

    grep -rn ":src=" app components pages 2>/dev/null
  2. 2Match what you see, then jump to its fix

    1. 1The failing URL still shows ~/assets or /assets with no hashA ~/assets path inside a bound :srcFix
    2. 2Image names are decided at runtime or come from a CMS, and you do not need hashed file namesThe images should live in public insteadFix

What does the error look like?

  • Dynamic image works with a fixed path but breaks when :src uses a variable
  • Image with a dynamic :src does not load after nuxt build

Cause 1 of 2

A ~/assets path inside a bound :src

1Check

Look for :src with a template string that starts with ~/assets. The build tool rewrites literal src values only.

grep -rn "~/assets" app components pages 2>/dev/null

2Fix

Import the images through Vite. For a known set, use import.meta.glob with the url query and look the file up by name.

<script setup lang="ts">
const props = defineProps<{ name: string }>()
const images = import.meta.glob<string>('./assets/img/*.{png,jpg,svg}', {
  query: '?url',
  import: 'default',
})
const src = await images[`./assets/img/${props.name}.png`]?.()
</script>

<template>
  <img :src="src" alt="">
</template>

3Verify

Run a production build and open the page.

npx nuxt build
npx nuxt preview

Confirmed by nuxt.com

Did this fix it?

Cause 2 of 2

The images should live in public instead

1Check

Files in public are served from the root with their original names. Files in assets are not served at a static URL.

ls public/img

2Fix

Move the files into public and build the URL with a root path.

const imageUrl = computed(() => `/img/${props.name}.png`)

3Verify

Request the file straight from the running site.

curl -I http://localhost:3000/img/logo.png

Confirmed by nuxt.com

Did this fix it?

How do I stop it coming back?

  • Use static paths in templates whenever the file is known at build time.
  • For runtime names, pick one pattern (public, glob or variable import) and use it everywhere.
  • Do not use new URL(..., import.meta.url) in server-rendered code; Nuxt documents that it does not work with SSR.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.