I have a module I wrote that prints dynamic data for JavaScript variables.

function javascript_api_page_attachments(array &$page) {
$page['#attached']['html_head'][] = [
        // The data.
        '#type' => 'html_tag',
        // The HTML tag to add, in this case a  tag.
        '#tag' => 'script',
        // The value of the HTML tag, here we want to end up with alert("Hello world!");.
        '#value' => javascript_api_print_parameters(),

    // A key, to make it possible to recognize this HTML  element when altering.

My problem is when Drupal caches the page.Need to stop caching my pages in drupal.

How can I avoid the output of my module is cached?

  • Why are you adding JavaScript like this? – Eyal Dec 24 '16 at 9:02
  • Because all the variables are created by the module. – Haniel Bitton Dec 24 '16 at 10:44
  • 1
    You have drupalSettings to pass variables. – Eyal Dec 24 '16 at 10:48

As Eyal mentioned, you can pass the variables from your module to jQuery like this:


function mymodule_page_attachments(array &$attachments) {
    $attachments['#attached']['library'][] = 'mymodule/auto';
    $attachments['#attached']['drupalSettings']['cool'] = 'hello world';     


  version: 1.0
    js/auto.js: {}
      - core/jquery


(function($, Drupal, drupalSettings) {

    $( document ).ready(function() {

})(jQuery, Drupal, drupalSettings);

This will show a js alert message saying "hello world".

| improve this answer | |
  • Hi my problem is not in the way i am adding the js to the page . my problem is this function: javascript_api_print_parameters() collect all the module the implement the hook and create a dynamic parameters . because drupal caching is enabled the first time you hit the page the parameters are sticky to this page . – Haniel Bitton Dec 24 '16 at 12:01
  • @HanielBitton is this a custom page created by your module? or is this a content type node page? – No Sssweat Dec 24 '16 at 12:08
  • its a regular node page. – Haniel Bitton Dec 24 '16 at 12:10
  • @HanielBitton what data are you grabbing from the node page? or Exactly how does your dynamic data get created? – No Sssweat Dec 24 '16 at 12:26

I suggest you first create a Js file, put your Js function there then pass desire variable ( or object) to it with using drupalSettings and try to get variable(s) in your js files. ( I guess you are know how attach js libraries to page if not take a took at Helper1 and Helper2

$page['#attached']['drupalSettings']['myvars'] = $myvars;

Then Drupal.drupalSettings.myvar will be exists (it's will be dynamic and not cached) in your Js file.

| improve this answer | |

To disable the page cache for anonymous users try the first method:

// Deny any page caching on the current request.    

Else, you need to set max-age 0 in e.g. hook_node_view().

function yourmodule_node_view(array &$build, NodeInterface $node, $display, $view_mode) {
  if ($node->getType() == 'yourtype' && $view_mode == 'full') {
    $build['#cache']['max-age'] = 0;

OR, if you like to remove cache from route, provide no_cache in routing.yml of your module.

  path: '/abc'
    _controller: '\Drupal\your_module\Controller\your_controller::render'
    _permission: 'access content'
    no_cache: TRUE
| improve this answer | |

Thanks for the help i came up to create a new page with a controller and this page will be attached to the page .


namespace Drupal\javascript_api\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Response;

 * An example controller.
class PageParamsController extends ControllerBase {
    public function build(){
        $result = javascript_api_print_parameters();
        return new Response($result , 200 , array('Content-Type'=> 'application/javascript'));

  # Block the page from being loaded until Modernizr is initialized.
  header: true
  version: "v1"
    /javascript_api/pageparms.js: { preprocess: 0, weight: -21, minified: true }

Now i have a dyanmic js that i can put on the page and to use my Variable in the global area.

Thanks All :)

| improve this answer | |
  • I don't think that the hook_page_attachments method will be cached. And it is always better to go with the framework than against it. – Eyal Dec 24 '16 at 22:20
  • It actually been cached tried that when added the JavaScript to the head section. – Haniel Bitton Dec 25 '16 at 11:17

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.