JS and CSS in hook_preprocess_hook() In the project I am working on I have created a theme hook that renders a .tpl.php file:

/**
 * Implements hook_theme().
 */
function my_module_theme() {

  $theme['my_module_list'] = array(
    'variables' =&gt; array(
      '#markup' =&gt; array(),
    ),
    'path' =&gt; drupal_get_path('module', 'my_module'),
    'template' =&gt; 'templates/my_module_list',
  );

  return $theme;
}


Then I want some JS and CSS to be added only when this theme hook is used in the current page. Thus I implemented of theme_preprocess_hook() as such:

/**
 * Implements template_preprocess(). 
 */
function my_theme_preprocess_my_module_list(&amp;$vars) {
  drupal_add_js(drupal_get_path('module', 'my_module') . '/js/my_module.js');
  drupal_add_css(drupal_get_path('module', 'my_module') . '/css/my_module.css');
}


However it appears that the preprocess function is not called every time the hook is used resulting in CSS and JS not being added. What am I missing? Is it bad practice to add it there? And if yes where should I add it in order for it to be loaded only when necessary? I couldn't find any documentation that clarifies it and I've also seen it done other places.

Thanks a lot in advance :-)
I would add JS/CSS files inside the preprocess_page() function. If I don't want the CSS/JS files loading on some pages I will use an IF statement and check for a setting or if something exists etc, this will depend on the project.