Home » Php » javascript – What is the right way to call dynamic content (currently using ajax) inside a cached page?

javascript – What is the right way to call dynamic content (currently using ajax) inside a cached page?

Posted by: admin July 12, 2020 Leave a comment


We have a news website where we cache a complete article page.

There are 4 areas that need to continue to be dynamic on that page:

  1. View Counter: We add +1 to view_counts of that article when page loads.
  2. Header: On the header of the website we check if session->id exists or not if it does we display a Welcome [Name], My Profile / Logout and if not we show Register / Login.
  3. Comments: We display the comments made for that article.
  4. Track User Behavior: We track every single action made by users on the site

Now the only way we could think of doing this is through AJAX calls:

$('#usercheck').load(<?php echo "'" . base_url() . "ajax/check_header'"; ?>);

And so on.

This is creating a massive load on CPU, but what would be the right/alternative way of approaching this?

Please see attached:

The load in 60 minutes

The high transactions on the site

How to&Answers:

First of all, you do not have to use AJAX for every possible dynamic content, especially in the case of comments, you may as well load them via an iframe.
That way, you are not relying on Javascript to make the request.
It may even work for the counter.

However, you problem is not Javascript, nor the database server, based on what I can see from your graph. It seems to me you have some heavy PHP controllers, maybe you are loading a heavy framework just to have $session->id checked.

Further, what do you mean by “we track every single action”? How do you track them? Are you sending an AJAX request from every little thing or are you debouncing them with JS and only sending them one every 30 seconds or so?

My advice is that you consider the size of the PHP code you are calling, and slim it down as much as you can, even to zero if it seems feasible (by leveraging localStorage to keep track of you user session after the first login), and maybe loading the counter and the comments in alternative ways.

For example, I infer you are only checking the counter once per page load, ignoring subsequent loads by other users while the current user is reading the article, so your counter may happen to be out-of-date once i a while, depending on your traffic.

I going to explain it better: your page has n views, so when I load it, you request for n and then display n+1 to me. While I’m reading, the same page gets requested and viewed x times by other users. Your counter on the server has been surely updated to n+x, but the counter on my page still says “n views”.
So, what’s the point in being picky and showing n+1 to me and the not updating it, thus being off by x?

So, first of all the counter controller should be as slim as possible, and what if you loaded it within an iframe, auto updating without AJAX?

How to refresh an iframe not using javascript?

That would keep the counter up-to-date, you may render it with PHP just once per page view, and then just statically serve the resulting HTML file.