I want to make a block of recently viewed nodes on a website which is completely cached using Boost module (it saves a static HTML page for every request on the website). I don't want to disable Boost and recache a page every time this block updated, instead, I wondering if it possible to use JavaScript here. Is there any way I can do this?

  • Short answer, sure? You'd have to wire up a menu route and JS with ajax that triggers the node statistics API (I assume this is what you are talking about).
    – Kevin
    Dec 6, 2021 at 16:40
  • @Kevin I was thinking of a client-only approach using some tracking of visited pages that are filtered by page address (if JS have such mechanic). But I'll be happy with any method available, since it's barely worth writing it from scratch.
    – NSp
    Dec 6, 2021 at 22:31
  • That data still has to be stored somewhere though - right? Or are you saying just using cookie or HTML5 LocalStorage? That would all be JS at that point basically, not much Drupal. The only Drupal part might be getting that output on the page, which you could maybe do with JS dom writing anyway.
    – Kevin
    Dec 6, 2021 at 23:36

1 Answer 1


Not a module, but still better than nothing. Note that some parts of the script are theme-aware.

This goes to the bottom of node--[type].tpl.php:

<?php if ($page): ?>
  <script type="text/javascript">updateRecentlyVisited();</script>
<?php endif; ?>

This to the new block (block's text format shouldn't change tags):

<script type="text/javascript">
var thisBlock = document.getElementById('[PUT BLOCK ID HERE]');
if (currentlyStored) {
  var recentlyVisited = '<ul">';
  for (var i = 0; i < currentlyStored.length; i++) {
    recentlyVisited += '<li><a href="' + currentlyStored[i][0] + '">' + currentlyStored[i][1] + '</a></li>';
  recentlyVisited += '</ul>';

  thisBlock.innerHTML = recentlyVisited;

And the script.js to attach in theme.info

var currentlyStored = JSON.parse(localStorage.getItem('recentlyVisited'));

function updateRecentlyVisited() {
    var maxLinks = 20;
    var currentPage = new Array (document.URL, document.getElementsByClassName('node-title').item(0).textContent);

    if (currentlyStored)        
        for (var i = 0; i < currentlyStored.length; i++) {
            if (currentlyStored[i][0] == currentPage[0]) {
        currentlyStored = new Array();
    if (currentlyStored.length > maxLinks)
    localStorage.setItem('recentlyVisited', JSON.stringify(currentlyStored));

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.