Generate plugin assets for json files

One more topic on missing plugin assets:

Are only assets of a certain filetype moved (or aliased) to the media/ folder ? Or all (as I previously expected)? When I try to access various assets I need for my Plugin, I found out, that at least in my case, only some assets are moved to the media folder:

$path = 'media/plugins/my-name/plugin-name';

// correctly moved to the media folder
echo url( $path . "/test.png" );
echo css( $path . "/test.css" );
echo js(  $path . "/test.js" );

// correct url is printed, but file not moved to media folder, 404
echo url( $path . "/test.css" );
echo url( $path . "/test.js" );
echo url( $path . "/test.json" );
echo url( $path . "/test.txt" );
echo url( $path . "/test.unityweb" );

So in case of the css file: It is moved when using the css() function, but not with url(). While url() does work for the image file.

However, I just need the correct URL, nothing else. And therefore, the file has correctly be aliased/copied to the media folder.

Do I have to do it manually for those file types such as json, … or should that happen automatically? Do I have to register those filetypes somewhere?Am I missing something? Or is there another recommended way to access β€œspecial” plugin assets?

Thank you very much for your ideas!

(Me, one month later and smarter:)

Yes, only certain file types are copied to the media folder. The route that takes care of that lives in kirby/config/routes.php:

$media . '/plugins/(:any)/(:any)/(:all).(css|gif|js|jpg|png|svg|webp|woff2|woff)'

You can extend that list with a small plugin or integrate the following in your existing plugin:

<?php
/**
 * copied from kirby/config/routes.php
 * modified to just add the file types you need in addition
 */

use Kirby\Cms\PluginAssets;

$media = $kirby->url('media');

// ↓ you only need that, if you want to be flexible with renaming the `media` folder
$index = $kirby->url('index');
if (Str::startsWith($media, $index) === true) {
    $media = Str::after($media, $index);
} else {
    // media URL is outside of the site, we can't make routing work;
    // fall back to the standard media route
    $media = 'media';
}
// ↑

Kirby::plugin('you/link-additional-file-types-in-media-folder', [

    'routes' => [
        [
            // put here a list of all the extensions you want to support
            'pattern' => $media.'/plugins/(:any)/(:any)/(:all).(json|map|unityweb|otf)',
            'env'     => 'media',
            'action'  => function (string $provider, string $pluginName, string $filename, string $extension) use ($kirby) {
                return PluginAssets::resolve($provider . '/' . $pluginName, $filename . '.' . $extension);
            }
        ]
    ]

]);
1 Like

This was really helpful. Thanks for posting the solution!