Onebox fail with 429 error should still create valid <img> tags

AI-generated summary

rahim123 is experiencing issues with Imgur’s rate limiting (429 error) on their Discourse forum. When this occurs, Discourse incorrectly interprets the error as an invalid image link and displays the URL as a link with <a> tags. However, users can still view the image inline. To handle this issue better, rahim123 suggests that Discourse should generate valid <img> tags even if it receives a 429 error, and only display the naked URL if it receives a 404 error.

On my Discourse forum users are encouraged to use external image hosting services when possible. The most common is Imgur, which after a while starts rate-limiting the Discourse server with a 429 error. While this is an unsolvable problem with Imgur, Discourse also doesn’t handle it optimally. Currently when Imgur throws a 429 error Discourse interprets it as if it were an invalid image link and it just displays the image URL with <a> tags. But there is nothing preventing individual users from viewing those images inline in Discourse since their browser and IP are different. Discourse should still generate valid HTML to display the image. For example:

https://i.imgur.com/eHltaVt.jpeg
This URL gets rate-limited on my server and Discourse just generates a link:
<a href="https://i.imgur.com/eHltaVt.jpeg" target="_blank" rel="noopener noreferrer" tabindex="-1">https://i.imgur.com/eHltaVt.jpeg</a>

But the following HTML works fine to display the image within the end-user’s browser, both in the post preview and in the baked post:
<img src="https://i.imgur.com/eHltaVt.jpeg">
That is the HTML that Discourse should generate even if it receives a 429 when the server hits the URL, and it should only leave the naked URL if it gets a 404 error.

3 Likes