Skip to content

Images get emitted to _astro, even though they're not used in the output #15505

@spaceemotion

Description

@spaceemotion

Astro Info

Astro                    v5.17.2
Vite                     v6.4.1
Node                     v24.13.0
System                   Linux (x64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             none

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

When importing an image, either synchronously via import Image from '...', or async via import('...'), the final build output in dist/_astro contains the images even though no Astro-native image functions are used (e.g. getImage, or the Image/Picture components from astro:assets).

In the reproduction, I am only importing the image metadata itself, and process it manually into a base64 Data URI string. However, the final build output still contains a processed image (dist/_astro/example.C_B8VLaJ.png).

I replicated this with both, Astro 5 as well as the latest Astro 6 beta.

Curiously enough, this also happens when just importing an image to get its width or height, and not even touching its src property.

Why depend on ImageMetadata in the first place?

If I try to work around this via importing an image using the ?url query (e.g. import Image from './example.png?url'), Astro already processes the URL into something that points to the _astro/ folder (and not the original vite-internal module source).

What's the expected result?

Images that aren't actively used in the output should not appear in the final build output (as in, rendered to HTML).
Times when I expect images to be included:

  1. When adding to the module graph via:

    <img src={MyImage.src} />
  2. When using the Astro-native getImage method:

    const processedImg = await getImage({
      src: MyImage,
      width: 200,
      height: 200,
      format: 'webp',
    })
  3. When using the Astro-native Image and Picture components (which use getImage under the hood, afaik).

  4. Manually calling the emitImageMetadata function - as it implies we're "emit"-ing an asset.

Link to Minimal Reproducible Example

https://github.com/spaceemotion/astro-bug-incorrect-image-emits

Participation

  • I am willing to submit a pull request for this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    - P2: has workaroundAn edge case that only affects very specific usage, but has a trivial workaround (priority)pkg: astroRelated to the core `astro` package (scope)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions