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

Inconsistent behaviour between build and dev with /src/assets #10634

Open
1 task
WilfSilver opened this issue Apr 1, 2024 · 10 comments
Open
1 task

Inconsistent behaviour between build and dev with /src/assets #10634

WilfSilver opened this issue Apr 1, 2024 · 10 comments
Assignees
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)

Comments

@WilfSilver
Copy link

WilfSilver commented Apr 1, 2024

Astro Info

Astro                    v4.0.3
Node                     v21.5.0
System                   Linux (x64)
Package Manager          yarn
Output                   static
Adapter                  none
Integrations             @astrojs/starlight
                         @astrojs/react

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

All

Describe the Bug

I have an md file referencing an image/gif:

...
![my test image](/src/assets/image.gif)
...

If I run astro dev, it displays the image fine, however running astro build, the image does not show on the website (in place it just has the alt text)

From looking at the files, it does not transfer/build the images into the dist directory.

If I change the link to a relative one e.g. ../../assets/image.gif it works absolutely fine

What's the expected result?

Consistency between astro dev and astro build when using /src/assets.

EDIT: please see comment below, but in summary, the image should not be displayed in both instances

Link to Minimal Reproducible Example

https://github.com/WilfSilver/astro-asset-link-issue

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Apr 1, 2024
@WilfSilver
Copy link
Author

After further investigation, the issue seems to be that the images are actually displaying in astro dev.

I should be using the import.meta.env.ASSETS_PREFIX or via the path option in the typescript config explained here

Therefore, the expected result should be that astro dev does not display the images/prints out an error saying the images can't be found.

@WilfSilver WilfSilver changed the title Assets not built with link /src/assets Inconsistent behaviour between build and dev with /src/assets Apr 1, 2024
@Princesseuh
Copy link
Member

This is because Vite serves the filesystem in dev, nothing to do with assets or images in particular. I've always found this quite weird personally, but I assume that Vite uses this for specific reasons

@bluwy
Copy link
Member

bluwy commented Apr 2, 2024

Vite supports root-relative paths as it's advertised as a glorified filesystem server that serves files from your project root. There's a few explorations in the past to make this more strict, e.g. only allowing behind /@something/src/assets/... only, but that will be a really big breaking change.

I think we can support this in Astro if we want to since we call this.resolve here:

resolved:
(await this.resolve(imagePath, id))?.id ?? path.join(path.dirname(id), imagePath),

And it can resolve root-relative paths and public paths, and do some additional checks there. But I'm also fine if we want a blanket "starts with / is never handled":

function shouldOptimizeImage(src: string) {
// Optimize anything that is NOT external or an absolute path to `public/`
return !isValidUrl(src) && !src.startsWith('/');
}

@matthewp
Copy link
Contributor

matthewp commented Apr 2, 2024

I don't think we should try and support this, otherwise people will expect it in other places as well.

@matthewp matthewp added needs discussion Issue needs to be discussed and removed needs triage Issue needs to be triaged labels Apr 2, 2024
@ematipico
Copy link
Member

Yeah, I agree with Matthew here. I would keep the behaviour as simple as possible and clarify the user expectations.

@matthewp matthewp added - P3: minor bug An edge case that only affects very specific usage (priority) and removed needs discussion Issue needs to be discussed labels Apr 3, 2024
@matthewp
Copy link
Contributor

matthewp commented Apr 3, 2024

After discussing this we think this might be reasonable to support and not be too difficult to implement.

@DJOCKER-FACE
Copy link

in the meantime is there any way to have the images display normally on the build?

@bluwy
Copy link
Member

bluwy commented Apr 15, 2024

You can use relative paths, or use aliases like @assets/my-image.png

@DJOCKER-FACE
Copy link

@bluwy those didn't work, relative paths get passed as a string of text for some reason when used in .md and importing through assets directly doesn't work for build.

So this is how i kinda fixed this, i had to add those images on public/images, have to update astro(if not running latest) and then import them.

@WilfSilver
Copy link
Author

@bluwy those didn't work, relative paths get passed as a string of text for some reason when used in .md and importing through assets directly doesn't work for build.

Could you share the code for this? Relative links and aliases were working in my original project (and the example code - relative links are used as an example of how it should be working)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

No branches or pull requests

6 participants