How can caching improve the loading time of a website with a large JS file that is not frequently updated?
Caching can improve the loading time of a website with a large JS file that is not frequently updated by storing a copy of the file on the user's browser or server. This way, the file does not need to be re-downloaded every time a user visits the website, reducing loading times significantly.
<?php
$js_file = 'path/to/large.js';
$cache_time = 3600; // 1 hour
if(file_exists($js_file) && time() - filemtime($js_file) < $cache_time) {
header('Content-Type: application/javascript');
readfile($js_file);
} else {
// Generate or include the JS file here
// Save the generated file to $js_file
header('Content-Type: application/javascript');
readfile($js_file);
}
Related questions
-
Cross-browser compatibility navigation buttons PHP
-
older version software component PHP errors
-
PHP MySQL form data