#<Image> not working for github attachments

83 messages ยท Page 1 of 1 (latest)

errant nacelle
#

First I tried in mdx

import { Image } from 'astro:assets';

## Abc

<Image width="2878" height="1610" src="https://github.com/user-attachments/assets/62464ccc-a34f-4bef-a819-37d4ff1a0931" loading="lazy"
alt='kekw' />

when I try to use md syntax for image ( check 2nd image )

[alt](link)

Astro config

    image: {
        domains: ['github.com'],
        remotePatterns: [
            { hostname: '**.github.com' },
            { hostname: '**.githubusercontent.com' },
        ],
    },

All I want is optimizing remote images. whats the best approach to this?

main bison
#

I get a completely different error, but maybe a newer version of Astro made the error more clear.

fresh tiger
#

Also: Could you try removing the width and height attributes, but add inferSize instead. This is specifically for remote images. Not sure if this is related, but maybe the sizes are not right? ๐Ÿค”

main bison
#

It's a CORS problem. As always.

#

Wait no that doesn't make sense xD

#

The server is fetching it so it should go through.

#

Ok, I see the actual issue.

#

At least for the error in my case, so I would like to confirm what version of Astro you are running.

main bison
#

Huh. Well I can't replicate the exact error you are getting.

#

Regardless, I think the issue is that GitHub returns a 302 for their uploaded images.

#

The Astro image handling explicitly doesn't handle redirects but it really doesn't give a proper error message.

errant nacelle
main bison
#

I get

FailedToFetchRemoteImageDimensions
Failed to retrieve remote image dimensions
errant nacelle
#

๐Ÿ˜ฎ

main bison
#

Because of the stack trace ๐Ÿ˜„

errant nacelle
#

only the <img> works

main bison
#

Try with an image from like https://picsum.photos/200/300 (which also redirects with a 302).
You should get the same error.
And then open it in the browser, and copy the redirected link into the markdown, then it should work.

#

Just to see if the 302 is actually the issue.

main bison
#

Otherwise it will not do anything with it.

#

I'm trying to speedrun writing a content collection right now.

errant nacelle
#

true

main bison
#

Okay yeah when rendering <Content /> I get the same error.

errant nacelle
#

phew

main bison
#

Looks like the 302 is not the issue? Although I don't get how it could not be.

#

Let's do some pnpm patch'ing

#

I patched in some extra logging... and I get Remote returned 400
(Edit: this makes sense, GitHub includes a unique ID in the query parameters so it only works directly from the redirect)

#

What

#

Anyway, the main issue right now is that the image link is returning a 302 redirect and Astro just doesn't handle them.

#

From reading the code it seems to be intentional.

#

I checked with the picsum images after adding them to the optimization and they also don't work because of the same issue.
And when I manually replace the URL with the URL from after the redirect, the picsum images start to work normally.

errant nacelle
main bison
#

I wanna ask one of the maintainers but I'm not sure how to be honest ๐Ÿฅฒ

#

Because I think having a redirecting URL as an image is pretty common.

fresh tiger
dreamy rivet
#

Ayup, just seen this. Is the TL;DR that the image component (and other stuff) doesn't work when there's redirects involved?

#

I think that could be patched in, maybe under a flag or a "allow how many redirects" kinda deal

dreamy rivet
#

Alright, I'll get on it!

dreamy rivet
#

Yeah I think I can make an option for that, to follow redirects

main bison
# dreamy rivet Yeah I think I can make an option for that, to follow redirects

Yo, thanks for looking at it.
From my tests it's a bit more complicated, and I don't fully understand what's happening.

This line seems to block redirects. I patched some extra logging to confirm this was the underlying error when the Failed to parse image reference error showed up.

const response = await fetch(url, { redirect: 'manual' });

But for some reason when trying to replicate it in StackBlitz, the image from picsum which 100% redirects did work.
The image from GitHub still didn't work. But the StackBlitz doesn't really work with PNPM patches so I couldn't confirm what was going differently there.
The only difference between those is that picsum redirects to the same domain (a subdomain), while GitHub redirects to s3.amazonaws.com.

#

Those two cases don't have a different code path from what I can tell.

dreamy rivet
#

Yeah I'm at about the same point rn

#

I'll just make a followRedirects option to set that redirect thing to follow, seems to work locally so far

main bison
#

Doesn't that create a security issue, allowing domains other than configured in the domains config?

#

Not sure, maybe if you trust a domain itself you should trust that it redirects properly as well.

dreamy rivet
dreamy rivet
main bison
# dreamy rivet

Nice. Could you test the GitHub link posted here as well?
It was a bit weird in my testing.

dreamy rivet
#

Yep, just a sec

#

Yeah the github url works too

main bison
dreamy rivet
#

I'll still need to add tests and check in with other maintainers and the docs about the config option and how I added it, but I can probably get this done today, Friday at the latest

#

So if all goes well this'll land in 6.2, if not, 6.3

dreamy rivet
#

Alright, next minor (6.2) seems likely. It'll evolve a bit until then but we've had others bring this up too

errant nacelle
#

wow a whole lot of work just for my issue ๐Ÿ˜ญ . Thanks guys

dreamy rivet
#

We gotcha!

dreamy rivet
#

Whelp, I tried! It'll be 6.3, unfortunately

dreamy rivet
#

Release for this is Thursday or Friday!

errant nacelle
#

Coool

errant nacelle
#

i see the 2nd pr being closed too ๐Ÿ˜ญ

dreamy rivet
#

Yeah it's merged! 6.3 has image redirects!

errant nacelle
dreamy rivet
#

Yep

#

It's dependent on your image.remotePatterns or image.domains configuration

errant nacelle
dreamy rivet
#

If you run into issues, let me know!

errant nacelle
dreamy rivet
#

You know, good question. kek

errant nacelle
#

works regardless ๐Ÿš€

dreamy rivet