4

In a custom module a, I return a JSON string that needs to be compatible with the Boost module.

My first try was using the following code:

if ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 'XMLHttpRequest' == $_SERVER['HTTP_X_REQUESTED_WITH']) || strpos($_SERVER['REQUEST_URI'], '?json') !== FALSE) {
  header('Cache-Control: no-cache, must-revalidate');
  header('Content-type: application/json', true);
  $json = loadData();
  die(json_encode($json));
}

This is obviously that this output won't be cached. Than I replaced the line with die() with the following one:

return drupal_json_output(($json));

This works, but it is not cached. I enabled the JSON output in Boost, but nothing happens.

What should I try?


Removing the two header lines does not change anything. The function drupal_json_output adds the Content-Type: application/json. But I belive that boost only accepts text/javascript. Also if I override the Content-Type again nothing will be cached.

4 Answers 4

0

You need to add

module_invoke_all('exit');

to the template-file, immediately prior to the exit or die statement. This will trigger the boost-module's code that stores the generated output in a cache-file. Additionally, you need to change the content-type to text/javascript.

0

Why do you need to cache JSON output? Anyhow, try removing header('Cache-Control: no-cache, must-revalidate'); and see what happens.

8
  • I tried that. No changes. I need that because drupal needs about 1.2 seconds to response and this is annoying.
    – rekire
    Apr 17, 2012 at 11:52
  • Did you tried like this: if(( isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 'XMLHttpRequest' == $_SERVER['HTTP_X_REQUESTED_WITH'] ) || strpos($_SERVER['REQUEST_URI'], '?json')!==FALSE) { header('Content-type: application/json', true); $json=loadData(); return drupal_json_output(($json)); }
    – Zyy
    Apr 17, 2012 at 11:55
  • Tried to clear cache?
    – Zyy
    Apr 17, 2012 at 12:05
  • Ehm after flushing the cache boost stoped to work. Nothing is cached at the moment...
    – rekire
    Apr 17, 2012 at 12:18
  • Yep, It needs to rebuild the content. Anyhow JSON should not be cached, because you won't get real-time data. Otherwise - consider other format for content delivery.
    – Zyy
    Apr 17, 2012 at 12:25
0

Boost currently doesn't support application/json Content-Type. See this issue for details and patches.

1
  • I stopped using Boost but that makes sense, why that did not work for me.
    – rekire
    Mar 11, 2013 at 19:03
0

This might help others who are trying to have Boost cache JSON data.

Boost wont cache "application/json", but there are settings to cache TEXT/JAVASCRIPT AJAX request of type "text/javascript"

One problem you are going to find with : drupal_json_output() It uses : drupal_add_http_header('Content-Type', 'application/json');

So a solution you can try is this :

$data = array("hello","world");
drupal_add_http_header('Content-Type', 'text/javascript');
echo drupal_json_encode($data);
drupal_exit();

So if you have Boost "AJAX/JSON SETTINGS" enabled it should now cache the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.