1

In drupal 6, I've written a module that outputs some html which I am displaying in a block using hook_block.

There is a js file that I want to load only when that block loads so I am drupal_add_js() inside hook_block.

It is all working except for this problem: when you refresh the page (regardless of whether you clear caches at the same time), often an old version of the script gets loaded. Newer and older versions turn up on a seemingly random basis.

What can I do to ensure that the correct version is loaded? Thanks.

Edit: One of the stylesheets that I'm loading via the theme's .info file is doing the same thing - random versions of it are loading. So it looks like a general problem not module or block related.

naomi
  • 1,855
  • 1
  • 13
  • 28
  • I am not a drupal expert, but did you try eg. drupal_add_js('yourfile.js?ver=123456') ? – nez Aug 28 '12 at 09:26
  • Thanks. Hard coding the version number sounds like it can't be right though. It would mean people working on style or js would have to hack the php when they made changes ... doesn't seem like the drupal way ... – naomi Aug 28 '12 at 09:51
  • This is not a good approach, no. 'Problem' lies in the internal server-cache-mechanism in drupal. A click on 'clear cache' in the drupal administration would probably refresh contents am i right? Changing the src-href on script tag would force a cache re-render (bad if get params are random == each request builds a cache). Also, it will force the client browser cache to create a completely new entry. See below how to avoid this workaround. – mschr Aug 29 '12 at 07:58
  • You can use the solution that is addressed in another topic here: http://stackoverflow.com/questions/32414/how-can-i-force-clients-to-refresh-javascript-files – Joni Van Bogaert Aug 28 '12 at 12:48

1 Answers1

0

Until youre satisfied with the javascript file, so that it may be locked down and used in cache, use the 'defer' and 'cache' arguments to drupal_add_js.

In admin > > performance you will find directives that includes js and css - if they're deactivated, then one request is made per file added by drupal_add_*. If for instance js has preprocess set true, then all .info javascripts and scripts added by drupal_add_js will get sutured together into: one optimized deeply cached script

< 7.0

drupal_add_js($data = NULL, $type = 'module', $scope = 'header', $defer = FALSE, $cache = TRUE, $preprocess = TRUE)

7.0 >

drupal_add_js($data = NULL, $options = NULL)

In your situation, if youre developing a script or need to push one fresh on every load, under all circumstances use false under $cacheand $preprocess, so loading 'main.js' looks like:

drupal_add_js(base_path() . '/sites/default/files/myjs/main.js', 
     'core', 
     'header', 
     false, 
     false, 
     false
);

In v7+ func argv[1-X] would simply be put together in an associative array as $options, see API

mschr
  • 8,221
  • 3
  • 19
  • 35
  • to avoid client cache mechanisms, follow answer by Joni – mschr Aug 29 '12 at 07:54
  • That's what I ended up doing and it works so I've ticked it. Thanks for the detailed explanation too! But it still seems like a bit of a hack - having to change the arguments to drupal_add_js on deploy to production ... – naomi Aug 29 '12 at 12:08
  • if youre in production mode - you really shouldnt have to, your scripts are changing? If you are developing, simply go here: admin/settings/performance and tick no in 'optimize javascript'.. If so, then rest is not drupal - but your apache / iis / whatever that sets cache-control headers – mschr Aug 29 '12 at 16:24
  • I didn't think of Apache but yes that makes sense - we are getting the same behaviour with stylesheets despite optimize being unticked for both. Thanks – naomi Aug 30 '12 at 08:10
  • Its like this; your browser live view and a cache view, server a file object (be it php, asp or js css whatever). The headers `Expires, Cache-Control, ETag, Last-Modified, Date` then is communicated between browser and server - if servers version is newer then the browsers cached view - browser will fetch a new. But only, if you call a 'force-refresh' in browser like `CTRL + r`. Otherwise browsers tend to never ask if server has a new version – mschr Aug 30 '12 at 12:35