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

Feature Request: Flexible placement of plugin assets (files & inline styles/code) #876

Closed
ulilu opened this issue Jun 2, 2016 · 5 comments

Comments

@ulilu
Copy link

ulilu commented Jun 2, 2016

In order to get the fastest possible "Time to render" i'm doing critical path optimization. So far i had some pleasant surprises with using grouping, prioritizing, etc to sort the load order. However, there is one major problem, that i can't seem to overcome.

In base.html.twig, next to the assets i add myself, Grav automatically adds JS (and CSS) files from the activated plugins which ALWAYS are placed within {{ assets.css() }} (or .js). As far as i know, there is no way to change that. This is really problematic in regards to optimization.

In my case, i have a separate critical path CSS file, which i want to inline in the header, using the Custom CSS Plugin. Since this file contains all necessary styles for content "above the fold", i want to relocate ALL OTHER CSS to the bottom, just before the</body>.

Unfortunately, this is not possible because the inline style as well as all the plugin styles (in my case Featherlight, Lightslider) get grouped automatically in {{ assets.css() }} and appear in the same place in the DOM.

Now, this is very similar with JS-Assets:
I tried to completely relocate my JS from the <head> and put it just before the</body>. Unfortunately the LightSlider-Plugin is constructed in a way, that it contains inline JS code inside a twig-template. And because this JS code needs jQuery to run, i need to call jQuery in the <head>, making critical path optimization effectively impossible.

I really think that there is huge potential for optimization here, to make grav as fast as possible, especially on mobile, where it really counts. I'd love to hear your feedback on this...

@rhukster
Copy link
Member

rhukster commented Jun 2, 2016

why not just move the {{ assets.css() }} to the bottom of the template, and put a new {{assets.css('top') }} at the top?

This way all the default stuff will load at the bottom, and you can put specific ones you want at the top in that top group.

Regarding jQuery, you could manually add it to your top group and remove it from the default group.

@rhukster
Copy link
Member

rhukster commented Jun 2, 2016

Actually there's no easy way currently to get and set the assets manually. I'm going to add some getter/setters.

rhukster added a commit that referenced this issue Jun 3, 2016
…d allow full control of manipulating assets via plugins. #876
@rhukster
Copy link
Member

rhukster commented Jun 3, 2016

Ok that should let you do pretty much anything from a plugin or theme event. If you know the asset path, you can manipulate it.. either remove it, and then add a custom one, or you could just get the whole array, find the one you want, modify it, including changing group, and then setting the array back on the Assets manager.

@rhukster rhukster closed this as completed Jun 3, 2016
@ulilu
Copy link
Author

ulilu commented Jun 3, 2016

By moving {{ assets() }} to the bottom, the inline styles set via the Custom CSS Plugin get moved to the bottom as well. Unless there is another method of directly inlining the critical path styles at the top without using the Custom CSS Plugin (a twig command to "write out" the contents of a CSS-file perhaps?) i don't know what to do... wait, oh wow! Thanks for the commit, i'm gonna check it out and get back to you (Awesome :-)

@boredland
Copy link

Has this been solved? If yes: could you let me know how you did it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants