If you look through the implementations of hook_init(), you see a lot of modules adding CSS. This means on every page load, those CSS files will show up, no matter whether or not they're actually being used. In #323112: Vertical Tabs, we introduced the #attached property, which will add the supporting CSS files. We should start using that instead of putting the calls to drupal_add_css in hook_init().

This patch tries to hit every module doing this, except for user module.

CommentFileSizeAuthor
hook_init_css.patch8.26 KBrobloach

Comments

Status: Needs review » Needs work

The last submitted patch failed testing.

sun’s picture

+++ modules/poll/poll.module	11 Nov 2009 19:59:47 -0000
@@ -220,6 +213,9 @@
   $form = array(
     '#cache' => TRUE,

ugh - how is this possible? #583730: How many ways are there to cache a form? should have removed all #cache properties in $forms.

Could you also roll with diff -up, please?

I'm on crack. Are you, too?

moshe weitzman’s picture

$node->content['poll_view_results'] = array(
+      '#markup' => poll_view_results($node, TRUE, TRUE),
+      '#attached' => array('css' => array(
+        drupal_get_path('module', 'poll') . '/poll.css',
+      )),
+    );

Not related to this patch, but #markup in a node rendering is no good at all. Damn poll.module.

Status: Needs work » Needs review
Issue tags: -Performance, -DX (Developer Experience), -DrupalWTF, -D7DX

dendie requested that failed test be re-tested.

Status: Needs review » Needs work
Issue tags: +Performance, +DX (Developer Experience), +DrupalWTF, +D7DX

The last submitted patch failed testing.

sun’s picture

Cross-linking the actual counter-part for this issue: #769226: Optimize JS/CSS aggregation for front-end performance and DX

owen barton’s picture

Title: Stop including CSS in hook_init() » Check non-preprocessed CSS/JS is loaded only when really needed

Now that the #769226: Optimize JS/CSS aggregation for front-end performance and DX is committed the focus of this shifts somewhat. I am not clear what there is that actually falls into this category, but I think it would be worth checking.

Status: Needs work » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.