If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Simplifying website caching

Started by hwpiy, Oct 21, 2023, 12:01 AM

Previous topic - Next topic

hwpiyTopic starter

I developed a website, uploaded it to hosting, checked it on PageSpeed Insights, and discovered that caching needed to be enabled. I was informed that enabling caching manually was very difficult and was suggested to switch my site to the Wordpress engine and install a plugin. However, no matter how much I thought about it, I decided to proceed with the installation and activation of the plugin. Unfortunately, the site did not get cached as expected. Seeking a solution, I learned that caching can be easily implemented through .htaccess when working with HTML/CSS.

Now I am wondering if I can simply place my layout on hosting and configure caching via .htaccess. Will this ensure that the site functions properly without any future issues or difficulties?
  •  


kredu

if your site is using HTML/CSS and you have an Apache-based server. This approach to caching can greatly improve your website's loading speeds. However, bear in mind that while it's relatively straightforward, there are still potential complications and challenges.

Here's a basic example of how you might set up caching:

In your .htaccess file, you might add directives like:

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##
This code specifies how long different file types should be cached in the user's browser. Accordingly, adjust these times based on your desired caching strategy.

Furthermore, these key points are crucial to keep in mind:

.htaccess can be a powerful tool, but one misplaced character or incorrect directive can wreak havoc on your site. Always back up your existing .htaccess file before making changes, and be prepared to restore it if things go awry.

You need to be aware that .htaccess might not work on non-Apache servers, such as Nginx or Microsoft servers.

Apart from enabling browser caching, make sure your website is optimized in other ways as well. Minimize the use of large images, use minified versions of CSS and JavaScript files, and consider implementing a content delivery network (CDN) to deliver your content more quickly to users worldwide.

Re-test your site with PageSpeed Insights or a similar tool after making changes, to ensure that your adjustments are having the intended positive effect.

Keep your user experience central to your strategy. While it's important to achieve good performance metrics, ultimately your site needs to serve your users well.

Caching is best for static files. If your content changes often or if dynamic content is utilized, then cache settings should reflect these needs.

In sum, while using .htaccess for browser caching can be a piece of the puzzle, you must plan and implement a comprehensive performance optimization strategy across the board.


If you're seeking to clarify more about caching via .htaccess, here are further aspects to consider:

1. Gzip Compression:

Gzip compression can be implemented for faster data transfer between the server and the client's browser by compressing larger files into smaller sizes. Your .htaccess file can also handle this.

Example:

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
2. Leverage Browser Caching:

In addition to the ExpiresByType directive discussed in previous examples, you can also add the attributes for the "Leverage Browser Caching" through .htaccess, which further helps you optimize the site.

Example:

<IfModule mod_headers.c>
<FilesMatch ".(js|css|xml|gz|html)$">
Header append Vary: Accept-Encoding
</FilesMatch>
</IfModule>
In this directive, Vary: Accept-Encoding will assist intermediaries like proxies and CDNs in handling gzipped static assets properly, which helps to serve your site's assets faster.

Remember: It is crucial to wisely set cache timing, considering the frequency of file updates. While caching is beneficial for a site's speed, the negative part is if you cache a file for 1 year and update that file on your server, users won't see that update until their cache updates or expires.


Along with caching strategies, here are other factors that may significantly influence your website's performance:

1. Image Optimization:

High-resolution images can significantly slow down your website. Consider using tools to optimize and compress images without diminishing their quality. Also take advantage of modern formats like WebP, which provides superior compression and quality characteristics compared to older formats like JPEG or PNG.

2. Minify CSS, JavaScript, and HTML:

Minifying your code removes unnecessary characters (like spaces and comments) from your HTML, CSS, and JavaScript. This doesn't change the functionality of the code but does increase your site speed by reducing the size of the files that need to be downloaded for the page to load.

3. Use a Content Delivery Network (CDN):

A CDN hosts your site's files across a network of global servers. This means that when someone visits your website, their browser downloads files from the server that's geographically closest to them, which reduces load times.

4. Optimizing Database:

If you're using a Content Management System (CMS) like WordPress, Joomla, or Drupal, your website relies on a database to function. Regular database optimization - just like regularly defragmenting your computer's hard drive - helps to keep your site running smoothly.

5. Postpone Loading of JavaScript:

Loading JS files can take up substantial bandwidth that may hinder loading the rest of your webpage. Postponing JS loading until after other elements have loaded can speed up your pages. Be careful, though: improperly deferring certain scripts can break features of your website.

6. Use HTTP/3:

HTTP/3 improves upon previous versions of the HTTP protocol, and it can result in faster website loading times. However, it's not universally supported and requires specific server configurations to use.
  •  

johmarcovaSemi

You can include the following code in your htaccess file to achieve the desired result:

<ifModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/css text/javascript application/javascript application/x-javascript
</ifModule>

<IfModule mod_gzip.c>
  mod_gzip_on Yes
  mod_gzip_dechunk Yes
  mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
  mod_gzip_item_include mime ^text\.*
  mod_gzip_item_include mime ^application/x-javascript.*
  mod_gzip_item_include mime ^application/javascript.*
  mod_gzip_item_exclude mime ^image\.*
  mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>

In addition, it is recommended to set cache control headers for certain file types to optimize performance:

<ifModule mod_headers.c>
    <filesMatch "\.(ico|jpg|jpeg|png|gif|swf)$">
        Header set Cache-Control "public"
    </filesMatch>

    <filesMatch "\.(css)$">
        Header set Cache-Control "public"
    </filesMatch>

    <filesMatch "\.(js)$">
        Header set Cache-Control "public"
    </filesMatch>

    <filesMatch "\.(x?html?|php)$">
        Header set Cache-Control "private, must-revalidate"
    </filesMatch>
</ifModule>

To further enhance caching, you can set expiration dates for specific file types:

<ifModule mod_expires.c>
    Expires Active On
    ExpiresDefault "access 7 days"
    ExpiresByType image/x-icon "access plus 6 month"
    ExpiresByType image/jpeg "access plus 6 month"
    ExpiresByType image/jpg "access plus 6 month"
    ExpiresByType image/png "access plus 6 month"
    ExpiresByType image/gif "access plus 6 month"
    ExpiresByType application/x-shockwave-flash "access plus 6 month"
    ExpiresByType text/css "access plus 7504800 seconds"
    ExpiresByType text/javascript "access plus 7516000 seconds"
    ExpiresByType application/javascript "access plus 7516000 seconds"
    ExpiresByType application/x-javascript "access plus 7516000 seconds"
    ExpiresByType text/html "access plus 600 seconds"
    ExpiresByType application/xhtml+xml "access plus 600 seconds"
</ifModule>

These modifications will help optimize the performance and caching of your website.
  •  

feloBiaf

Ignore any advice you receive from others!
1. Acquire knowledge on caching through independent reading.
2. Adjust the settings based on the guidelines provided in the official dоcumentation.

Taking insightful suggestions and feedback into account is crucial for successful development. However, it is also important to conduct your own research and gain a deeper understanding of the concept of caching. By doing so, you can tailor the configuration to meet your specific needs.

Remember to consult reliable sources, such as the dоcumentation, to ensure accurate implementation and optimal performance. Comprehending the intricacies of caching can greatly benefit your applications and contribute to their efficiency and speed.

3. Once you have made the necessary adjustments, it is advisable to evaluate the performance of your website using tools like Google PageSpeed Insights. This will enable you to identify any further optimizations that can be made to enhance the overall user experience and increase the loading speed of your pages.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...