Update to Kirby 5: direct Image Links do not work anymore

This is regarding links to images uploaded to panel:
e.g.

Page “news”, uploaded Image: “blog-orginal.jpg”

In Kirby 4 you could call mydomain.com/news/blog-orginal.jpg

this worked

My customer often puts hardcoded html-snippets into the content and uses the images that way, I have a few hundred occurences of that kind.

In Kirby 5 : 404

I added a custom route to make that work.

However I now tried to find out what should be the correct way.

Docs said: use the link in the panel associated with the image, which is
/media/pages/news/c7994f67f2-1762266993/blog-orginal.jpg

However, this has 2 disadvantages:

  1. not SEO-friendly
  2. If the image is updated the hash changes. I understand that this is a cache-busting-mechanism, but it’s a catastrophe for all external links.

Is this really the way to go?

Would my custom route do any harm?

 'pattern' => '(:all)/(:any)\.(pdf|jpg|jpeg|png|gif|webp|svg)',

    'language' => '*',

'action'  => function ($language, string $parent, string $slug, string $ext) {

$cache = kirby()->cache('file-redirects');

$key = 'file-route:' . $parent . ':' . $slug . ':' . $ext;

$result = $cache->get($key);




if ($result !== null) {

if ($result === 'notfound') {

return false;

        }

      }




$page = page($parent);

if (!$page) {

$cache->set($key, 'notfound', 600);

return false;

      }




$file = $page->file($slug . '.' . $ext);

if ($file) {

// Read file and send with correct content-type

$path = $file->root();

$content = file_get_contents($path);

$mime = mime_content_type($path);




header('Content-Type: ' . $mime);

header('Content-Length: ' . filesize($path));

header('Cache-Control: public, max-age=31536000');




$cache->set($key, $content, 600);

echo $content;

exit;

      }




$cache->set($key, 'notfound', 600);

return false;

    }

  ],

This has several advantages, yes, as outlined here: Image URLs - How to remove string? - #10 by bastianallgeier

In modern webdesign, you usually do not use images as uploaded, but optimize them for use on your website (convert jpg/png to modern image formats like webp/avif), and use srcsets to create responsive images.