PHP vs Javascript?

I am currently working on a site that uses the PHP function to retrieve JSON data and display it on the page. However, when the page loads, it freezes until it receives a response that looks weird because it has not yet loaded the closing html tags.

I can make an ajax call with Javascript that will happen asynchronously after the page loads, but since the pages are static, I cache them using PHP, so the response will not be cached in this way.

Is there a way to make a PHP JSON call after loading the page using PHP, or can I cache the javascript JSON response?

+4
source share
2 answers

The approach you take is called bootstrapping. As a rule, it’s good practice to separate components where possible, in your case, if you upload a large amount of data to a page, it may be better to leave it as it is and cache the resulting html (Cloudflare is my favorite tool for this) .

Running an asynchronous javascript request for a large amount of data can lead to unwanted loading times on the input side, especially if you have the necessary libraries such as jQuery to load in the first place.

The larger the boot JSON, the better it is to use PHP.

+1
source

I remove JSON extraction from inline and use JavaScript to call AJAX. From there, you can run JSON through a separate PHP script on your site and add some additional caching, such as apc, to speed up the PHP call.

In apc caching, you will need mod_apc. Check out the apc_fetch and apc_store function calls you can use to cache JSON without requiring an expensive call so often.

If you make a GET request where all the API parameters are in the URL, you can do something similar to speed up repeated AJAX requests.

$url = "http://songkick.com/api/url/to/whatever"; $apcKey = "url:$url"; $data = apc_fetch($apcKey); if(!$data) { $data = file_get_contents($url); //or curl, or whatever you're using. apc_store($apcKey, $data); //save for next time. } echo $data; 
+3
source

Source: https://habr.com/ru/post/1485105/


All Articles