We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Async Javascript Assets

Does anyone know how to make the output from $this->assets->outputJs(); an async javascript?

At the moment, I'm doing this just to get it working:

$contents = ob_get_contents();
echo str_replace('<script ', '<script async ', $contents);

What I would prefer is a more beautiful solution.

You could do this by adding attribute array in addJs().

$this->asset->addJs('path/to/file', null, false, array('async' => 'async'));

Second and third parameter should be adjusted to your needs, this are only the defaults.

Thanks for that. How do I do it if I'm combining more than one javascript file?

Simply call this function for each file you want to insert.

You can add as many as you want and outputJs() prints the html for your site.

OK, so I call the function like this:

            ->addJs('assets/v1/js/jquery-2.1.1.js', true, null, array('async' => 'async'))
            ->addJs('assets/v1/js/jquery.cookie.js', true, null, array('async' => 'async'))
            ->addJs('assets/v1/js/default.js', true, null, array('async' => 'async'))
            ->addFilter(new \Phalcon\Assets\Filters\Jsmin());

But it only generates the following code:

<script type="text/javascript" src="/assets/v1/production/final.js"></script>

That's correct, because you join all files you inserted into the collection to the final.js

I don't know if async works in this case, because the single files are never send to browser, only the joined file.

Maybe I should put a change request in. Thanks for your help.

edited Dec '14


interesting discussion. @Gareth: did you try without the join statement ? Does this work ?

Sorry for the delay, but yes I just tried it without the join statement, and I get a link to final.js without async.



you can use this :

        ->setAttributes(array('async' => true))

Refer to the doc' : https://docs.phalcon.io/pt/latest/api/Phalcon_Assets_Collection.html

It'll product :

<script type="text/javascript" src="/main.js" async="1"></script>