0

I'm trying to figure out if in my case it makes sense to use the High-performance JavaScript callback handler module. Between js/MODULE/example.json (hook_js()) and example.json (hook_menu()), the difference of time is not huge, but I think hook_js() is faster.

In a case I'm not using any Drupal function, variable or session, and I'm only calling another server with REST. Can I bypass all the bootstrap functions, or do I have to use the first layer (DRUPAL_BOOTSTRAP_CONFIGURATION)?

What would be the faster solution?

function MODULE_js(){
    return array(
      // Example URL is /js/MODULE/example.json
      'example.json' => array(
        'callback' => 'my_json',
        'includes' => array('unicode', 'locale', 'language'),
        'dependencies' => array(),
        'bootstrap' => DRUPAL_BOOTSTRAP_CONFIGURATION,
        'access callback' => 'access_callback',
        'access arguments' => array(),
        'page arguments' => array(),
        'skip_hook_init' => TRUE,
        'delivery callback' => 'drupal_json_output',
      ),
    );
}
function access_callback (){
    return true;
}


function MODULE_menu()
    $items['example.json'] = array(
        'type' => MENU_CALLBACK,
        'page callback' => 'my_json',
        'page arguments' => array(),
        'access callback' => true,
        'delivery callback' => 'drupal_json_output',
    );
return $items;
}

function my_json(){
    //call with curl/rest and return a json
    return 'json';
}
2

Actually, as the project documentation says, you should use http://example.com/js.php?q=js/mymodule/callback instead of http://example.com/index.php?q=js/mymodule/callback, and basing on the test reported from the maintainer, the previous is faster than the latter (59.371 ms per request versus 446.846 ms per request). Since you are waiting a reply from another server, probably you would not notice that difference too.

For you second question, if you are going to use the High-performance JavaScript callback handler module in the way shown in the documentation, js.php bootstraps Drupal at DRUPAL_BOOTSTRAP_DATABASE level. In any case, if you are not using any Drupal function nor the session, and you are using just plain PHP functions or classes, then you don't need to bootstrap Drupal at all. Keep in mind that this means you cannot use any function implemented by Drupal modules too.

  • 1
    Isn't the idea that you set a rewrite rule to route /js/(.*) to js.php, and not use the clean URL version at all? I'm trying to remember, it's been a while since I set a site up with this module – Clive Dec 7 '15 at 17:59
  • Yes there is a rewrite rule to implement in the htaccess. – GwenM Dec 8 '15 at 14:04
  • How can I avoid the bootstrap then? if I'm using js.php, it forces me to use at least DRUPAL_BOOTSTRAP_DATABASE, is it the solution? Or do I need to call a custom php file? – GwenM Dec 8 '15 at 14:15
  • @GwenM You cannot avoid js.php bootstraps Drupal at that level. You need to find a different way, if you don't need bootstrapping Drupal at DRUPAL_BOOTSTRAP_DATABASE level. – kiamlaluno Apr 21 '18 at 17:57

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.