JavaScript and CSS files are currently attached to a page. However, (semantically) they should be attached to the page fragment they belong to. This is required so that cached page fragements retain their CSS/JS when loaded from cache. Example:

  • Block adds JavaScript file using drupal_add_js
  • Block content is being cached
  • Another page: Block is retrieved from cache and displayed

The Problem: The added JavaScript file is no longer present as it was added to the original page, and not associated with the block

The same problem also applies to CSS files, HTML headers (using drupal_set_html_head), HTTP headers and so on.

The same problem exists not only for blocks, but for most cachable items which can arbitrary actions (e.g. forms are cached, too).

The cache API already has a partial solution for this: the $headers parameter. This should be extended to allow arbitrary “meta data” to be associated with a cachable item.

The Vertical Tabs patch needs this in order to show descriptions on submitted forms (e.g. the preview button).

CommentFileSizeAuthor
#4 block_meta.zip3.21 KBkkaefer
#3 js-css-recording.patch3.1 KBkkaefer
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

kkaefer’s picture

When reproducing, please note that UID 1 never uses the block cache.

kkaefer’s picture

I'm currently not sure if it really makes sense to extend the cache tables because both the block and forms implementation have to be modified to cache js/css files and there's little code that can be shared among the two. Also, we can't move the functionality of adding the JavaScript files back to the current page when a cached item is retrieved since that cached item might not be used on that page.

kkaefer’s picture

Status: Active » Needs review
FileSize
3.1 KB

This patch goes another way: it records function calls and replays them when requested. That way, we can record drupal_add_css/drupal_add_js function calls and replay them when the cached item is used.

kkaefer’s picture

FileSize
3.21 KB

This module provides proof that this works. You should test with a UID > 1 and block cache enabled.

dmitrig01’s picture

needs top be extensible, ATM css and JS are hardcoded

kkaefer’s picture

It is extensible; you can use drupal_record()->add('drupal_set_html_head', array('<title>Foo</title>'));.

dmitrig01’s picture

i mean in blocks and forms you're limited to css and js

dmitrig01’s picture

maybe just 1 array for all stuff that needs to be readded?

kkaefer’s picture

Ah yeah, makes sense... Do you have a name suggestion?

David Strauss’s picture

Subscribing.

Status: Needs review » Needs work

The last submitted patch failed testing.