drupal_add_js causes blocks to be executed twice I added this code to template.php (in Zen subtheme):

function mytheme_preprocess_page(&amp;$vars) {
  if ($vars['is_front']) {
    drupal_add_js (libraries_get_path('jquery.cycle') . '/jquery.cycle.all.js', 'file');
  }
}


Just after adding it, each block (e.g. drupal search block, my custom blocks etc) and also this preprocess function are executed twice. I checked it using a debugger. The output on pages is OK, but executing blocks twice has performance implications.

By the way, the same happens if the JavaScript file is added in mytheme_preprocess_html() instead of mytheme_preprocess_page.

Is hook_preprocess_page() the right place to add JavaScript code? (I guess not.) Is there another way of adding JavaScript to a specific page other than the above? (I know I can add in mytheme.info, but it will add to each page, whereas it's needed only on front page.)
I finally discovered two circumstances when blocks are executed twice:

First is adding a missing javascript file, either in theme.info file or in theme_preprocess_page() function using drupal_add_js.

Second is by adding a broken url(..) image reference in a css file which is actually used on the page.

Here are the steps to recreate the problem with css, for javascript it's analogously:


install Drupal (it has Bartik theme as default) and log in, to see
the search box
in a debugger, set a breakpoint inside function search_block_view()
refresh the page and check that the breakpoint is reached once
go to /themes/bartik/css/styles.css line 1467 and change the line containing:


background: url(../images/search-button.png)

into something that does not exist:

background: url(../images/search-button1.png)


refresh the page with shift and check that the breakpoint is reached
twice


Maybe such behavior is obvious for someone more advanced and has rational explanation, but for me it is not obvious and took me a while to discover. Anyway the conclusion is just to make sure that all loaded css and javascript resources are available and nothing is missing, otherwise blocks will be silently executed twice and depending what they contain, the page may load longer.