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

Can't add assets after update #117

Open
mmcrt opened this issue May 10, 2022 · 4 comments
Open

Can't add assets after update #117

mmcrt opened this issue May 10, 2022 · 4 comments
Assignees

Comments

@mmcrt
Copy link

mmcrt commented May 10, 2022

Recently I updated both Grav (v1.7.33) and shortcode-core (v5.1.1). Since then a third party plugin stopped working.

It seems that shortcode-core doesn't load assets at all. On a clean plugin sample (using the [strike] shortcode from shortcode-core main page) I successfully get the formatted string (with <del>) but trying to add assets (either js or css) doesn't seem to work.

The code is really simple:

public function init()
{
    $this->shortcode->getHandlers()->add('strike', function(ShortcodeInterface $sc) {
            
        $this->shortcode->addAssets('js', 'plugin://test-plugin/js/alert.js');
        $this->shortcode->addAssets('inlinejs', 'alert("-");');
        $this->shortcode->addAssets('inlinejs', 'console.log("-");');
        $this->shortcode->addAssets('css', 'plugin://test-plugin/css/helloworld.css');

        return '<del>'.$sc->getContent().'</del>';
    });
}

Only the <del> part gets successfully added and rendered.

Am I doing something wrong?

@rhukster
Copy link
Member

I will take a look.

@mmcrt
Copy link
Author

mmcrt commented May 10, 2022

I will take a look.

Thank you. Just for reference the other plugin used $this->grav['assets']->addJs in place of $this->shortcode->addAssets.

@rhukster
Copy link
Member

Shortcode’s addAssets() function is mandatory because it does things differently to ensure the assets are added to the page meta data so they are not lost when the content is rendered and cached.

@rhukster
Copy link
Member

I just tested with Shortcode-UI plugin which has various JS and CSS and it works fine even when cache is enabled:

...
<link href="/grav-sandbox/user/plugins/shortcode-ui/css/ui-accordion.css?g-ae33dc60" type="text/css" rel="stylesheet">
<link href="/grav-sandbox/user/plugins/shortcode-ui/css/ui-tabs.css?g-ae33dc60" type="text/css" rel="stylesheet">
<link href="/grav-sandbox/user/plugins/shortcode-ui/css/ui-browser.css?g-ae33dc60" type="text/css" rel="stylesheet">
<link href="/grav-sandbox/user/plugins/shortcode-ui/css/ui-tooltips.css?g-ae33dc60" type="text/css" rel="stylesheet">
<link href="/grav-sandbox/user/plugins/shortcode-ui/css/ui-callouts.css?g-ae33dc60" type="text/css" rel="stylesheet">
<link href="/grav-sandbox/user/plugins/shortcode-ui/css/ui-cslider.css?g-ae33dc60" type="text/css" rel="stylesheet">
<link href="/grav-sandbox/user/plugins/shortcode-ui/css/ui-atext.css?g-ae33dc60" type="text/css" rel="stylesheet">
...
<script src="/grav-sandbox/user/plugins/shortcode-ui/js/ui-tabs.js?g-ae33dc60"></script>
<script src="/grav-sandbox/user/plugins/shortcode-ui/js/ui-tooltips.js?g-ae33dc60"></script>
<script src="/grav-sandbox/user/plugins/shortcode-ui/js/ui-cslider.js?g-ae33dc60"></script>
<script src="/grav-sandbox/user/plugins/shortcode-ui/js/ui-atext.js?g-ae33dc60"></script>
...

And they are being added the same as you:

$this->shortcode->getHandlers()->add('ui-animated-text', function(ShortcodeInterface $sc) {
    // Add assets
    $this->shortcode->addAssets('css', 'plugin://shortcode-ui/css/ui-atext.css');
    $this->shortcode->addAssets('js', 'plugin://shortcode-ui/js/ui-atext.js');
....

Also nothing has really changed in either the Asset Manager or shortcode core when it comes to assets in a quite some time.

Are you sure the assets you are linking actually exist? If the assets are not found, they will be skipped.

@rhukster rhukster self-assigned this May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants